0

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

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'    

请帮我解决这个问题

4

1 回答 1

0

你可能错过了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()
于 2020-12-19T05:19:59.593 回答