Python Modbus RTU over TCP

Question:

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)

Asked By: maja95

||

Answers:

Read holding register needs a unit to correctly read the message

read = client.read_holding_registers(address=4000, unit=1)
Answered By: maja95

This is what helped me.

Check slave_ID of your device (unit=slave_ID). It is not nessecary 1 or 0. In my case – 240, for example.

result = client.read_holding_registers(address=0x0010, count = 2, **unit=240**)

Documentation says:

    def write_register(self, address, value, **kwargs):
    '''

    :param address: The starting address to write to
    :param value: The value to write to the specified address
    **:param unit: The slave unit this request is targeting**
    :returns: A deferred response handle
    '''

And:

    def read_holding_registers(self, address, count=1, **kwargs):
    '''

    :param address: The starting address to read from
    :param count: The number of registers to read
    **:param unit: The slave unit this request is targeting**
    :returns: A deferred response handle
    '''

Look file "common.py" or press Ctrl+B while cursor is on function.

Answered By: immortal max