Skip to content
Snippets Groups Projects
Commit 14ae357c authored by Daniele Nicolodi's avatar Daniele Nicolodi
Browse files

pfeifferRS485: Move checksum validation to beginning of frame decoding

This catches invalid packets early and allows to report a better error
message than when the decoding fails somewhere unexpected.
parent 9a9ede45
No related branches found
No related tags found
No related merge requests found
......@@ -138,6 +138,9 @@ class PfeifferRS485:
def unpack(frame):
if len(frame) < 10:
raise ValueError(f'unexpected frame lenght: {frame!r}')
checksum = int(frame[-3:])
if sum(frame[:-3]) % 256 != checksum:
raise ValueError(f'checksum error: {frame!r}')
address = int(frame[0:3])
op = int(frame[3:4])
parameter = int(frame[5:8])
......@@ -145,9 +148,6 @@ class PfeifferRS485:
if len(frame) != 13 + length:
raise ValueError(f'unexpected frame lenght: {frame!r}')
data = frame[10:10 + length]
checksum = int(frame[-3:])
if sum(frame[:-3]) % 256 != checksum:
raise ValueError(f'checksum error: {frame!r}')
return address, op, parameter, data
def recv(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment