相关文章推荐
开朗的山楂  ·  Java™ SE Development ...·  2 年前    · 
坚强的柿子  ·  Jackson2JsonMessageCon ...·  2 年前    · 
大方的柑橘  ·  充电续航 | UCL ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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)

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
    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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.