Add error handling to async serial.

This commit is contained in:
Johan Bloemberg 2016-11-21 13:59:48 +01:00
parent 927a4bc8e7
commit 616db8b1cc
No known key found for this signature in database
GPG Key ID: B1AE6948ED49BB55

View File

@ -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):
try:
parsed_telegram = self.telegram_parser.parse(telegram)
# push new parsed telegram onto queue
queue.put_nowait(self.telegram_parser.parse(telegram))
queue.put_nowait(parsed_telegram)
except ParseError:
logger.exception("failed to parse telegram")
telegram = []