diff --git a/src/openmv/camera.py b/src/openmv/camera.py index fd1aa20..414fb19 100644 --- a/src/openmv/camera.py +++ b/src/openmv/camera.py @@ -7,6 +7,7 @@ # This module provides the high-level Camera class that handles all # camera operations and channel communications using the OpenMV Protocol. +import time import struct import sys import serial @@ -329,15 +330,24 @@ def update_capabilities(self): self._send_cmd_wait_resp(Opcode.PROTO_SET_CAPS, 0, payload) @retry_if_failed - def stop(self): - """Stop running script""" + def stop(self, timeout=None): + """Stop running script and wait for it to exit""" # Stop running script stdin_id = self.get_channel(name="stdin") self._channel_ioctl(stdin_id, ChannelIOCTL.STDIN_STOP) + # Wait for the script to exit + timeout = self.timeout if timeout is None else timeout + start_time = time.time() + while self.read_status()["stdin"]: + if time.time() - start_time > timeout: + raise TimeoutException("Timeout waiting for the script to stop") @retry_if_failed def exec(self, script): """Write and execute a script""" + # Stop the running script first + if self.read_status()["stdin"]: + self.stop() stdin_id = self.get_channel(name="stdin") # Reset script buffer self._channel_ioctl(stdin_id, ChannelIOCTL.STDIN_RESET) diff --git a/src/openmv/transport.py b/src/openmv/transport.py index c0121f5..6f72202 100644 --- a/src/openmv/transport.py +++ b/src/openmv/transport.py @@ -215,7 +215,6 @@ def recv_packet(self, poll_events=False): if packet['flags'] & Flags.EVENT: self.event_callback(packet['channel'], 0xFFFF if not packet['length'] else struct.unpack(' self.max_payload: self.log(seq, chan, opcode, flags, length, "Rjct") self.buf.consume(1) - elif not self._check_seq(seq, self.sequence, opcode, flags): + elif not self._check_crc(crc, header[:Protocol.HEADER_SIZE - 2], 16): self.log(seq, chan, opcode, flags, length, "Rjct") self.buf.consume(1) - elif not self._check_crc(crc, header[:Protocol.HEADER_SIZE - 2], 16): + elif not self._check_seq(seq, self.sequence, opcode, flags): self.log(seq, chan, opcode, flags, length, "Rjct") self.buf.consume(1) + raise SequenceException("") else: self.state = State.PAYLOAD self.plength = Protocol.HEADER_SIZE + length