Skip to content

Commit

Permalink
chore: Improve error messages
Browse files Browse the repository at this point in the history
Closes #76
  • Loading branch information
gavinying committed Dec 17, 2024
1 parent d58ed36 commit 505c76c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions modpoll/modbus_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 505c76c

Please sign in to comment.