Merge branch 'master' of github.com:ndokter/dsmr_parser

This commit is contained in:
Nigel Dokter 2020-01-28 19:36:23 +01:00
commit 47c20e763f
3 changed files with 25 additions and 4 deletions

View File

@ -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):
"""

View File

@ -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']

View File

@ -9,7 +9,6 @@ deps=
pytest-asyncio
pytest-catchlog
pytest-mock
PyCRC
commands=
py.test --cov=dsmr_parser test {posargs}
pylama dsmr_parser test