From 505c76c3b58ff6bacf69f4c81333f16c78febe4a Mon Sep 17 00:00:00 2001 From: Gavin Ying Date: Tue, 17 Dec 2024 15:50:20 +0800 Subject: [PATCH] chore: Improve error messages Closes #76 --- modpoll/modbus_task.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/modpoll/modbus_task.py b/modpoll/modbus_task.py index 37d4de2..ccb797a 100644 --- a/modpoll/modbus_task.py +++ b/modpoll/modbus_task.py @@ -58,6 +58,7 @@ def __init__( self.readableReferences: List[Reference] = [] self.disabled = False self.failcounter = 0 + self.logger = logging.getLogger(__name__) def poll(self, master) -> bool: if self.disabled or not master: @@ -104,9 +105,18 @@ def poll(self, master) -> bool: cur_ref += 1 if cur_ref >= self.start_address + ref_count: break - self._decode_and_update_reference(ref, decoder) + try: + self._decode_and_update_reference(ref, decoder) + self.device.update_reference(ref) + except UnicodeDecodeError: + self.logger.error( + f"Failed to decode unicode string for reference: {ref.name}, check the reference address or length of string in configuration file" + ) + except: + self.logger.error( + f"Failed to decode value for reference: {ref.name}" + ) cur_ref += ref.ref_width - self.device.update_reference(ref) self.update_statistics(True) return True except ModbusException: @@ -171,8 +181,6 @@ def _decode_and_update_reference( ref.update_value( decoder.decode_string(ref.ref_width * 2).decode("utf-8").rstrip("\x00") ) - else: - decoder.skip_bytes(2) # Skip unknown types def add_readable_reference(self, ref: "Reference"): if ref not in self.readableReferences: