【问题标题】:Using pymodbus to read registers使用 pymodbus 读取寄存器
【发布时间】:2016-09-09 22:54:48
【问题描述】:

我正在尝试使用 pymodbus 从 PLC 读取 modbus 寄存器。我正在关注here 发布的示例。当我尝试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

【问题讨论】:

  • @J Earls 错误消失但未读取寄存器。 Modbus Poll 读取正常。你发现代码还有什么问题吗?
  • 您是否尝试过先连接您的 modbus 实例? c.connect() 在 read_holding_registers 之前

标签: python modbus-tcp


【解决方案1】:

通过阅读the pymodbus code,看来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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 2022-07-18
    • 2022-01-23
    • 2015-03-08
    • 2022-10-26
    • 2018-12-15
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多