Plc4x cannot read more than 9 registers at once

630 views Asked by At

I am trying to understand the address system in the plac4x java implementation. Below an example of the reading code of the plcs:

    @Test
    void testReadingFromPlc() {
        // Establish a connection to the plc using the url provided as first argument
        try( PlcConnection plcConnection = new PlcDriverManager().getConnection( "modbus:tcp://1.1.2.1" ) ){
            // Create a new read request:
            // - Give the single item requested the alias name "value"
            var builder = plcConnection.readRequestBuilder();
            builder.addItem( "value-" + 1, "register:1[9]" );
            builder.addItem( "value-" + 2, "coil:1000[8]" );
            var readRequest = builder.build();

            LOGGER.info( "Synchronous request ..." );
            var syncResponse = readRequest.execute().get();
            // Simply iterating over the field names returned in the response.
            var bytes = syncResponse.getAllByteArrays( "value-1" );
            bytes.forEach( item -> System.out.println( TopicsMapping.byteArray2IntegerArray( item )[0] ) );
            var booleans = syncResponse.getAllBooleans( "value-2" );
            booleans.forEach( System.out::println );
        }catch(Exception e){
            e.printStackTrace();
        }
    }

Our PLCs manage 16 registers, but the regex of the addresses don't allow to have a quantity bigger than 9. Is it possible to change this?

Moreover, if I try to add an other field with the same purpose then no reading happen:

    var builder = plcConnection.readRequestBuilder();
    builder.addItem( "value-" + 0, "register:26[8]" );
    builder.addItem( "value-" + 1, "register:34[8]" );
    builder.addItem( "value-" + 2, "coil:1000[8]" );
    var readRequest = builder.build();

Any help much appreciated. Could you also show me where I can find more information on this framework?

1

There are 1 answers

1
Alvaro del Castillo On

I am reading and writing using the modbus driver in PLC4x with success. I have attached some writing code to your other question at: Plc4x addressing system

About reading, here is some code:

  public static PlcReadResponse readModbusTestData(ProtocolClient client,
                                                   String registerName,
                                                   int offset,
                                                   int size,
                                                   String registerType)
    throws ExecutionException, InterruptedException, TimeoutException {

    PlcReadRequest readRequest = client.getConnection().readRequestBuilder()
      .addItem(registerName, registerType + ":" + offset + "[" + size + "]").build();
    return readRequest.execute().get(2, TimeUnit.SECONDS);
  }

The multiple read adding more items to the PlcReadRequest has not been tested yet by me, but it should work. Writing several items is working.

In any case, in order to understand how PLC4x works for modbus or opc-ua I have needed to dive into the source code. It works, but you need to read the source code for the details at its current state.