Skip to content

Commit d71fc37

Browse files
committed
tools: Improve reading back from pyboard.
1 parent 57ad7aa commit d71fc37

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tools/pyboard.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,25 @@ def exec(self, command):
5858
if data != b'OK':
5959
raise Exception('could not exec command')
6060
data = self.serial.read(2)
61-
while self.serial.inWaiting() > 0:
62-
data = data + self.serial.read(self.serial.inWaiting())
63-
time.sleep(0.01)
61+
timeout = 0
62+
while True:
63+
if self.serial.inWaiting() > 0:
64+
data = data + self.serial.read(self.serial.inWaiting())
65+
timeout = 0
66+
elif data.endswith(b'\x04>'):
67+
break
68+
else:
69+
timeout += 1
70+
if timeout > 100:
71+
break
72+
time.sleep(0.1)
6473
if not data.endswith(b'\x04>'):
6574
print(data)
66-
raise Exception('could not exec command')
75+
raise Exception('timeout waiting for EOF reception')
6776
if data.startswith(b'Traceback') or data.startswith(b' File '):
6877
print(data)
6978
raise Exception('command failed')
70-
return str(data[:-2], encoding='ascii')
79+
return data[:-2]
7180

7281
def execfile(self, filename):
7382
with open(filename) as f:
@@ -82,7 +91,7 @@ def execfile(filename, device='/dev/ttyACM0'):
8291
pyb = Pyboard(device)
8392
pyb.enter_raw_repl()
8493
output = pyb.execfile(filename)
85-
print(output, end='')
94+
print(str(output, encoding='ascii'), end='')
8695
pyb.exit_raw_repl()
8796
pyb.close()
8897

0 commit comments

Comments
 (0)