Modbus_tk ERROR RTU

875 views
Skip to first unread message

Ionut Dinu

unread,
Feb 2, 2015, 3:28:03 AM2/2/15
to modb...@googlegroups.com
Hi,
     I used Modbus_tk library to acquire data from a server, via RTU. I'll put on this post my pyscript code and my error.

ERROR:

2015-02-02 08:27:12,744 INFO    modbus_rtu.__init__     MainThread      RtuMaster /dev/ttyO1 is opened
2015-02-02 08:27:12,748 INFO    test_rtu_modbus_tk.<module>     MainThread      connected
2015-02-02 08:27:12,751 DEBUG   modbus.execute  MainThread      -> 5-3-197-88-0-22-120-159
2015-02-02 08:27:17,766 DEBUG   modbus.execute  MainThread      <-
Traceback (most recent call last):
  File "test_rtu_modbus_tk.py", line 32, in <module>
    logger.info(master.execute(5, cst.READ_HOLDING_REGISTERS, 50520, 22))
  File "/usr/lib/python2.7/modbus_tk/utils.py", line 26, in new
    raise excpt
modbus_tk.modbus.ModbusInvalidResponseError: Response length is invalid 0


CODE:

import sys
import serial

#add logging capability
import logging

import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_rtu as modbus_rtu

logger = modbus_tk.utils.create_logger("console")

if __name__ == "__main__":
    try:
        #Connect to the slave
        master = modbus_rtu.RtuMaster(serial.Serial(port="/dev/ttyO1", baudrate=19200, bytesize=8, parity='N', stopbits=1, xonxoff=0))
        master.set_timeout(5.0)
        master.set_verbose(True)
        logger.info("connected")
      
        logger.info(master.execute(5, cst.READ_HOLDING_REGISTERS, 50520, 22))
      
      
    except modbus_tk.modbus.ModbusError, e:
        logger.error("%s- Code=%d" % (e, e.get_exception_code()))

klauspf

unread,
Aug 17, 2015, 11:57:06 AM8/17/15
to modbus-tk
Hi,
I know, the question is rather old, but maybe it will help someone else!
The Holding Registers are normally starting with 4xxxx and not like in Your example 50520.
And You don't have to write the 4 in the beginning.
So if You want to read the register 40520, the line should look like this:
 logger.info(master.execute(5, cst.READ_HOLDING_REGISTERS, 520, 22))

Hope this helps.

Klaus
Reply all
Reply to author
Forward
0 new messages