使用 Jamod 写入 modbus
在使用 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 的经验吗?那位能好心告诉我我做错了什么吗? 1.1 和 1.2rc1 版本的 jamod 都会出现这种情况。或者这可能是供应商特定的情况?
I came across with a curious situation when using jamod to write to modbus. Following read code works perfectly:
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();
}
But trying to write similar manner fails (Jamod tries 3 times, each times encounters SocketTimeoutException and finally throws 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);
}
Yes, I know that I am using multi-register versions of the request-objects, but device I'm working with only supports function codes 3 and 16.
I also wrote raw-socket tester to write registers, and as far as I can see it works correctly. But it would be nice to use jamod in both situations.
Does anyone have anyone experience using jamod and would that one be kind enough to tell what I'm doing wrong? This happens with both 1.1 and 1.2rc1 versions of jamod. Or is this possibly some vendor-specific situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
默认情况下,Modbus 请求的单元 id = 0。因此,必须将任何其他 id 设置为请求,例如:
浪费了几个小时尝试解决您在问题中描述的相同问题。
Modbus requests have unit id = 0 by default. So any other id must be set to request, e.g.:
Wasted a few hours trying to solve the same problem you described in the question.
最后我写了自己的modbus实现。我只需要支持2个不同的功能代码,所以实现很简单。
虽然后来我发现了另一个java的开源modbus库。我其他人使用 modbus4j 遇到同样的问题可能会有所帮助。
At the end I wrote my own modbus implementation. I only needed to support 2 different function codes, so implementation was simple.
Although I later found another open source modbus library for java. I someone else comes across the same problem using modbus4j might help.
我根据你的问题写的方法有效!
My method which i wrote based on your question works!
我遇到了类似的问题,我试图在“设备作业”中定义为线圈寄存器的寄存器中写入一个值。所以我使用:
这解决了问题。也许这有帮助。
再见!
I had a similar problem, I was trying to write a value in a register that was defined in the "device job" as a coil register. So I used:
and that solved the problem. Maybe this help.
Bye!
我在安卓上也遇到过这个问题。
由于此任务在等待外部设备的响应时可能会花费大量时间,因此我找到的解决方案是在另一个线程中执行写入和读取。
I have encountered this problem on Android.
Because this task can take some significant time as it waits for a response from an external device, the solution I found was to execute writing and reading in another thread.