0

I'm trying to read modbus registers from a PLC using pymodbus. 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. 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
Community
  • 1
  • 1
Mike C.
  • 1,761
  • 2
  • 22
  • 46
  • @J Earls The error is gone but not reading registers. Modbus Poll reads fine. Do you see anything else wrong with the code? – Mike C. Sep 09 '16 at 15:55
  • Have you ever tried to connect your modbus instance first?. c.connect() before read_holding_registers – Quentin Jun 08 '18 at 02:04

1 Answers1

2

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. 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
J Earls
  • 1,792
  • 8
  • 12