From 8bd8dfa8da6aa9e6b05047790a44b25f7361af5a Mon Sep 17 00:00:00 2001 From: Andreas Berthoud Date: Sun, 15 Aug 2021 18:24:45 +0200 Subject: [PATCH] backend: Fix handling of invalid stop byte error --- backend/monsun_backend/command_execution.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/monsun_backend/command_execution.py b/backend/monsun_backend/command_execution.py index d301009..0e73fa4 100644 --- a/backend/monsun_backend/command_execution.py +++ b/backend/monsun_backend/command_execution.py @@ -256,6 +256,10 @@ class CommandBytesReadInsufficient(CommandInterpretationError): """Raised in case the command could not be interpreted""" +class InvalidStopByteError(CommandInterpretationError): + """Raised in case of an invalid stop byte.""" + + class CommandInterpreter: header_size = 4 @@ -312,7 +316,7 @@ class CommandInterpreter: if stop_byte != 0xFF: self._logger.error("Invalid stop byte") - raise CommandInterpretationError() + raise InvalidStopByteError() try: return bytes_read[self.header_size + self.data_length + 1 :] @@ -354,8 +358,11 @@ class SerialReceiver: bytes_read=self._bytes_unread, ), ) - # except CommandBytesReadInsufficient: - # return commands_received, responses_received + except InvalidStopByteError: + # drop bytes until after the next stop byte or buffer is empty + while self._bytes_unread: + if self._bytes_unread.pop(0) == 0xFF: + continue except CommandInterpretationError: return commands_received, responses_received