-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathendpoint_serial.go
More file actions
78 lines (64 loc) · 1.38 KB
/
endpoint_serial.go
File metadata and controls
78 lines (64 loc) · 1.38 KB
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
package gomavlib
import (
"context"
"io"
"net"
"time"
"go.bug.st/serial"
)
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
}
type rwcToConn struct {
io.ReadWriteCloser
}
func (*rwcToConn) LocalAddr() net.Addr {
return nil
}
func (*rwcToConn) RemoteAddr() net.Addr {
return nil
}
func (*rwcToConn) SetDeadline(_ time.Time) error {
return nil
}
func (*rwcToConn) SetReadDeadline(_ time.Time) error {
return nil
}
func (*rwcToConn) SetWriteDeadline(_ time.Time) error {
return nil
}
// EndpointSerial sets up a endpoint that works with a serial port.
type EndpointSerial struct {
// name of the device of the serial port (i.e: /dev/ttyUSB0)
Device string
// baud rate (i.e: 57600)
Baud int
}
func (conf EndpointSerial) init(node *Node) (Endpoint, error) {
e := &endpointClient{
node: node,
conf: EndpointCustomClient{
Connect: func(_ context.Context) (net.Conn, error) {
rwc, err := serialOpenFunc(conf.Device, conf.Baud)
if err != nil {
return nil, err
}
return &rwcToConn{rwc}, nil
},
Label: "serial",
},
}
err := e.initialize()
return e, err
}