Plc4x library Modbus serial (RTU) get is not retrieving data

711 views Asked by At

I am trying to write a sample program to retrieve temperature data from SHT20 temperature sensor using serial port with apache plc4x library.

private void plcRtuReader() {

    String connectionString =
        "modbus:serial://COM5?unit-identifier=1&baudRate=19200&stopBits=" + SerialPort.ONE_STOP_BIT + "&parityBits=" + SerialPort.NO_PARITY + "&dataBits=8";

    try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionString)) {

      if (!plcConnection.getMetadata().canRead()) {
        System.out.println("This connection doesn't support reading.");
        return;
      }

      PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
      builder.addItem("value-1", "holding-register:258[2]");
      PlcReadRequest readRequest = builder.build();

      PlcReadResponse response = readRequest.execute().get();
      for (String fieldName : response.getFieldNames()) {
        if (response.getResponseCode(fieldName) == PlcResponseCode.OK) {
          int numValues = response.getNumberOfValues(fieldName);
          // If it's just one element, output just one single line.
          if (numValues == 1) {
            System.out.println("Value[" + fieldName + "]: " + response.getObject(fieldName));
          }
          // If it's more than one element, output each in a single row.
          else {
            System.out.println("Value[" + fieldName + "]:");
            for (int i = 0; i < numValues; i++) {
              System.out.println(" - " + response.getObject(fieldName, i));
            }
          }
        }
        // Something went wrong, to output an error message instead.
        else {
          System.out.println(
              "Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
        }
      }

      System.exit(0);
    } catch (PlcConnectionException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

Connection is established with the device using serial communication. But it fails to get data and instead prints the below warning messages continously.

debugger hangs at below line:

      PlcReadResponse response = readRequest.execute().get();

with below logs printing continuously.

2021-06-03-17:41:48.425 [nioEventLoopGroup-2-1] WARN  io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 512 times in a row; rebuilding Selector org.apache.plc4x.java.transport.serial.SerialPollingSelector@131f8986.
2021-06-03-17:41:55.080 [nioEventLoopGroup-2-1] WARN  io.netty.channel.nio.NioEventLoop - Selector.select() returned prematurely 512 times in a row; rebuilding Selector org.apache.plc4x.java.transport.serial.SerialPollingSelector@48c328c5.

With same URL data (i.e baudrate,stopBits etc..) using modpoll.exe it works and returns the data over RTU. I am not sure what is missing here. Kindly shed some light here.

0

There are 0 answers