【问题标题】:Cannot read register values using pymodbus无法使用 pymodbus 读取寄存器值
【发布时间】:2020-12-17 16:37:31
【问题描述】:

当我尝试以下代码时,我可以连接到智能电表,但我无法读取寄存器内容。

import traceback
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.2.1', port=502)
client.connect()

try:
    res = client.read_holding_registers(0x0063, 1, unit=1)
    print(res.registers)

except:
    traceback.print_exc()
    
client.close()

给出一个例外

  Traceback <Most recent call last>
    File "m2.py", Line 8 ,in <module>
    print<res.registers>
AttributeErro: 'ModbusIOException' objecthas noattribute 'registers'    

请帮我解决这个问题

【问题讨论】:

  • 更多信息会有所帮助。有例外吗?它是什么?你怎么知道你连接成功了?

标签: python pymodbus smartmeter


【解决方案1】:

您可能在这里错过了client.connect

from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient(host='192.168.2.1', port=502)
client.connect() ### Added here

try:
   result = client.read_holding_registers(0x0063,1, unit=1)
   print (result.registers)

except Exception as e:
   print (e)

client.close()

【讨论】:

  • 感谢您的回复,但是当我尝试您的解决方案时,它给出了类似 ModusIoException ' Object has no attribute registers' 之类的错误如果您提出解决方案,那就太好了
  • @VijuPoonthottam - 你能发布StackTrace 并更新问题
  • @Vaebhav 编辑和发布
猜你喜欢
  • 1970-01-01
  • 2021-12-21
  • 2022-07-18
  • 2018-12-15
  • 2021-06-22
  • 2022-01-23
  • 2015-03-08
  • 2022-10-26
  • 2023-03-31
相关资源
最近更新 更多