Plc4x addressing system

蓝咒 提交于 2020-01-25 08:22:09

问题


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:


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.



来源:https://stackoverflow.com/questions/59153619/plc4x-addressing-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!