forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint_serial.go
More file actions
57 lines (46 loc) · 1.03 KB
/
endpoint_serial.go
File metadata and controls
57 lines (46 loc) · 1.03 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
package gomavlib
import (
"fmt"
"github.com/tarm/serial"
"io"
"regexp"
"strconv"
)
var reSerial = regexp.MustCompile("^(.+?):([0-9]+)$")
// EndpointSerial sets up a endpoint that works through a serial port.
type EndpointSerial struct {
// the address of the serial port in format name:baudrate
// example: /dev/ttyUSB0:57600
Address string
}
type endpointSerial struct {
conf EndpointSerial
io.ReadWriteCloser
}
func (conf EndpointSerial) init() (Endpoint, error) {
matches := reSerial.FindStringSubmatch(conf.Address)
if matches == nil {
return nil, fmt.Errorf("invalid address")
}
name := matches[1]
baud, _ := strconv.Atoi(matches[2])
port, err := serial.OpenPort(&serial.Config{
Name: name,
Baud: baud,
})
if err != nil {
return nil, err
}
t := &endpointSerial{
conf: conf,
ReadWriteCloser: port,
}
return t, nil
}
func (t *endpointSerial) isEndpoint() {}
func (t *endpointSerial) Conf() interface{} {
return t.conf
}
func (t *endpointSerial) Label() string {
return fmt.Sprintf("serial")
}