Skip to content

Commit 48a9b3f

Browse files
committed
tools: Improve timout/reading of pyboard.py.
1 parent cce7119 commit 48a9b3f

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

tools/pyboard.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ def __init__(self, serial_device):
3232
def close(self):
3333
self.serial.close()
3434

35+
def read_until(self, min_num_bytes, ending, timeout=10):
36+
data = self.serial.read(min_num_bytes)
37+
timeout_count = 0
38+
while True:
39+
if self.serial.inWaiting() > 0:
40+
data = data + self.serial.read(self.serial.inWaiting())
41+
time.sleep(0.01)
42+
timeout_count = 0
43+
elif data.endswith(ending):
44+
break
45+
else:
46+
timeout_count += 1
47+
if timeout_count >= 10 * timeout:
48+
break
49+
time.sleep(0.1)
50+
return data
51+
3552
def enter_raw_repl(self):
3653
self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
3754
self.serial.write(b'\x04') # ctrl-D: soft reset
38-
data = self.serial.read(1)
39-
while self.serial.inWaiting() > 0:
40-
data = data + self.serial.read(self.serial.inWaiting())
41-
time.sleep(0.1)
55+
data = self.read_until(1, b'to exit\r\n>')
4256
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
4357
print(data)
4458
raise PyboardError('could not enter raw repl')
@@ -60,19 +74,7 @@ def exec(self, command):
6074
data = self.serial.read(2)
6175
if data != b'OK':
6276
raise PyboardError('could not exec command')
63-
data = self.serial.read(2)
64-
timeout = 0
65-
while True:
66-
if self.serial.inWaiting() > 0:
67-
data = data + self.serial.read(self.serial.inWaiting())
68-
timeout = 0
69-
elif data.endswith(b'\x04>'):
70-
break
71-
else:
72-
timeout += 1
73-
if timeout > 100:
74-
break
75-
time.sleep(0.1)
77+
data = self.read_until(2, b'\x04>')
7678
if not data.endswith(b'\x04>'):
7779
print(data)
7880
raise PyboardError('timeout waiting for EOF reception')

0 commit comments

Comments
 (0)