From 616db8b1cc1013220222d8130c129109f7fb96f5 Mon Sep 17 00:00:00 2001 From: Johan Bloemberg Date: Mon, 21 Nov 2016 13:59:48 +0100 Subject: [PATCH] Add error handling to async serial. --- dsmr_parser/serial.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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 = []