0

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 Aug 12, 2022 at 6:58

2 Answers 2

2

Read holding register needs a unit to correctly read the message

read = client.read_holding_registers(address=4000, unit=1)
Chris
139k139 gold badges317 silver badges293 bronze badges
answered Aug 12, 2022 at 10:06
Sign up to request clarification or add additional context in comments.

Comments

1

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 Aug 16, 2022 at 15:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.