usb connection improved

This commit is contained in:
Ayzen
2026-04-27 16:28:48 +03:00
parent 0ec504ffa9
commit 6b6689fa5f
3 changed files with 148 additions and 19 deletions

View File

@ -427,8 +427,8 @@ class Protocol:
)
words = _unpack_words(data)
expected_crc = _payload_checksum(list(words[1:14]))
if words[14] != expected_crc:
if not Protocol.has_valid_response_crc(data):
expected_crc = _payload_checksum(list(words[1:14]))
raise CRCError(expected=expected_crc, received=words[14])
return Measurements(
@ -448,6 +448,15 @@ class Protocol:
timestamp=datetime.now(),
)
@staticmethod
def has_valid_response_crc(data: bytes) -> bool:
"""Return True when a telemetry frame has the expected length and CRC."""
if len(data) != GET_DATA_TOTAL_LENGTH:
return False
words = _unpack_words(data)
expected_crc = _payload_checksum(list(words[1:14]))
return words[14] == expected_crc
@staticmethod
def decode_status(data: bytes) -> tuple[DeviceState, int]:
"""Decode the two-byte firmware status response into flags and detail."""