tools: Improve timout/reading of pyboard.py.

This commit is contained in:
Damien George 2014-04-14 01:47:36 +01:00
parent cce7119a2b
commit 48a9b3fd11
1 changed files with 19 additions and 17 deletions

View File

@ -32,13 +32,27 @@ class Pyboard:
def close(self): def close(self):
self.serial.close() self.serial.close()
def read_until(self, min_num_bytes, ending, timeout=10):
data = self.serial.read(min_num_bytes)
timeout_count = 0
while True:
if self.serial.inWaiting() > 0:
data = data + self.serial.read(self.serial.inWaiting())
time.sleep(0.01)
timeout_count = 0
elif data.endswith(ending):
break
else:
timeout_count += 1
if timeout_count >= 10 * timeout:
break
time.sleep(0.1)
return data
def enter_raw_repl(self): def enter_raw_repl(self):
self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL
self.serial.write(b'\x04') # ctrl-D: soft reset self.serial.write(b'\x04') # ctrl-D: soft reset
data = self.serial.read(1) data = self.read_until(1, b'to exit\r\n>')
while self.serial.inWaiting() > 0:
data = data + self.serial.read(self.serial.inWaiting())
time.sleep(0.1)
if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'): if not data.endswith(b'raw REPL; CTRL-B to exit\r\n>'):
print(data) print(data)
raise PyboardError('could not enter raw repl') raise PyboardError('could not enter raw repl')
@ -60,19 +74,7 @@ class Pyboard:
data = self.serial.read(2) data = self.serial.read(2)
if data != b'OK': if data != b'OK':
raise PyboardError('could not exec command') raise PyboardError('could not exec command')
data = self.serial.read(2) data = self.read_until(2, b'\x04>')
timeout = 0
while True:
if self.serial.inWaiting() > 0:
data = data + self.serial.read(self.serial.inWaiting())
timeout = 0
elif data.endswith(b'\x04>'):
break
else:
timeout += 1
if timeout > 100:
break
time.sleep(0.1)
if not data.endswith(b'\x04>'): if not data.endswith(b'\x04>'):
print(data) print(data)
raise PyboardError('timeout waiting for EOF reception') raise PyboardError('timeout waiting for EOF reception')