首页 > 解决方案 > Plc4x addressing system

问题描述

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();
        }
    }

标签: apache-plc4x

解决方案


我已经使用 PLC4x 成功地使用 modbus 驱动程序进行编写。这是我正在使用的一些示例代码:

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));
}

在 modbus 写入的情况下,写入器 Future 的返回存在问题,但写入已完成。在 modbus 用例中,我不需要任何掩码。


推荐阅读