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