Skip to content

Commit d59ca4e

Browse files
committed
tools/pyboard.py: Change logic for when raw ">" prompt is parsed.
In raw REPL ">" indicates the prompt. We originally read this character upon entering the raw REPL, and after reading the last bit of the output. This patch changes the logic so the ">" is read only just before trying to send the next command. To make this work (and as an added feature) the input buffer is now flushed upon entering raw REPL. The main reason for this change is so that pyboard.py recognises the EOF when sys.exit() is called on the pyboard. Ie, if you run pyboard.py with a script that calls sys.exit(), then pyboard.py will exit after the sys.exit() is called.
1 parent 3ce212e commit d59ca4e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

tools/pyboard.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,22 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
7171

7272
def enter_raw_repl(self):
7373
self.serial.write(b'\r\x03\x03') # ctrl-C twice: interrupt any running program
74+
75+
# flush input (without relying on serial.flushInput())
76+
n = self.serial.inWaiting()
77+
while n > 0:
78+
self.serial.read(n)
79+
n = self.serial.inWaiting()
80+
7481
self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
7582
data = self.read_until(1, b'to exit\r\n>')
7683
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
7784
print(data)
7885
raise PyboardError('could not enter raw repl')
86+
7987
self.serial.write(b'\x04') # ctrl-D: soft reset
80-
data = self.read_until(1, b'to exit\r\n>')
81-
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
88+
data = self.read_until(1, b'to exit\r\n')
89+
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n'):
8290
print(data)
8391
raise PyboardError('could not enter raw repl')
8492

@@ -93,8 +101,8 @@ def follow(self, timeout, data_consumer=None):
93101
data = data[:-1]
94102

95103
# wait for error output
96-
data_err = self.read_until(2, b'\x04>', timeout=timeout)
97-
if not data_err.endswith(b'\x04>'):
104+
data_err = self.read_until(1, b'\x04', timeout=timeout)
105+
if not data_err.endswith(b'\x04'):
98106
raise PyboardError('timeout waiting for second EOF reception')
99107
data_err = data_err[:-2]
100108

@@ -107,6 +115,11 @@ def exec_raw_no_follow(self, command):
107115
else:
108116
command_bytes = bytes(command, encoding='utf8')
109117

118+
# check we have a prompt
119+
data = self.read_until(1, b'>')
120+
if not data.endswith(b'>'):
121+
raise PyboardError('could not enter raw repl')
122+
110123
# write command
111124
for i in range(0, len(command_bytes), 256):
112125
self.serial.write(command_bytes[i:min(i + 256, len(command_bytes))])

0 commit comments

Comments
 (0)