Python Forum

Full Version: Modbus-tk library catching modbus-tk exception
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey. I am working with raspberry PI and modbus. Raspberry PI is configured as master and I have multiple ESP32 devices configured as slave devices.

I am not entirely sure how modbus-tk library works but everytime I write to register or read from slaves, the raspberry PI is waiting for a valid response. If the response is invalid ( slave did not respond or its turned OFF), the python will throw me an error code:
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/usr/lib/python3.7/tkinter/__init__.py", line 749, in callit
    func(*args)
  File "/home/pi/Desktop/lukas_programming/PTL_python/PTL_Modbus2.py", line 464, in <lambda>
    master.after(100,lambda:update_current_operation(myConnection))
  File "/home/pi/Desktop/lukas_programming/PTL_python/PTL_Modbus2.py", line 508, in update_current_operation
    master_modbus.execute(int(Device), cst.WRITE_SINGLE_REGISTER, 24, output_value=Quantity)
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/utils.py", line 39, in new
    raise excpt
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/utils.py", line 37, in new
    ret = fcn(*args, **kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/modbus.py", line 306, in execute
    response_pdu = query.parse_response(response)
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/modbus_rtu.py", line 46, in parse_response
    raise ModbusInvalidResponseError("Response length is invalid {0}".format(len(response)))
modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0
https://ibb.co/TM1Vp5f

My question is to how to properly handle this invalid response? For example If I want to do some task which involve 2 ESP32 slave devices I would use this command to initiate a task:
for Device,Quantity,Serial in cur.fetchall() :
        device_list.append(Device)
        Priority = Assign_priorities(Serial)
        item.append([Device, Quantity, Serial, Priority])
        counter=counter+1
        master_modbus.execute(int(Device), cst.WRITE_SINGLE_REGISTER, 24, output_value=Quantity)
This will write to register 24 for both devices, if both devices are working properly, everything will be fine, however,if one of the devices are OFF or not working, I need to be able to know and handle it appropriately(notify user to turn ON the required device for example or something)



Also, same thing happens when I send "restart" command to any of my ESP32 slaves. When ESP32 receives the command, it restarts but I get the same error message on my raspberry PI ( My guess is that ESP32 device did not respond to the valid raspberry PI message before restarting).

Can someone suggest me a way to handle that?
What you're showing is python 2.7 code (obsolete as of Jan 1, 2020).
If you show full code listing, or let us know package name, it's possible that a newer version can be located.
It may also be easily modified to run on python 3, but still need full code for that to happen.