Plc4x addressing system

682 views Asked by At

I am discovering the Plc4x java implementation which seems to be of great interest in our field. But the youth of the project and the documentation makes us hesitate. I have been able to implement the basic hello world for reading out of our PLCs, but I was unable to write. I could not find how the addresses are handled and what the maskwrite, andMask and orMask fields mean.

Please can somebody explain to me the following example and detail how the addresses should be used?

    @Test
    void testWriteToPlc() {
        // 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.writeRequestBuilder();
            builder.addItem( "value-" + 1, "maskwrite:1[1]/2/3", 2 );
            var writeRequest = builder.build();

            LOGGER.info( "Synchronous request ..." );
            var syncResponse = writeRequest.execute().get();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
1

There are 1 answers

0
Alvaro del Castillo On

I have used PLC4x for writing using the modbus driver with success. Here is some sample code I am using:

public static void writePlc4x(ProtocolConnection connection, String registerName, byte[] writeRegister, int offset)
    throws InterruptedException {

    // modbus write works ok writing one record per request/item
    int size = 1;
    PlcWriteRequest.Builder writeBuilder =  connection.writeRequestBuilder();

    if (writeRegister.length == 2) {
      writeBuilder.addItem(registerName, "register:" + offset + "[" + size + "]", writeRegister);
    }
...
    PlcWriteRequest request = writeBuilder.build();
    request.execute().whenComplete((writeResponse, error) -> {
      assertNotNull(writeResponse);
    });
    Thread.sleep((long) (sleepWait4Write * writeRegister.length * 1000));
}

In the case of modbus writing there is an issue regarding the return of the writer Future, but the write is done. In the modbus use case I don't need any mask stuff.