From eac9681e0792294cd5fa384fc8f858cda6b77dc0 Mon Sep 17 00:00:00 2001 From: Nigel Dokter Date: Sat, 21 Dec 2019 17:37:50 +0100 Subject: [PATCH 1/3] updated changelog --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index babab7c..fe4d4ea 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,10 @@ Change Log ---------- +**0.17** (2019-12-21) + +- Add a true telegram object (`pull request #40 `_). + **0.16** (2019-12-21) - Add support for Belgian and Smarty meters (`pull request #44 `_). From d714528c5a8ec8e4286176c5711398100bc2c23c Mon Sep 17 00:00:00 2001 From: Jean-Louis Dupond Date: Tue, 28 Jan 2020 17:26:45 +0100 Subject: [PATCH 2/3] Include needed PyCRC code --- dsmr_parser/parsers.py | 27 +++++++++++++++++++++++++-- setup.py | 1 - tox.ini | 1 - 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/dsmr_parser/parsers.py b/dsmr_parser/parsers.py index 4b415f6..4609287 100644 --- a/dsmr_parser/parsers.py +++ b/dsmr_parser/parsers.py @@ -1,7 +1,7 @@ import logging import re -from PyCRC.CRC16 import CRC16 +from ctypes import c_ushort from dsmr_parser.objects import MBusObject, CosemObject, Telegram from dsmr_parser.exceptions import ParseError, InvalidChecksumError @@ -79,7 +79,7 @@ class TelegramParser(object): 'incomplete. The checksum and/or content values are missing.' ) - calculated_crc = CRC16().calculate(checksum_contents.group(0)) + calculated_crc = TelegramParser.crc16(checksum_contents.group(0)) expected_crc = int(checksum_hex.group(0), base=16) if calculated_crc != expected_crc: @@ -91,6 +91,29 @@ class TelegramParser(object): ) ) + @staticmethod + def crc16(telegram): + crc16_tab = [] + + for i in range(0, 256): + crc = c_ushort(i).value + for j in range(0, 8): + if (crc & 0x0001): + crc = c_ushort(crc >> 1).value ^ 0xA001 + else: + crc = c_ushort(crc >> 1).value + crc16_tab.append(hex(crc)) + + crcValue = 0x0000 + + for c in telegram: + d = ord(c) + tmp = crcValue ^ d + rotated = c_ushort(crcValue >> 8).value + crcValue = rotated ^ int(crc16_tab[(tmp & 0x00ff)], 0) + + return crcValue + class DSMRObjectParser(object): """ diff --git a/setup.py b/setup.py index e1f0139..7c54d73 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,6 @@ setup( 'pyserial>=3,<4', 'pyserial-asyncio<1', 'pytz', - 'PyCRC>=1.2,<2' ], entry_points={ 'console_scripts': ['dsmr_console=dsmr_parser.__main__:console'] diff --git a/tox.ini b/tox.ini index 23fe214..3efa213 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,6 @@ deps= pytest-asyncio pytest-catchlog pytest-mock - PyCRC commands= py.test --cov=dsmr_parser test {posargs} pylama dsmr_parser test From dc6c35a0b6dc5a96e15d8863933125dba61bf1d2 Mon Sep 17 00:00:00 2001 From: Nigel Dokter Date: Tue, 28 Jan 2020 19:40:27 +0100 Subject: [PATCH 3/3] Updated changelog --- CHANGELOG.rst | 4 ++++ setup.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fe4d4ea..0fda0e3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,10 @@ Change Log ---------- +**0.18** (2020-01-28) + +- PyCRC replacement (`pull request #48 `_). + **0.17** (2019-12-21) - Add a true telegram object (`pull request #40 `_). diff --git a/setup.py b/setup.py index 7c54d73..78eba62 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( author='Nigel Dokter', author_email='nigel@nldr.net', url='https://github.com/ndokter/dsmr_parser', - version='0.17', + version='0.18', packages=find_packages(), install_requires=[ 'pyserial>=3,<4',