Fixed CRC calculation

This commit is contained in:
Alex Mekkering 2017-01-04 10:21:47 +01:00
parent 6dec45ae2c
commit e512456cc2

View File

@ -61,22 +61,19 @@ class TelegramParserV4(TelegramParser):
:raises InvalidChecksumError: :raises InvalidChecksumError:
""" """
full_telegram = ''.join(line_values) full_telegram = '\r\n'.join(line_values)
# Extract the bytes that count towards the checksum. # Extract the bytes that count towards the checksum.
checksum_contents = re.search(r'\/.+\!', full_telegram, re.DOTALL) contents = re.search(r'(\/.+\!)([0-9A-Z]{4})', full_telegram, re.DOTALL)
# Extract the hexadecimal checksum value itself. if not contents:
checksum_hex = re.search(r'((?<=\!)[0-9A-Z]{4}(?=\r\n))+', full_telegram)
if not checksum_contents or not checksum_hex:
raise ParseError( raise ParseError(
'Failed to perform CRC validation because the telegram is ' 'Failed to perform CRC validation because the telegram is '
'incomplete. The checksum and/or content values are missing.' 'incomplete. The checksum and/or content values are missing.'
) )
calculated_crc = CRC16().calculate(checksum_contents.group(0)) calculated_crc = CRC16().calculate(contents.group(1))
expected_crc = checksum_hex.group(0) expected_crc = contents.group(2)
expected_crc = int(expected_crc, base=16) expected_crc = int(expected_crc, base=16)
if calculated_crc != expected_crc: if calculated_crc != expected_crc: