我在使用jamod写入modbus时遇到了一个奇怪的情况。以下读取代码完美地运行:
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("host.somewhere");
TCPMasterConnection connection = new TCPMasterConnection(address);
connection.setPort(502);
connection.connect();
ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(0, 1);
ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse)
executeTransaction(connection, request);
}
private static ModbusResponse executeTransaction(TCPMasterConnection connection,
ModbusRequest request)
throws ModbusIOException, ModbusSlaveException, ModbusException {
ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
transaction.setRequest(request);
transaction.execute();
return transaction.getResponse();
}
然而,试图以类似的方式编写却失败了(Jamod尝试了3次,每次遇到SocketTimeoutException,最终抛出了ModbusException)。
public static void main(String[] args) throws Exception {
final InetAddress address = InetAddress.getByName("host.somewhere");
final TCPMasterConnection connection = new TCPMasterConnection(address);
connection.setPort(502);
connection.connect();
Register reg = new SimpleRegister(0);
WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(0,
new Register[]{reg});
executeTransaction(connection, request);
}
是的,我知道我正在使用多寄存器版本的请求对象,但我正在使用的设备仅支持功能码3和16。
我还编写了原始套接字测试程序来写入寄存器,就我所见,它运行正常。但在这两种情况下使用jamod会更好。
有没有人有使用jamod的经验,并且能否告诉我我做错了什么?这在jamod的1.1和1.2rc1版本中都发生了。或者这可能是某个特定供应商的情况吗?