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