0

I know this may seem like a duplicate question but the answers provided didn't solve the issue I am having. I am writing a program to read in analog channels from an ADAM 6017 using pymodbus. To start I am just asking for the first holding register 40000 or as I have it register 0, I can get a reading using Simply Modbus but when I run my code in Python all I get back is Exception Response(131, 3, IllegalAddress)

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient("192.168.1.201", port=502, auto_open=True)

rr = client.read_holding_registers(0, 1, unit=0x00)
raw_value = client.read_holding_registers(0, 1, unit=0x00)
rr_response = client.execute(rr)
raw_value_response = client.execute(raw_value)
print(raw_value_response)
print (rr_response)

Michael H.
  • 93
  • 3
  • 13

1 Answers1

2

This is what I get when I run your code with my dummy Modbus server:

DEBUG:pymodbus.server.sync:Client Disconnected [127.0.0.1:33075]
DEBUG:pymodbus.server.sync:Started thread to serve client at ('127.0.0.1', 49439)
DEBUG:pymodbus.server.sync:Client Connected [127.0.0.1:49439]
DEBUG:pymodbus.server.sync:Handling data: 0x0 0x4 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0xff
DEBUG:pymodbus.framer.socket_framer:Processing: 0x0 0x4 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0xff
DEBUG:pymodbus.factory:Factory Request[ReadHoldingRegistersRequest: 3]
ERROR:pymodbus.server.sync:Socket exception occurred Traceback (most recent call /register_read_message.py", line 40, in decode
    self.address, self.count = struct.unpack('>HH', data)
error: unpack requires a string argument of length 4

DEBUG:pymodbus.server.sync:Client Disconnected [127.0.0.1:49439]

So you have a problem there. To make it work I just changed it to:

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient("192.168.1.201", port=502, auto_open=True)
client.connect()
rr = client.read_holding_registers(0, 1, unit=0x00)

print (rr.registers)

And I got a list of 1 element with the correct value of the register on my server.

You can take a look at the sync client example of pymodbus for more details: https://pymodbus.readthedocs.io/en/v1.3.2/examples/synchronous-client.html

Marcos G.
  • 3,371
  • 2
  • 8
  • 16
  • Thank you, I tried it with the client.connect() line when I first started but I noticed more and more people were dropping that so I dropped it. Its working now with those changes you suggested. – Michael H. May 22 '19 at 13:23
  • 1
    Great! You're right on the connect, it seems to be optional, at least for TCP – Marcos G. May 22 '19 at 14:16
  • @MichaelH. There is not `auto_open=True` argument in `ModbusTcpClient` in [pymodbus](https://pypi.org/project/pymodbus/) library, I think you mistake it with [pyModbusTcp](https://pypi.org/project/pyModbusTCP/) library. – Benyamin Jafari May 26 '19 at 07:47