-
Notifications
You must be signed in to change notification settings - Fork 36
/
endpoint_serial.go
84 lines (66 loc) · 1.58 KB
/
endpoint_serial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gomavlib
import (
"context"
"io"
"go.bug.st/serial"
"github.com/bluenviron/gomavlib/v3/pkg/reconnector"
)
var serialOpenFunc = func(device string, baud int) (io.ReadWriteCloser, error) {
dev, err := serial.Open(device, &serial.Mode{
BaudRate: baud,
Parity: serial.NoParity,
DataBits: 8,
StopBits: serial.OneStopBit,
})
if err != nil {
return nil, err
}
dev.SetDTR(true) //nolint:errcheck
dev.SetRTS(true) //nolint:errcheck
return dev, nil
}
// EndpointSerial sets up a endpoint that works with a serial port.
type EndpointSerial struct {
// the name of the device of the serial port (i.e: /dev/ttyUSB0)
Device string
// baud rate (i.e: 57600)
Baud int
}
type endpointSerial struct {
conf EndpointConf
reconnector *reconnector.Reconnector
}
func (conf EndpointSerial) init(_ *Node) (Endpoint, error) {
// check device existence
test, err := serialOpenFunc(conf.Device, conf.Baud)
if err != nil {
return nil, err
}
test.Close()
t := &endpointSerial{
conf: conf,
reconnector: reconnector.New(
func(_ context.Context) (io.ReadWriteCloser, error) {
return serialOpenFunc(conf.Device, conf.Baud)
},
),
}
return t, nil
}
func (t *endpointSerial) isEndpoint() {}
func (t *endpointSerial) Conf() EndpointConf {
return t.conf
}
func (t *endpointSerial) close() {
t.reconnector.Close()
}
func (t *endpointSerial) oneChannelAtAtime() bool {
return true
}
func (t *endpointSerial) provide() (string, io.ReadWriteCloser, error) {
conn, ok := t.reconnector.Reconnect()
if !ok {
return "", nil, errTerminated
}
return "serial", conn, nil
}