Using pymodbus to read registers
<p>I'm trying to read modbus registers from a PLC using pymodbus. I am following the example posted <a href="http://stackoverflow.com/questions/24872269/reading-registers-with-pymodbus">here</a>. When I attempt <code>print.registers</code> I get the following error: <code>object has no attribute 'registers'</code>
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. </p>

<p>Here is my code:</p>

<pre><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
</code></pre>

<p>From reading <a href="https://github.com/bashwork/pymodbus/blob/master/pymodbus/register_read_message.py" rel="nofollow" title="pymodbus read_holding_registers request object">the pymodbus code</a>, it appears that the <code>read_holding_registers</code> object's <code>execute</code> method will return <em>either</em> a response object <em>or</em> an <code>ExceptionResponse</code> object that contains an error.  I would guess you're receiving the latter.  You need to try something like this:</p>

<pre><code>from pymodbus.register_read_message import ReadHoldingRegistersResponse
#...
response = c.execute(chk)
if isinstance(response, ReadHoldingRegistersResponse):
  print response.registers
else:
  pass # handle error condition here
</code></pre>

python modbus-tcp