@@ -22,12 +22,11 @@ import (
2222 "net/url"
2323 "path/filepath"
2424 "strings"
25- "time"
2625
2726 "github.com/arduino/arduino-cli/arduino/cores"
2827 "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
28+ "github.com/arduino/arduino-cli/arduino/serialutils"
2929 "github.com/arduino/arduino-cli/arduino/sketches"
30- "github.com/arduino/arduino-cli/cli/feedback"
3130 "github.com/arduino/arduino-cli/commands"
3231 "github.com/arduino/arduino-cli/executils"
3332 rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -275,38 +274,26 @@ func runProgramAction(pm *packagemanager.PackageManager,
275274 outStream .Write ([]byte (fmt .Sprintf ("Performing 1200-bps touch reset on serial port %s" , p )))
276275 outStream .Write ([]byte (fmt .Sprintln ()))
277276 }
278- if err := touchSerialPortAt1200bps (p ); err != nil {
277+ logrus .Infof ("Touching port %s at 1200bps" , port )
278+ if err := serialutils .TouchSerialPortAt1200bps (p ); err != nil {
279279 outStream .Write ([]byte (fmt .Sprintf ("Cannot perform port reset: %s" , err )))
280280 outStream .Write ([]byte (fmt .Sprintln ()))
281281 }
282282 break
283283 }
284284 }
285-
286- // Scanning for available ports seems to open the port or
287- // otherwise assert DTR, which would cancel the WDT reset if
288- // it happened within 250 ms. So we wait until the reset should
289- // have already occurred before we start scanning.
290- time .Sleep (500 * time .Millisecond )
291285 }
292286
293287 // Wait for upload port if requested
294288 if uploadProperties .GetBoolean ("upload.wait_for_upload_port" ) {
295289 if verbose {
296290 outStream .Write ([]byte (fmt .Sprintln ("Waiting for upload port..." )))
297291 }
298- if p , err := waitForNewSerialPort (); err != nil {
299- return fmt .Errorf ("cannot detect serial ports: %s" , err )
300- } else if p == "" {
301- feedback .Print ("No new serial port detected." )
302- } else {
303- actualPort = p
304- }
305292
306- // on OS X, if the port is opened too quickly after it is detected,
307- // a "Resource busy" error occurs, add a delay to workaround.
308- // This apply to other platforms as well.
309- time . Sleep ( 500 * time . Millisecond )
293+ actualPort , err = serialutils . WaitForNewSerialPortOrDefaultTo ( actualPort )
294+ if err != nil {
295+ return errors . WithMessage ( err , "detecting serial port" )
296+ }
310297 }
311298 }
312299
@@ -382,64 +369,6 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
382369 return nil
383370}
384371
385- func touchSerialPortAt1200bps (port string ) error {
386- logrus .Infof ("Touching port %s at 1200bps" , port )
387-
388- // Open port
389- p , err := serial .Open (port , & serial.Mode {BaudRate : 1200 })
390- if err != nil {
391- return fmt .Errorf ("opening port: %s" , err )
392- }
393- defer p .Close ()
394-
395- if err = p .SetDTR (false ); err != nil {
396- return fmt .Errorf ("cannot set DTR" )
397- }
398- return nil
399- }
400-
401- // waitForNewSerialPort is meant to be called just after a reset. It watches the ports connected
402- // to the machine until a port appears. The new appeared port is returned
403- func waitForNewSerialPort () (string , error ) {
404- logrus .Infof ("Waiting for upload port..." )
405-
406- getPortMap := func () (map [string ]bool , error ) {
407- ports , err := serial .GetPortsList ()
408- if err != nil {
409- return nil , err
410- }
411- res := map [string ]bool {}
412- for _ , port := range ports {
413- res [port ] = true
414- }
415- return res , nil
416- }
417-
418- last , err := getPortMap ()
419- if err != nil {
420- return "" , fmt .Errorf ("scanning serial port: %s" , err )
421- }
422-
423- deadline := time .Now ().Add (10 * time .Second )
424- for time .Now ().Before (deadline ) {
425- now , err := getPortMap ()
426- if err != nil {
427- return "" , fmt .Errorf ("scanning serial port: %s" , err )
428- }
429-
430- for p := range now {
431- if ! last [p ] {
432- return p , nil // Found it!
433- }
434- }
435-
436- last = now
437- time .Sleep (250 * time .Millisecond )
438- }
439-
440- return "" , nil
441- }
442-
443372func determineBuildPathAndSketchName (importFile , importDir string , sketch * sketches.Sketch , fqbn * cores.FQBN ) (* paths.Path , string , error ) {
444373 // In general, compiling a sketch will produce a set of files that are
445374 // named as the sketch but have different extensions, for example Sketch.ino
0 commit comments