0

When I tried the following code, I can connect to the smart meter, but I'm not able to read the register content.

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()

The gives an exception

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

Please help me to fix this

  • 2
    More information would help. Is there an exception? What is it? How do you know that you're connecting successfully? – Caleb Dec 17 '20 at 17:47

1 Answers1

0

You might be missing the client.connect here

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()
Vaebhav
  • 1,471
  • 5
  • 21