繁体   English   中英

使用pymodbus读取寄存器

[英]Using pymodbus to read registers

我正在尝试使用pymodbus从PLC读取modbus寄存器。 我遵循此处发布的示例。 当我尝试print.registers ,出现以下错误: object has no attribute 'registers'该示例未显示正在导入的模块,但似乎是可接受的答案。 我认为错误可能是我导入了错误的模块或缺少模块。 我只是想阅读一个寄存器。

这是我的代码:

from pymodbus.client.sync import ModbusTcpClient    
c = ModbusTcpClient(host="192.168.1.20")
chk = c.read_holding_registers(257,10, unit = 1)
response = c.execute(chk)
print response.registers

从读取的pymodbus代码 ,看来该read_holding_registers对象的execute方法将返回一个响应对象 ExceptionResponse包含错误的对象。 我想你会收到后者的。 您需要尝试这样的事情:

from pymodbus.register_read_message import ReadHoldingRegistersResponse
#...
response = c.execute(chk)
if isinstance(response, ReadHoldingRegistersResponse):
  print response.registers
else:
  pass # handle error condition here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM