Python modbus-tk library exception handling

Thread Starter

zazas321

Joined Nov 29, 2015
892
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:
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

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

2020-11-10-114409_1920x1080_scrot.png

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?
 

Attachments

bogosort

Joined Sep 24, 2011
678
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:
I'm not a fan of Python so I don't know its capabilities, but if it's raising exceptions then you should be able to catch those exceptions. Either guard the "brittle" requests with try/catch blocks, or write your own global exception handlers so your program can gracefully handle such errors.
 

strantor

Joined Oct 3, 2010
5,632
Python:
try:   
    for Device,Quantity,Serial bla bla bla
except Exception as e:
    print("the following error occurred: ", e)
    doSomethingElse()
    # (whatever you want)
 
Top