pythonmodbusmodbus-tcppymodbusmodbus-rtu-over-tcp

Python Modbus RTU over TCP


I am trying to read and write data over Modbus TCP with python. When I am using ModbusPoll with the following setup everything works.

ModbusPoll

I try to read the data now with python and I am using the pymodbus library for this. My code looks like this:

from pymodbus.client.sync import ModbusTcpClient
from pymodbus.transaction import ModbusRtuFramer as ModbusFramer

client = ModbusTcpClient("192.168.0.7", port=502, framer=ModbusFramer)
success = client.connect()

read = client.read_holding_registers(address=4000)
read.registers

But I am always getting the following error:

ModbusIOException(InvalidMessageReceivedException('No response received, expected at least 2 bytes (0 received)'), 1)


Solution

  • Read holding register needs a unit to correctly read the message

    read = client.read_holding_registers(address=4000, unit=1)