From 5c02a1a845b1b2f1bcc5e4dfa72558146eb8714d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Wed, 3 Feb 2021 09:42:23 +0100 Subject: [PATCH 1/2] Pass a partially received response in the ValueError exception. Allow better debugging when an incomplete response was received or timed out. The already received part (empty on timeout) is passed as the first and only argument in the ValueError exception, accessible as e.args[0] in a handler. --- umodbus/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/umodbus/utils.py b/umodbus/utils.py index 403e504..b3803a3 100644 --- a/umodbus/utils.py +++ b/umodbus/utils.py @@ -138,6 +138,6 @@ def recv_exactly(recv_fn, size): response = b''.join(chunks) if len(response) != size: - raise ValueError + raise ValueError(response) return response From 7522a4fe882f69f2563fce8c2bf56a01aadd67ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Wed, 3 Mar 2021 12:43:47 +0100 Subject: [PATCH 2/2] Make the failed message bytes available in a CRCError exception. When a serial CRC validation fails, the exception is constructed with a descriptive text as its only argument. To aid in debugging, pass in the original failing message instead. Make sure the string representation of the exception stays the same, by adding a __str__() method like in the library's other exceptions. --- umodbus/client/serial/redundancy_check.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/umodbus/client/serial/redundancy_check.py b/umodbus/client/serial/redundancy_check.py index f228dce..e3e7a89 100644 --- a/umodbus/client/serial/redundancy_check.py +++ b/umodbus/client/serial/redundancy_check.py @@ -73,9 +73,11 @@ def validate_crc(msg): """ if not struct.unpack('