From e512456cc2acfe0df34778273da04a389e9db04c Mon Sep 17 00:00:00 2001 From: Alex Mekkering Date: Wed, 4 Jan 2017 10:21:47 +0100 Subject: [PATCH] Fixed CRC calculation --- dsmr_parser/parsers.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/dsmr_parser/parsers.py b/dsmr_parser/parsers.py index cf91e2c..431b61c 100644 --- a/dsmr_parser/parsers.py +++ b/dsmr_parser/parsers.py @@ -61,22 +61,19 @@ class TelegramParserV4(TelegramParser): :raises InvalidChecksumError: """ - full_telegram = ''.join(line_values) + full_telegram = '\r\n'.join(line_values) # 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. - checksum_hex = re.search(r'((?<=\!)[0-9A-Z]{4}(?=\r\n))+', full_telegram) - - if not checksum_contents or not checksum_hex: + if not contents: raise ParseError( 'Failed to perform CRC validation because the telegram is ' 'incomplete. The checksum and/or content values are missing.' ) - calculated_crc = CRC16().calculate(checksum_contents.group(0)) - expected_crc = checksum_hex.group(0) + calculated_crc = CRC16().calculate(contents.group(1)) + expected_crc = contents.group(2) expected_crc = int(expected_crc, base=16) if calculated_crc != expected_crc: