Skip to content

Commit 40a6925

Browse files
fix a panic when the serial port is busy
1 parent 37c4d48 commit 40a6925

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"log"
88
"time"
99

10-
"github.com/arduino/arduino-cli/arduino/serialutils"
1110
"github.com/arduino/arduino-cli/executils"
1211
helper "github.com/arduino/fwuploader-plugin-helper"
1312
"github.com/arduino/go-paths-helper"
1413
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/certificate"
1514
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/serial"
15+
serialutils "github.com/arduino/uno-r4-wifi-fwuploader-plugin/serial/utils"
1616
semver "go.bug.st/relaxed-semver"
1717
serialx "go.bug.st/serial"
1818
"golang.org/x/exp/slog"
@@ -215,7 +215,7 @@ func (p *unoR4WifiPlugin) uploadCommandsSketch(portAddress string, feedback *hel
215215
defer rebootFile.Remove()
216216

217217
slog.Info("sending serial reset")
218-
if _, err = serialutils.Reset(portAddress, false, nil, false); err != nil {
218+
if err := serialutils.TouchSerialPortAt1200bps(portAddress); err != nil {
219219
return err
220220
}
221221

serial/utils/utils.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Those function are token from https://github.com/arduino/arduino-cli/blob/master/arduino/serialutils/serialutils.go
2+
// that's because we don't have the `tr` here and importing the serialutils from the cli will lead to a panic
3+
package utils
4+
5+
import (
6+
"fmt"
7+
"runtime"
8+
"time"
9+
10+
"go.bug.st/serial"
11+
)
12+
13+
// TouchSerialPortAt1200bps open and close the serial port at 1200 bps. This
14+
// is used on many Arduino boards as a signal to put the board in "bootloader"
15+
// mode.
16+
func TouchSerialPortAt1200bps(port string) error {
17+
// Open port
18+
p, err := serial.Open(port, &serial.Mode{BaudRate: 1200})
19+
if err != nil {
20+
return fmt.Errorf("opening port at 1200bps")
21+
}
22+
23+
if runtime.GOOS != "windows" {
24+
// This is not required on Windows
25+
// TODO: Investigate if it can be removed for other OS too
26+
27+
// Set DTR to false
28+
if err = p.SetDTR(false); err != nil {
29+
p.Close()
30+
return fmt.Errorf("setting DTR to OFF")
31+
}
32+
}
33+
34+
// Close serial port
35+
p.Close()
36+
37+
// Scanning for available ports seems to open the port or
38+
// otherwise assert DTR, which would cancel the WDT reset if
39+
// it happens within 250 ms. So we wait until the reset should
40+
// have already occurred before going on.
41+
time.Sleep(500 * time.Millisecond)
42+
43+
return nil
44+
}

0 commit comments

Comments
 (0)