I'm currently trying to establish modbus communication between a commercial router (UR71 from Ursalink) and a Modbus RTU (ZBRN2 from Schneider Electric).
UR71 router is running on Linux OpenWRT LEDE-17.01 Python is v2.7.13 Python is using modbus-tk 0.5.7 and pyserial 3.4
My application requires modbus reads (FC03) and modbus multiple write (FC16)
The thing is that I succeed running with my computer (windows 10) as modbus master, but when i switch to UR71 as modbus master, FC16 does not work anymore and sends :
(<class 'modbus_tk.exceptions.ModbusInvalidResponseError'>, ModbusInvalidResponseError('Response length is invalid 0',), <traceback object at 0x7f7b4f7cf8>)
I made a little example app to show you:
class ModbusLoop(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
while (True):
try:
# MODBUS READ
logger.debug("Reading modbus FC03...")
result = rtu.execute(1, tkCst.READ_HOLDING_REGISTERS, 6100, 60)
logger.debug(result)
time.sleep(1)
#MODBUS WRITE SINGLE RESISTER
logger.debug("Reading modbus FC06...")
result = rtu.execute(1, tkCst.WRITE_SINGLE_REGISTER, 2010, output_value = 3841)
logger.debug(result)
result = rtu.execute(1, tkCst.WRITE_SINGLE_REGISTER, 2010, output_value = 4097)
logger.debug(result)
time.sleep(1)
logger.debug("Reading modbus FC16...")
result = rtu.execute(1, tkCst.WRITE_MULTIPLE_REGISTERS, 2010, output_value = [3841])
logger.debug(result)
result = rtu.execute(1, tkCst.WRITE_MULTIPLE_REGISTERS, 2010, output_value = [4097])
logger.debug(result)
except:
logger.error(sys.exc_info())
finally:
logger.debug("")
time.sleep(5)
Modbus is initialized like this:
rtu = ur71_modbus.UR71RTU(ur71_init.port_id, ur71_init.baudrate)
rtu.set_timeout(2.0)
rtu.set_verbose(True)
with UR71RTU containing serial conf:
ser = serial.Serial(port=self.dev, baudrate=baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, xonxoff=0)
This resulting in :
2.7.13 (default, Aug 21 2018, 20:45:19)
run.py [2018-11-30 14:00:58 PM] [INFO] Modbus initialization
ur71_modbus.py [2018-11-30 14:00:58 PM] [DEBUG] Linux
modbus_rtu.py [2018-11-30 14:00:58 PM] [INFO] RtuMaster /dev/ttyS1 is opened
run.py [2018-11-30 14:00:58 PM] [DEBUG] Reading modbus FC03...
modbus.py [2018-11-30 14:00:58 PM] [DEBUG] -> 1-3-23-212-0-60-0-87
modbus.py [2018-11-30 14:00:58 PM] [DEBUG] <- 1-3-120-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-209-7
run.py [2018-11-30 14:00:58 PM] [DEBUG] (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
run.py [2018-11-30 14:00:59 PM] [DEBUG] Reading modbus FC06...
modbus.py [2018-11-30 14:00:59 PM] [DEBUG] -> 1-6-7-218-15-1-109-117
modbus.py [2018-11-30 14:00:59 PM] [DEBUG] <- 1-6-7-218-15-1-109-117
run.py [2018-11-30 14:00:59 PM] [DEBUG] (2010, 3841)
modbus.py [2018-11-30 14:00:59 PM] [DEBUG] -> 1-6-7-218-16-1-101-69
modbus.py [2018-11-30 14:00:59 PM] [DEBUG] <- 1-6-7-218-16-1-101-69
run.py [2018-11-30 14:00:59 PM] [DEBUG] (2010, 4097)
run.py [2018-11-30 14:01:00 PM] [DEBUG] Reading modbus FC16...
modbus.py [2018-11-30 14:01:00 PM] [DEBUG] -> 1-16-7-218-0-1-2-15-1-7-154
modbus.py [2018-11-30 14:01:02 PM] [DEBUG] <-
run.py [2018-11-30 14:01:02 PM] [ERROR] (<class 'modbus_tk.exceptions.ModbusInvalidResponseError'>, ModbusInvalidResponseError('Response length is invalid 0',), <traceback object at 0x7f7b4f7cf8>)
run.py [2018-11-30 14:01:02 PM] [DEBUG]
Same code in Windows results in :
2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
INFO:modbus_tk:RtuMaster COM4 is opened
DEBUG:ur71:Serial link checked
DEBUG:ur71:Reading modbus FC03...
DEBUG:modbus_tk:-> 1-3-23-212-0-60-0-87
DEBUG:modbus_tk:<- 1-3-120-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-209-7
DEBUG:ur71:(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
DEBUG:ur71:Reading modbus FC06...
DEBUG:modbus_tk:-> 1-6-7-218-15-1-109-117
DEBUG:modbus_tk:<- 1-6-7-218-15-1-109-117
DEBUG:ur71:(2010, 3841)
DEBUG:modbus_tk:-> 1-6-7-218-16-1-101-69
DEBUG:modbus_tk:<- 1-6-7-218-16-1-101-69
DEBUG:ur71:(2010, 4097)
DEBUG:ur71:Reading modbus FC16...
DEBUG:modbus_tk:-> 1-16-7-218-0-1-2-15-1-7-154
DEBUG:modbus_tk:<- 1-16-7-218-0-1-33-70
DEBUG:ur71:(2010, 1)
DEBUG:modbus_tk:-> 1-16-7-218-0-1-2-16-1-15-170
DEBUG:modbus_tk:<- 1-16-7-218-0-1-33-70
DEBUG:ur71:(2010, 1)
DEBUG:ur71:
I cannot figure out what would cause the issue.
- I investigated timeouts (up to 10s), inter_byte_timeouts and so, but if it works for FC06, why should it be different for FC16?
- I tested with minimalmodbus lib and the issue is the same, so i don't think the error comes from modbus-tk lib
I'm running out of ideas here, and would appreciate some help if someone already encountered this kind of issues. Maybe there are some differences of serial port on Linux & Windows that I don't now about (but again why FC06 work?)?
Best regards