Python Forum
Modbus-tk library catching modbus-tk exception
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modbus-tk library catching modbus-tk exception
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/", line 1705, in __call__
    return self.func(*args)
  File "/usr/lib/python3.7/tkinter/", line 749, in callit
  File "/home/pi/Desktop/lukas_programming/PTL_python/", line 464, in <lambda>
  File "/home/pi/Desktop/lukas_programming/PTL_python/", 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/", line 39, in new
    raise excpt
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/", line 37, in new
    ret = fcn(*args, **kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/", line 306, in execute
    response_pdu = query.parse_response(response)
  File "/home/pi/.local/lib/python3.7/site-packages/modbus_tk/", 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:
for Device,Quantity,Serial in cur.fetchall() :
        Priority = Assign_priorities(Serial)
        item.append([Device, Quantity, Serial, Priority])
        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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Catching a crash within a library code ebolisa 9 650 Nov-22-2021, 11:02 AM
Last Post: bowlofred
  Catching Errors Alienspecimen 1 1,431 May-18-2019, 11:07 PM
Last Post: Larz60+
  Pexpect not catching embeded ssh response luchoArg32 0 1,405 Feb-08-2019, 08:45 AM
Last Post: luchoArg32
  catching / handle index out of range 3Pinter 3 1,579 Feb-04-2019, 01:19 PM
Last Post: buran
  Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 1,765 Feb-02-2019, 10:15 AM
Last Post: Larz60+
  During handling of the above exception, another exception occurred Skaperen 7 20,690 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  Segmentation fault when connecting to modbus device with Libmodbus alice 0 1,422 Dec-18-2018, 04:03 PM
Last Post: alice
  win32com Events not catching dageci 0 2,410 Aug-06-2018, 03:18 PM
Last Post: dageci
  PyInstaller, how to create library folder instead of file ? harun2525 2 3,270 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020