From fa7ce8ac32ab3b17fffa0276c72beebc1c311535 Mon Sep 17 00:00:00 2001 From: BrianPugh Date: Sun, 29 Jan 2023 16:25:41 -0800 Subject: [PATCH 1/3] exit_raw_repl() on close. Also atexit.register so that if the user forgets to close the device, hopefully subsequent scripts run fine. Fixes broken rshell after running belay applications on windows. --- belay/pyboard.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/belay/pyboard.py b/belay/pyboard.py index 40ac116..c3d03fe 100644 --- a/belay/pyboard.py +++ b/belay/pyboard.py @@ -357,8 +357,15 @@ def __init__( time.sleep(1.0) + atexit.register(self.close) + def close(self): + if not self.serial: + return + self.exit_raw_repl() self.serial.close() + self.serial = None + atexit.unregister(self.close) def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None): """ From fa37bf3285f989972f4651c65a821b288eaab974 Mon Sep 17 00:00:00 2001 From: BrianPugh Date: Sun, 29 Jan 2023 16:48:24 -0800 Subject: [PATCH 2/3] give windows even more time --- belay/pyboard.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/belay/pyboard.py b/belay/pyboard.py index c3d03fe..377d921 100644 --- a/belay/pyboard.py +++ b/belay/pyboard.py @@ -216,6 +216,9 @@ def process_output(): thread.start() time.sleep(5.0) # Give process a chance to boot up. + if platform.system() == "Windows": + # Windows needs more time + time.sleep(5.0) def cleanup(): _kill_process(subp.pid) From 08db9c6566e6555983be1ce21845f229716e5cf6 Mon Sep 17 00:00:00 2001 From: BrianPugh Date: Sun, 29 Jan 2023 17:12:55 -0800 Subject: [PATCH 3/3] unregister _kill_process after closing process --- belay/pyboard.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/belay/pyboard.py b/belay/pyboard.py index 377d921..a1c7bea 100644 --- a/belay/pyboard.py +++ b/belay/pyboard.py @@ -220,13 +220,11 @@ def process_output(): # Windows needs more time time.sleep(5.0) - def cleanup(): - _kill_process(subp.pid) - - atexit.register(cleanup) + atexit.register(self.close) def close(self): _kill_process(self.subp.pid) + atexit.unregister(self.close) def read(self, size=1): while len(self.buf) < size: