我有以下情况:我需要将信息发送到将显示在 PTV 屏幕上的标签 400001 到 400006。我没有太多使用modbus的经验,但据我了解,我应该使用Function code 16来设置标签值。
我目前有以下代码,这似乎在模拟器中工作:
// Open the connection
TCPMasterConnection connection = new TCPMasterConnection(address);
connection.setPort(Modbus.DEFAULT_PORT);
connection.connect();
ModbusTCPTransaction trans = new ModbusTCPTransaction(connection); //the transaction
// Prepare the request
SimpleRegister[] registers = new SimpleRegister[1];
registers[0] = new SimpleRegister(15);
WriteMultipleRegistersRequest req = new WriteMultipleRegistersRequest(400001, registers);
req.setUnitID(1);
trans.setRequest(req);
//No we are ready for action. The last part is executing the prepared transaction the given (repeat) number of times and then for cleanup, close the connection:
log.debug("ModbusSlave: FC" + req.getFunctionCode() + " ref=" + req.getReference() + " value=" + registers[0].getValue());
trans.execute();
// Close the connection
connection.close();
但是如何设置像“0010323”这样的字符串呢?