在尝试modbus-tk的同时,我也得到了同样的错误响应,所以在我的Python代码中,一定是什么引起了这个问题。 从传统系统(vb.net )获取的读数与在JACE中看到的输出相同。
这是从Modbus获取数据的简单功能。 我们是40160和40162,第一个是读366哪个是正确的,第二个是读数367。 在其他寄存器中也看到了同样的问题。 在这些寄存器中,如果在JACE中看到值增加,则不会更新读取。
#-* -编码: utf-8-* -
from __future__ import division,print_function,unicode_literals
frompymodbus.client.syncimportmodbustcpclient
frompymodbus.constantsimportendian
frompymodbus.payloadimportbinarypayloaddecoder
defget _ Modbus _ register _ data (IP _ address,register,device,count=2) :
“”'
retrieve Modbus数据。
“”'
client=Modbus TCP client (IP _ address,timeout=10 )。
client.connect (
#读注册
response=client.read _ holding _ registers (
address=register,# 40162
count=count,# 2
unit=device(#4
decoder=binarypayloaddecoder.from registers ()
registers=response.registers,
byteorder=Endian.Big,
wordorder=Endian.Little )
value=decoder.decode _ 32 bit _ float (
client.close (
return value # 366 and it should be 367
pymodbus调试日志
debug : py Modbus.transaction : currenttransactionstate-idle
调试: py Modbus.transaction :运行传输1
debug : py Modbus.transaction : send :0 x 00 x 000 x 000 x 60 x 30 x 00 xa 00 x0x 2
debug : py Modbus.client.sync : newtransactionstate ' sending '
debug : py Modbus.transaction : changingtransactionstatefrom ' sending ' to ' waiting for reply '
调试: py Modbus.transaction : changingtransactionstatefrom ' waiting for reply ' to ' processing reply '
debug : py Modbus.transaction 3360 recv :0 x 00 x 000 x 000 x 70 x 40 x 30 x 00 x 000 x 430 XB 7
debug : py Modbus.framer.socket _ framer : processing 33600 x 000 x 0000 x 70 x 30 x 40 x 000 x 000 x 000 x 000 x 000 x 000 x 000 x x 400 x
调试: py Modbus.factory : factory response [ readholdingregistersresponse 3360 ]
debug : py Modbus.transaction : adding transaction 1
debug : py Modbus.transaction : getting transaction 1
debug : py Modbus.transaction : changingtransactionstatefrom ' processing reply ' to ' transaction _ complete '
更新
在三局的帮助下,有人给我指出来了。 我用的偏移可能不正确。 实际上,通过将偏置变更为1(40162-40001=161 ),可以从寄存器中取出正确的值。 这意味着wordorder必须更改为Endian.Big。
更新的代码
defget _ Modbus _ register _ data (IP _ address,register,device,count=2) :
“”'
retrieve Modbus数据。
“”'
client=Modbus TCP client (IP _ address,timeout=10 )。
client.connect (
#读注册
response=client.read _ holding _ registers (
address=register,# 40161
count=count,# 2
unit=device(#4
decoder=binarypayloaddecoder.from registers ()
registers=response.registers,
byteorder=Endian.Big,
wordorder=Endian.Big )
value=decoder.decode _ 32 bit _ float (
client.close (
return value # 367