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

View file

@ -305,7 +305,11 @@ def readMemory(fh: RawIOBase, address: int, words: int = 1) -> Optional[bytes]:
data = fh.read(size) data = fh.read(size)
_crc = fh.read(2) _crc = fh.read(2)
if data and _crc: 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])) calculated_crc = modbus(bytes([tag, operation, size, *data]))
if crc == calculated_crc: if crc == calculated_crc:
return data return data