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.
This commit is contained in:
parent
946f870e3c
commit
f4fcc14cfb
@ -117,13 +117,32 @@ class TelnetToSerial:
|
|||||||
return n_waiting
|
return n_waiting
|
||||||
|
|
||||||
class Pyboard:
|
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:
|
if device and device[0].isdigit() and device[-1].isdigit() and device.count('.') == 3:
|
||||||
# device looks like an IP address
|
# device looks like an IP address
|
||||||
self.serial = TelnetToSerial(device, user, password, read_timeout=10)
|
self.serial = TelnetToSerial(device, user, password, read_timeout=10)
|
||||||
else:
|
else:
|
||||||
import serial
|
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):
|
def close(self):
|
||||||
self.serial.close()
|
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('-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('-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('-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('--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')
|
cmd_parser.add_argument('files', nargs='*', help='input files')
|
||||||
args = cmd_parser.parse_args()
|
args = cmd_parser.parse_args()
|
||||||
|
|
||||||
def execbuffer(buf):
|
def execbuffer(buf):
|
||||||
try:
|
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()
|
pyb.enter_raw_repl()
|
||||||
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
|
ret, ret_err = pyb.exec_raw(buf, timeout=None, data_consumer=stdout_write_bytes)
|
||||||
pyb.exit_raw_repl()
|
pyb.exit_raw_repl()
|
||||||
@ -291,7 +311,7 @@ def main():
|
|||||||
|
|
||||||
if args.follow or (args.command is None and len(args.files) == 0):
|
if args.follow or (args.command is None and len(args.files) == 0):
|
||||||
try:
|
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)
|
ret, ret_err = pyb.follow(timeout=None, data_consumer=stdout_write_bytes)
|
||||||
pyb.close()
|
pyb.close()
|
||||||
except PyboardError as er:
|
except PyboardError as er:
|
||||||
|
Loading…
Reference in New Issue
Block a user