apache-plc4x - 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();
}
}
解决方案
我已经使用 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 用例中,我不需要任何掩码。
推荐阅读
- python - 如何在 PyCharms 的 python 控制台中编辑“import sys”?
- java - 为 Java LocalDateTime 生成正确的招摇规范
- javascript - 模糊时验证输入。希望点击下拉菜单不会触发模糊,因为验证不起作用
- node.js - Mongo DB 驱动程序“找不到模块 'bson'。” 并且没有智能感知
- ios - 清理画布时拖放新的 UILables 时出现错误,如何解决?
- haskell - 将位置、值元组列表转换为单个列表
- java - 不允许模拟最终对象
- mysql - MYSQL 计算两种不同条件下的行数,如果计数相等,则返回值作为输出
- c++ - 对于写入二进制文件的接口,应该将 void*/std::size_t 函数参数对更改为什么?
- php - 在 WooCommerce 购物车和结帐表中显示产品自定义字段值