From 3ffe36d4c4ec46b838629af21861b8b71cb6754f Mon Sep 17 00:00:00 2001 From: Jesper Korsen Date: Thu, 2 Mar 2023 21:14:06 +0200 Subject: [PATCH] added fixes for connecting to micropython if it doesn't jump into REPL after boot --- rshell/pyboard.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/rshell/pyboard.py b/rshell/pyboard.py index 3e35c0b..bfb51e5 100644 --- a/rshell/pyboard.py +++ b/rshell/pyboard.py @@ -160,11 +160,15 @@ def __init__(self, device, baudrate=115200, user='micro', password='python', wai def close(self): self.serial.close() - def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None): - data = self.serial.read(min_num_bytes) + def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None, hard_timeout=3): + if self.serial.inWaiting() > 0: + data = self.serial.read(min_num_bytes) + else: + data = b'' if data_consumer: data_consumer(data) timeout_count = 0 + hard_timeout_count = 0 while True: if data.endswith(ending): break @@ -176,8 +180,11 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None): timeout_count = 0 else: timeout_count += 1 + hard_timeout_count += 1 if timeout is not None and timeout_count >= 100 * timeout: break + if hard_timeout is not None and hard_timeout_count >= 100 * hard_timeout: + break time.sleep(0.01) return data @@ -203,7 +210,11 @@ def enter_raw_repl(self): raise PyboardError('could not enter raw repl') # By splitting this into 2 reads, it allows boot.py to print stuff, # which will show up after the soft reboot and before the raw REPL. - data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n') + for _ in range(2): + data = self.read_until(1, b'raw REPL; CTRL-B to exit\r\n') + if data.endswith(b'raw REPL; CTRL-B to exit\r\n'): + break + self.serial.write(b'\x03') # ctrl-C: stop running program if not data.endswith(b'raw REPL; CTRL-B to exit\r\n'): print(data) raise PyboardError('could not enter raw repl')