Handle CRC too short in readMemory

This commit is contained in:
Odd Stråbø 2022-01-26 21:01:55 +01:00
parent 00fb5f1dee
commit efddfd60cb
1 changed files with 5 additions and 1 deletions

View File

@ -305,7 +305,11 @@ def readMemory(fh: RawIOBase, address: int, words: int = 1) -> Optional[bytes]:
data = fh.read(size)
_crc = fh.read(2)
if data and _crc:
crc = struct.unpack_from("<H", _crc)[0]
try:
crc = struct.unpack_from("<H", _crc)[0]
except struct.error:
log(f"readMemory: CRC error; read {len(_crc)} bytes (2 expected)")
return None
calculated_crc = modbus(bytes([tag, operation, size, *data]))
if crc == calculated_crc:
return data