diff options
author | Peter Hinch <peter@hinch.me.uk> | 2015-12-01 06:13:50 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-12-08 11:10:09 +0000 |
commit | f4fcc14cfb69aeae381a2931eb1f606a2975b1e7 (patch) | |
tree | c55788444f89cb8f9462f93a4d0225b0566c11b5 /tools/pyboard.py | |
parent | 946f870e3c1a9537bd09036d2d3a655275f23d5a (diff) | |
download | micropython-f4fcc14cfb69aeae381a2931eb1f606a2975b1e7.tar.gz micropython-f4fcc14cfb69aeae381a2931eb1f606a2975b1e7.zip |
tools: Add option to pyboard.py to wait for serial device to be ready.
Also prints a nicer error message if the serial connection could not be
established.
Diffstat (limited to 'tools/pyboard.py')
-rwxr-xr-x | tools/pyboard.py | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/tools/pyboard.py b/tools/pyboard.py index 510f61be69..3ff694a7c8 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -117,13 +117,32 @@ class TelnetToSerial: return n_waiting class Pyboard: - def __init__(self, device, baudrate=115200, user='micro', password='python'): + def __init__(self, device, baudrate=115200, user='micro', password='python', wait=0): if device and device[0].isdigit() and device[-1].isdigit() and device.count('.') == 3: # device looks like an IP address self.serial = TelnetToSerial(device, user, password, read_timeout=10) else: import serial - self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1) + delayed = False + for attempt in range(wait + 1): + try: + self.serial = serial.Serial(device, baudrate=baudrate, interCharTimeout=1) + break + except OSError, IOError: # Py2 and Py3 have different errors + if wait == 0: + continue + if attempt == 0: + sys.stdout.write('Waiting {} seconds for pyboard '.format(wait)) + delayed = True + time.sleep(1) + sys.stdout.write('.') + sys.stdout.flush() + else: + if delayed: + print('') + raise PyboardError('failed to access ' + device) + if delayed: + print('') def close(self): self.serial.close() @@ -261,13 +280,14 @@ def main(): cmd_parser.add_argument('-u', '--user', default='micro', help='the telnet login username') cmd_parser.add_argument('-p', '--password', default='python', help='the telnet login password') cmd_parser.add_argument('-c', '--command', help='program passed in as string') + cmd_parser.add_argument('-w', '--wait', default=0, type=int, help='seconds to wait for USB connected board to become available') cmd_parser.add_argument('--follow', action='store_true', help='follow the output after running the scripts [default if no scripts given]') cmd_parser.add_argument('files', nargs='*', help='input files') args = cmd_parser.parse_args() def execbuffer(buf): try: - pyb = Pyboard(args.device, args.baudrate, args.user, args.password) + pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait) pyb.enter_raw_repl() ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes) pyb.exit_raw_repl() @@ -291,7 +311,7 @@ def main(): if args.follow or (args.command is None and len(args.files) == 0): try: - pyb = Pyboard(args.device, args.baudrate, args.user, args.password) + pyb = Pyboard(args.device, args.baudrate, args.user, args.password, args.wait) ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes) pyb.close() except PyboardError as er: |