pythonmodbusmodbus-tcppymodbus

modbus controller: Trying to get data from Modbus controller using IP address in Python


I am trying to get data from Modbus controller using IP address.

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

client = ModbusClient("10.98.237.80", port=502, auto_open=True)
client.connect()

rr = client.read_holding_registers(60, 2, unit=1)

print (rr.registers)

This gives the below exception

2023-04-03 14:57:18,678 MainThread      DEBUG    sync           :216      Connection to Modbus server established. Socket ('10.80.144.99', 64492)
2023-04-03 14:57:18,680 MainThread      DEBUG    transaction    :140      Current transaction state - IDLE
2023-04-03 14:57:18,681 MainThread      DEBUG    transaction    :145      Running transaction 1
2023-04-03 14:57:18,682 MainThread      DEBUG    transaction    :273      SEND: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x3 0x0 0x3c 0x0 0x2
2023-04-03 14:57:18,683 MainThread      DEBUG    sync           :76       New Transaction state 'SENDING'
2023-04-03 14:57:18,684 MainThread      DEBUG    transaction    :287      Changing transaction state from 'SENDING' to 'WAITING FOR REPLY'
2023-04-03 14:57:20,082 MainThread      DEBUG    transaction    :375      Changing transaction state from 'WAITING FOR REPLY' to 'PROCESSING REPLY'
2023-04-03 14:57:20,083 MainThread      DEBUG    transaction    :297      RECV: 0x0 0x1 0x0 0x0 0x0 0x3 0x1 0x83 0xfc
2023-04-03 14:57:20,084 MainThread      DEBUG    socket_framer  :147      Processing: 0x0 0x1 0x0 0x0 0x0 0x3 0x1 0x83 0xfc
2023-04-03 14:57:20,084 MainThread      DEBUG    factory        :266      Factory Response[131]
2023-04-03 14:57:20,085 MainThread      DEBUG    transaction    :454      Adding transaction 1
2023-04-03 14:57:20,085 MainThread      DEBUG    transaction    :465      Getting transaction 1
2023-04-03 14:57:20,086 MainThread      DEBUG    transaction    :224      Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-1ea661f8edc1> in <module>
      6 rr = client.read_holding_registers(60, 2, unit=1)
      7 
----> 8 print (rr.registers)

AttributeError: 'ExceptionResponse' object has no attribute 'registers'

I am completely new to this Modbus and modbus server/client. Please help. any help is much appreciated.


Solution

  • Firstly if your Modbus Server is connected via ethernet there should be no need to specify the Unit ID as it's used to identify individual PLCs that communicate in Modbus-RTU or Modbus+ whereas Modbus TCP uses the IP address so simply having rr = client.read_holding_registers(60, 2) should be enough.

    The message 2023-04-03 14:57:20,084 MainThread DEBUG factory :266 Factory Response[131] indicates that there was an error reading the holding registers from the server this can be deduced by simply adding up the function code number (3 in this case for reading holding registers) and 128. Make sure that the register 59 (%MW59) is defined in the modbus server. (Keep in mind that the first register is addressed as register 0.)