How to Catch ModbusIOException #2106
-
I'm just curious on how to handle one exception I see popup. My setup has a TCP converter connected to a serial device. Sometimes the serial device will be down but I'll still be able to connect to the converter. This seems to propagate a few exceptions. One is the ModbusException which happens when I try to do things like read_holding_registers. That part works great and as expected. But is also seems to propagate a ModbusIOException every so often but not every call. It looks like this:
The trouble I'm having is that I don't know where this is being propagated. Generically catching all exceptions on read_holding_registers doesn't catch this. Any idea where I can capture this to control the output? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 20 replies
-
That depends a lot on whether you are using async or sync, an the version you use. |
Beta Was this translation helpful? Give feedback.
-
You need a try/except ModbusIOException, around your read call to capture the exception, please see our examples. |
Beta Was this translation helpful? Give feedback.
-
The example works !! I modified the examples:
2024-03-16 23:42:23,857 DEBUG logging:103 Connecting to 127.0.0.1:5020.
|
Beta Was this translation helpful? Give feedback.
-
Just tested again, the client call is:
and the response I get:
so the exception can be catched (we actually reraise the original exception). |
Beta Was this translation helpful? Give feedback.
Well we need the exception in order to bypass an unknown call stack.
Logger and stack traces writes to stderr, so you can catch it there.