Search code examples
pythonmodbusmodbus-tcppymodbus

Exception Responce (131, 3, IllegalAddress)


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)


Solution

  • 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