diff --git a/dsmr_parser/serial.py b/dsmr_parser/serial.py index f9276f7..fbe51e6 100644 --- a/dsmr_parser/serial.py +++ b/dsmr_parser/serial.py @@ -1,8 +1,15 @@ -import serial import asyncio +import logging + +import serial + import serial_asyncio +from dsmr_parser.exceptions import ParseError from dsmr_parser.parsers import TelegramParser, TelegramParserV2_2 +logger = logging.getLogger(__name__) + + SERIAL_SETTINGS_V2_2 = { 'baudrate': 9600, 'bytesize': serial.SEVENBITS, @@ -44,7 +51,6 @@ SERIAL_SETTINGS_V4_EVEN = { } - def is_start_of_telegram(line): return line.startswith('/') @@ -129,6 +135,11 @@ class AsyncSerialReader(SerialReader): telegram.append(line) if is_end_of_telegram(line): - # push new parsed telegram onto queue - queue.put_nowait(self.telegram_parser.parse(telegram)) + try: + parsed_telegram = self.telegram_parser.parse(telegram) + # push new parsed telegram onto queue + queue.put_nowait(parsed_telegram) + except ParseError: + logger.exception("failed to parse telegram") + telegram = [] diff --git a/test/test_parse_v4_2.py b/test/test_parse_v4_2.py index 14730a9..36b1402 100644 --- a/test/test_parse_v4_2.py +++ b/test/test_parse_v4_2.py @@ -1,13 +1,13 @@ """Test parsing of a DSMR v4.2 telegram.""" import datetime - from decimal import Decimal + import pytz +from dsmr_parser import obis_references as obis +from dsmr_parser import telegram_specifications from dsmr_parser.objects import CosemObject, MBusObject from dsmr_parser.parsers import TelegramParser -from dsmr_parser import telegram_specifications -from dsmr_parser import obis_references as obis TELEGRAM_V4_2 = [ '1-3:0.2.8(42)', @@ -22,7 +22,8 @@ TELEGRAM_V4_2 = [ '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)(000103180420W)(0000237126*s)(000101000001W)(2147483647*s)(000101000001W)(2147483647*s)', + ('1-0:99.97.0(3)(0-0:96.7.19)(000103180420W)(0000237126*s)' + '(000101000001W)(2147483647*s)(000101000001W)(2147483647*s)'), '1-0:32.32.0(00000)', '1-0:52.32.0(00000)', '1-0:72.32.0(00000)',