0

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.

  • 1
    The current version of the Modbus driver is a pure Modbus TCP driver. However the community is currently working on implementing the additional Modbus RTU driver, which shouldn't be much additional work. Unfortunately it's currently not finished. – Christofer Dutz Jun 03 '21 at 14:19
  • 1
    However you could try the 0.6.1 version of PLC4X, because 0.7.0 was an internal switch to fully generated drivers and the old version used an external library that could support serial communication. – Christofer Dutz Jun 03 '21 at 14:20
  • Thankyou so much for quick response. I am currently using 0.8.0 version of plc4x library. I shall try 0.6.1 version and see if it works for serial communication and let you know the result. – Purushothama Yanamala Jun 04 '21 at 04:43
  • I see there is no 0.6.1 version in maven and hence I will try with 0.6.0 instead. – Purushothama Yanamala Jun 04 '21 at 04:51
  • It fails with error Connection url doesn't match the format 'modbus:{type}//{port|host}' The following is the dependency added. org.apache.plc4x plc4j-api 0.6.0 – Purushothama Yanamala Jun 04 '21 at 05:06
  • URL:modbus:serial://COM5?unit-identifier=1&baudRate=19200&stopBits=1&parityBits=0&dataBits=8 org.apache.commons.lang3.NotImplementedException: Not implemented yet at org.apache.plc4x.java.modbus.connection.ModbusSerialPlcConnection.getChannelHandler(ModbusSerialPlcConnection.java:44) at org.apache.plc4x.java.base.connection.NettyPlcConnection.connect(NettyPlcConnection.java:67) at org.apache.plc4x.java.PlcDriverManager.getConnection(PlcDriverManager.java:73) at – Purushothama Yanamala Jun 04 '21 at 05:36
  • 1
    Oh ... yeah ... prior to the big refactoring of 0.7.0 all drivers sort of had their own syntax. We greatly cleaned that up with the new drivers. Sorry for having wasted your time. But rest assured that we're working on it. If you are willing and able to help, I would be glad to mentor you on this. In general it shouldn't be a lot of work as we generally have all the puzzle pieces in place. If you're interested, please sign up to dev@plc4x.apache.org (send an empty email to dev-subscribe@plc4x.apache.org) and say hi :-) – Christofer Dutz Jun 04 '21 at 08:21
  • Thank you so much for your clarifications and timely response. I have sent an email to dev-subscribe to be part of development activity. – Purushothama Yanamala Jun 04 '21 at 09:06

0 Answers0