From 81cd5f6e62bf8ba065158638e4afe2fc6e4440a3 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Tue, 14 Feb 2023 19:38:35 -0800 Subject: [PATCH] Cancel running program prior to attempting to run teardown executers --- belay/device.py | 2 ++ belay/pyboard.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/belay/device.py b/belay/device.py index fce5f63..1c4a411 100644 --- a/belay/device.py +++ b/belay/device.py @@ -652,6 +652,8 @@ def close(self) -> None: # Invoke all teardown executers prior to closing out connection. atexit.unregister(self.close) + self._board.cancel_running_program() + for executer in _sort_executers(self._belay_teardown._belay_executers): executer() diff --git a/belay/pyboard.py b/belay/pyboard.py index 478045f..2659b19 100644 --- a/belay/pyboard.py +++ b/belay/pyboard.py @@ -402,13 +402,17 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None): time.sleep(0.01) return data + def cancel_running_program(self): + """Interrupts any running program.""" + self.serial.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program + def enter_raw_repl(self, soft_reset=True): # flush input (without relying on serial.flushInput()) n = self.serial.inWaiting() while n > 0: self.serial.read(n) n = self.serial.inWaiting() - self.serial.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program + self.cancel_running_program() self.exit_raw_repl() # if device is already in raw_repl, b'>>>' won't be printed. self.read_until(1, b">>>") self.serial.write(b"\r\x01") # ctrl-A: enter raw REPL