@@ -22,12 +22,11 @@ import (
22
22
"net/url"
23
23
"path/filepath"
24
24
"strings"
25
- "time"
26
25
27
26
"github.com/arduino/arduino-cli/arduino/cores"
28
27
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
28
+ "github.com/arduino/arduino-cli/arduino/serialutils"
29
29
"github.com/arduino/arduino-cli/arduino/sketches"
30
- "github.com/arduino/arduino-cli/cli/feedback"
31
30
"github.com/arduino/arduino-cli/commands"
32
31
"github.com/arduino/arduino-cli/executils"
33
32
rpc "github.com/arduino/arduino-cli/rpc/commands"
@@ -275,38 +274,26 @@ func runProgramAction(pm *packagemanager.PackageManager,
275
274
outStream .Write ([]byte (fmt .Sprintf ("Performing 1200-bps touch reset on serial port %s" , p )))
276
275
outStream .Write ([]byte (fmt .Sprintln ()))
277
276
}
278
- if err := touchSerialPortAt1200bps (p ); err != nil {
277
+ logrus .Infof ("Touching port %s at 1200bps" , port )
278
+ if err := serialutils .TouchSerialPortAt1200bps (p ); err != nil {
279
279
outStream .Write ([]byte (fmt .Sprintf ("Cannot perform port reset: %s" , err )))
280
280
outStream .Write ([]byte (fmt .Sprintln ()))
281
281
}
282
282
break
283
283
}
284
284
}
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 )
291
285
}
292
286
293
287
// Wait for upload port if requested
294
288
if uploadProperties .GetBoolean ("upload.wait_for_upload_port" ) {
295
289
if verbose {
296
290
outStream .Write ([]byte (fmt .Sprintln ("Waiting for upload port..." )))
297
291
}
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
- }
305
292
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
+ }
310
297
}
311
298
}
312
299
@@ -382,64 +369,6 @@ func runTool(recipeID string, props *properties.Map, outStream, errStream io.Wri
382
369
return nil
383
370
}
384
371
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
-
443
372
func determineBuildPathAndSketchName (importFile , importDir string , sketch * sketches.Sketch , fqbn * cores.FQBN ) (* paths.Path , string , error ) {
444
373
// In general, compiling a sketch will produce a set of files that are
445
374
// named as the sketch but have different extensions, for example Sketch.ino
0 commit comments