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
-
@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.– Mike C.2016年09月09日 15:55:03 +00:00Commented Sep 9, 2016 at 15:55
-
Have you ever tried to connect your modbus instance first?. c.connect() before read_holding_registersQuentin– Quentin2018年06月08日 02:04:51 +00:00Commented Jun 8, 2018 at 2:04
1 Answer 1
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