reconnect fails on ModBus RTU

363 views
Skip to first unread message

klauspf

unread,
May 28, 2015, 2:33:04 PM5/28/15
to modb...@googlegroups.com
Hi luc,
thanks a lot for this great modbus-tk. In general it works pretty good. I want to fetch realtime data on a photovoltaik converter.
I can receive data in a while loop, after the serial connection is established.
But:
When I try to reconnect again, I receive an error  "Error  Response length is invalid 0"
pi@raspberrypi ~/modbus $ sudo python tk2.py
2015-05-28 18:23:07,703 INFO    modbus_rtu.__init__     MainThread      RtuMaster /dev/ttyUSB0 is opened
2015-05-28 18:23:07,708 INFO    tk2.<module>    MainThread      connected
2015-05-28 18:23:07,712 DEBUG   modbus.execute  MainThread      -> 2-3-6-114-0-5-37-105
2015-05-28 18:23:07,768 DEBUG   modbus.execute  MainThread      <- 2-3-10-17-173-19-131-0-99-0-134-0-14-134-70
2015-05-28 18:23:07,772 INFO    tk2.<module>    MainThread      (4525, 4995, 99, 134, 14)
2015-05-28 18:23:37,813 DEBUG   modbus.execute  MainThread      -> 2-3-6-114-0-5-37-105
2015-05-28 18:23:37,869 DEBUG   modbus.execute  MainThread      <- 2-3-10-17-221-19-130-0-99-0-133-0-14-3-68
2015-05-28 18:23:37,873 INFO    tk2.<module>    MainThread      (4573, 4994, 99, 133, 14)
^CTraceback (most recent call last):
  File "tk2.py", line 20, in <module>
    time.sleep(30)
KeyboardInterrupt

pi@raspberrypi ~/modbus $ sudo python tk2.py
2015-05-28 18:24:02,459 INFO    modbus_rtu.__init__     MainThread      RtuMaster /dev/ttyUSB0 is opened
2015-05-28 18:24:02,464 INFO    tk2.<module>    MainThread      connected
2015-05-28 18:24:02,468 DEBUG   modbus.execute  MainThread      -> 2-3-6-114-0-5-37-105
2015-05-28 18:24:07,480 DEBUG   modbus.execute  MainThread      <-
Error  Response length is invalid 0

After a reboot of the node, it works again until the reconnect.

The script I use is similiar to Your example:

#!/usr/bin/env python
# -*- coding: utf_8 -*-
import sys
import serial
import logging, time
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/ttyUSB0', baudrate=19200, bytesize=8, parity='N', stopbits=1, xonxoff=0))
        master.set_timeout(5.0)
        master.set_verbose(True)
        logger.info("connected")
        while True:
                logger.info(master.execute(2, cst.READ_HOLDING_REGISTERS, 1650, 5))
                time.sleep(30)

    except modbus_tk.modbus.ModbusError, e:
        logger.error("%s- Code=%d" % (e, e.get_exception_code()))
        print "Modbus error ", e.get_exception_code()

    except Exception, e2:
        print "Error ", str(e2)

    master.close()

The node is a raspberry Pi with a USB2RS485 connector. The connector runs fine with a windows machine using windows based modbus scanner. Also a reconnect works.

My assumption is, that the serial port is not closed correctly.

Can You please give me a hint. Any ideas are more than appreciated.

Thanks a lot in advance
Klaus

Luc JEAN

unread,
May 29, 2015, 2:46:29 AM5/29/15
to modb...@googlegroups.com
Hello Klaus,

Thanks for using modbus-tk. I think this is a pyserial error. Ivan had the same problem (see https://groups.google.com/forum/#!topic/modbus-tk/Yjxi-UZDBBA ) but I don't know if he succeed to solve it. I would be interested by feedback.

Unfortunately I don't have any rtu device at the moment so it is difficult for me to investigate. Sorry for this.

Best
luc

--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

klauspf

unread,
May 29, 2015, 7:01:12 AM5/29/15
to modb...@googlegroups.com
Thanks a lot for Your fast response.
I try to contact Ivan, maybe he has already some progress.

If You want, I can give You access to the raspberry PI.

Thanks
Klaus

zjuso...@gmail.com

unread,
Aug 21, 2017, 5:04:22 PM8/21/17
to modbus-tk
Hello Klaus,

I also use the Raspberry Pi 3B(run Ubuntu Mate 16.04 operating system) with a USBtoRS232 connector and a RS232toRS485 converter to fetch realtime data on a electric energy meter which supports Modbus-RTU protocol.



I have tried to use luc's example script and your script, but the Raspberry Pi never gets a response from the meter. It always returns this:

   
    2017-08-10 19:24:34,282 INFO modbus_rtu.__init__ MainThread RtuMaster /dev/ttyUSB0 is opened
   
2017-08-10 19:24:34,283 INFO rtumaster_example.<module> MainThread connected
   
2017-08-10 19:24:34,284 DEBUG modbus.execute MainThread -> 1-3-0-0-0-49-132-30
   
2017-08-10 19:24:39,291 DEBUG modbus.execute MainThread <-

   
Traceback (most recent call last):

     
File "rtumaster_example.py", line 34, in <module>
        logger
.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 49))
     
File "build/bdist.linux-x86_64/egg/modbus_tk/utils.py", line 39, in new
    modbus_tk
.exceptions.ModbusInvalidResponseError: Response length is invalid 0


I am sure that the parameters such as port, baudrate, bytesize,parity,and stopbits were set correctly, and the serial transmission line runs fine with a Windows laptop using a Windows based debugging tool, which was developed by the manufacturer of the meter. The debugging tool send the same Request (1-3-0-0-0-49-132-30)as  before, but the debugging tool can get correct Respondes. (Maybe it's because it ignored some incorrect Respondes and keep on send Requests regularly)  And it can represent that the Request message is correct and the connection of serial transmission has no problem.




I hope to know if it is possible to ignore the exceptions and keep on send Requests regularly, when I use Modbus-tk.
Can You please give me an advice? Any ideas are more than appreciated.

Thanks a lot!

在 2015年5月29日星期五 UTC+8上午2:33:04,klauspf写道:
Reply all
Reply to author
Forward
0 new messages