简体   繁体   English

使用pymodbus读取寄存器

[英]Using pymodbus to read registers

I'm trying to read modbus registers from a PLC using pymodbus. 我正在尝试使用pymodbus从PLC读取modbus寄存器。 I am following the example posted here . 我遵循此处发布的示例。 When I attempt print.registers I get the following error: object has no attribute 'registers' The example doesn't show the modules being imported but seems to be the accepted answer. 当我尝试print.registers ,出现以下错误: object has no attribute 'registers'该示例未显示正在导入的模块,但似乎是可接受的答案。 I think the error may be that I'm importing the wrong module or that I am missing a module. 我认为错误可能是我导入了错误的模块或缺少模块。 I am simply trying to read a register. 我只是想阅读一个寄存器。

Here is my code: 这是我的代码:

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

From reading the pymodbus code , it appears that the read_holding_registers object's execute method will return either a response object or an ExceptionResponse object that contains an error. 从读取的pymodbus代码 ,看来该read_holding_registers对象的execute方法将返回一个响应对象 ExceptionResponse包含错误的对象。 I would guess you're receiving the latter. 我想你会收到后者的。 You need to try something like this: 您需要尝试这样的事情:

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