@@ -117,13 +117,32 @@ def inWaiting(self):
117117 return n_waiting
118118
119119class Pyboard :
120- def __init__ (self , device , baudrate = 115200 , user = 'micro' , password = 'python' ):
120+ def __init__ (self , device , baudrate = 115200 , user = 'micro' , password = 'python' , wait = 0 ):
121121 if device and device [0 ].isdigit () and device [- 1 ].isdigit () and device .count ('.' ) == 3 :
122122 # device looks like an IP address
123123 self .serial = TelnetToSerial (device , user , password , read_timeout = 10 )
124124 else :
125125 import serial
126- self .serial = serial .Serial (device , baudrate = baudrate , interCharTimeout = 1 )
126+ delayed = False
127+ for attempt in range (wait + 1 ):
128+ try :
129+ self .serial = serial .Serial (device , baudrate = baudrate , interCharTimeout = 1 )
130+ break
131+ except OSError , IOError : # Py2 and Py3 have different errors
132+ if wait == 0 :
133+ continue
134+ if attempt == 0 :
135+ sys .stdout .write ('Waiting {} seconds for pyboard ' .format (wait ))
136+ delayed = True
137+ time .sleep (1 )
138+ sys .stdout .write ('.' )
139+ sys .stdout .flush ()
140+ else :
141+ if delayed :
142+ print ('' )
143+ raise PyboardError ('failed to access ' + device )
144+ if delayed :
145+ print ('' )
127146
128147 def close (self ):
129148 self .serial .close ()
@@ -261,13 +280,14 @@ def main():
261280 cmd_parser .add_argument ('-u' , '--user' , default = 'micro' , help = 'the telnet login username' )
262281 cmd_parser .add_argument ('-p' , '--password' , default = 'python' , help = 'the telnet login password' )
263282 cmd_parser .add_argument ('-c' , '--command' , help = 'program passed in as string' )
283+ cmd_parser .add_argument ('-w' , '--wait' , default = 0 , type = int , help = 'seconds to wait for USB connected board to become available' )
264284 cmd_parser .add_argument ('--follow' , action = 'store_true' , help = 'follow the output after running the scripts [default if no scripts given]' )
265285 cmd_parser .add_argument ('files' , nargs = '*' , help = 'input files' )
266286 args = cmd_parser .parse_args ()
267287
268288 def execbuffer (buf ):
269289 try :
270- pyb = Pyboard (args .device , args .baudrate , args .user , args .password )
290+ pyb = Pyboard (args .device , args .baudrate , args .user , args .password , args . wait )
271291 pyb .enter_raw_repl ()
272292 ret , ret_err = pyb .exec_raw (buf , timeout = None , data_consumer = stdout_write_bytes )
273293 pyb .exit_raw_repl ()
@@ -291,7 +311,7 @@ def execbuffer(buf):
291311
292312 if args .follow or (args .command is None and len (args .files ) == 0 ):
293313 try :
294- pyb = Pyboard (args .device , args .baudrate , args .user , args .password )
314+ pyb = Pyboard (args .device , args .baudrate , args .user , args .password , args . wait )
295315 ret , ret_err = pyb .follow (timeout = None , data_consumer = stdout_write_bytes )
296316 pyb .close ()
297317 except PyboardError as er :
0 commit comments