我已经通过使用j2mod成功地连接到我的modbusRTU,当我试图从保持40050到40054的寄存器读取数值时,我总是得到28826,28828,28830,28832,28834。我不知道这个值是什么,请帮帮忙。
ModbusSerialTransaction trans = null;
ReadMultipleRegistersRequest req = null;
ReadMultipleRegistersResponse res = null;
int unitid = 1; //the unit identifier we will be talking to
int ref = 40050; //the reference, where to start reading from
int count = 5; //the count of IR's to read
int repeat = 1; //a loop for repeating the transaction
//4. Open the connection
try {
//5. Prepare a request
req = new ReadMultipleRegistersRequest(ref, count);
req.setUnitID(unitid);
req.setHeadless();
//6. Prepare a transaction
trans = new ModbusSerialTransaction(con);
trans.setRequest(req);
int k = 0;
do {
trans.setTransDelayMS(50);
trans.execute();
res = (ReadMultipleRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) {
System.out.println("Word " + n + "=" + res.getRegisterValue(n));
}
k++;
} while (k < repeat);
con.close();
//8. Close the connection
} catch (ModbusException ex) {
System.out.print(ex);
}
please check this code.
发布于 2019-07-29 01:30:38
正如评论中所讨论的,您的问题被证明是非常容易解决的:您只是使用了错误的偏移量作为您想要读取的Modbus地址。
按照惯例,Modbus保持寄存器的编号从40000开始,但对于大多数库,不考虑此偏移量。
因此,要读取某个设备的Modbus映射中称为40011的寄存器,您必须使用索引号10。
按照同样的逻辑,要读取寄存器40050,您只需更改代码中的地址线:
int ref = 49; //the reference, where to start reading from
https://stackoverflow.com/questions/57125201
复制相似问题