From e512456cc2acfe0df34778273da04a389e9db04c Mon Sep 17 00:00:00 2001 From: Alex Mekkering Date: Wed, 4 Jan 2017 10:21:47 +0100 Subject: [PATCH 1/2] 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: From ce4d5b0e62670c922c372eb29ad3463a84dc98ca Mon Sep 17 00:00:00 2001 From: Alex Mekkering Date: Wed, 4 Jan 2017 10:51:29 +0100 Subject: [PATCH 2/2] Corrected unit test for failing CRC --- test/test_parse_v4_2.py | 72 ++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/test/test_parse_v4_2.py b/test/test_parse_v4_2.py index 58e757e..897c5f3 100644 --- a/test/test_parse_v4_2.py +++ b/test/test_parse_v4_2.py @@ -11,43 +11,43 @@ from dsmr_parser.objects import CosemObject, MBusObject from dsmr_parser.parsers import TelegramParser, TelegramParserV4 TELEGRAM_V4_2 = [ - '/KFM5KAIFA-METER\r\n', - '\r\n', - '1-3:0.2.8(42)\r\n', - '0-0:1.0.0(161113205757W)\r\n', - '0-0:96.1.1(3960221976967177082151037881335713)\r\n', - '1-0:1.8.1(001581.123*kWh)\r\n', - '1-0:1.8.2(001435.706*kWh)\r\n', - '1-0:2.8.1(000000.000*kWh)\r\n', - '1-0:2.8.2(000000.000*kWh)\r\n', - '0-0:96.14.0(0002)\r\n', - '1-0:1.7.0(02.027*kW)\r\n', - '1-0:2.7.0(00.000*kW)\r\n', - '0-0:96.7.21(00015)\r\n', - '0-0:96.7.9(00007)\r\n', + '/KFM5KAIFA-METER', + '', + '1-3:0.2.8(42)', + '0-0:1.0.0(161113205757W)', + '0-0:96.1.1(3960221976967177082151037881335713)', + '1-0:1.8.1(001581.123*kWh)', + '1-0:1.8.2(001435.706*kWh)', + '1-0:2.8.1(000000.000*kWh)', + '1-0:2.8.2(000000.000*kWh)', + '0-0:96.14.0(0002)', + '1-0:1.7.0(02.027*kW)', + '1-0:2.7.0(00.000*kW)', + '0-0:96.7.21(00015)', + '0-0:96.7.9(00007)', '1-0:99.97.0(3)(0-0:96.7.19)(000104180320W)(0000237126*s)(000101000001W)' - '(2147583646*s)(000102000003W)(2317482647*s)\r\n', - '1-0:32.32.0(00000)\r\n', - '1-0:52.32.0(00000)\r\n', - '1-0:72.32.0(00000)\r\n', - '1-0:32.36.0(00000)\r\n', - '1-0:52.36.0(00000)\r\n', - '1-0:72.36.0(00000)\r\n', - '0-0:96.13.1()\r\n', - '0-0:96.13.0()\r\n', - '1-0:31.7.0(000*A)\r\n', - '1-0:51.7.0(006*A)\r\n', - '1-0:71.7.0(002*A)\r\n', - '1-0:21.7.0(00.170*kW)\r\n', - '1-0:22.7.0(00.000*kW)\r\n', - '1-0:41.7.0(01.247*kW)\r\n', - '1-0:42.7.0(00.000*kW)\r\n', - '1-0:61.7.0(00.209*kW)\r\n', - '1-0:62.7.0(00.000*kW)\r\n', - '0-1:24.1.0(003)\r\n', - '0-1:96.1.0(4819243993373755377509728609491464)\r\n', - '0-1:24.2.1(161129200000W)(00981.443*m3)\r\n', - '!6796\r\n' + '(2147583646*s)(000102000003W)(2317482647*s)', + '1-0:32.32.0(00000)', + '1-0:52.32.0(00000)', + '1-0:72.32.0(00000)', + '1-0:32.36.0(00000)', + '1-0:52.36.0(00000)', + '1-0:72.36.0(00000)', + '0-0:96.13.1()', + '0-0:96.13.0()', + '1-0:31.7.0(000*A)', + '1-0:51.7.0(006*A)', + '1-0:71.7.0(002*A)', + '1-0:21.7.0(00.170*kW)', + '1-0:22.7.0(00.000*kW)', + '1-0:41.7.0(01.247*kW)', + '1-0:42.7.0(00.000*kW)', + '1-0:61.7.0(00.209*kW)', + '1-0:62.7.0(00.000*kW)', + '0-1:24.1.0(003)', + '0-1:96.1.0(4819243993373755377509728609491464)', + '0-1:24.2.1(161129200000W)(00981.443*m3)', + '!6796' ]