-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
adaptor.go
128 lines (104 loc) · 2.76 KB
/
adaptor.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package serialport
import (
"fmt"
"io"
"go.bug.st/serial"
"gobot.io/x/gobot/v2"
)
// configuration contains all changeable attributes of the driver.
type configuration struct {
name string
baudRate int
}
// Adaptor represents a Gobot Adaptor for the Serial Communication
type Adaptor struct {
port string
cfg *configuration
sp io.ReadWriteCloser
connectFunc func(string, int) (io.ReadWriteCloser, error)
}
// NewAdaptor returns a new adaptor given a port for the serial communication
func NewAdaptor(port string, opts ...optionApplier) *Adaptor {
cfg := configuration{
name: gobot.DefaultName("Serial"),
baudRate: 115200,
}
a := Adaptor{
cfg: &cfg,
port: port,
connectFunc: func(port string, baudRate int) (io.ReadWriteCloser, error) {
return serial.Open(port, &serial.Mode{BaudRate: baudRate})
},
}
for _, o := range opts {
o.apply(a.cfg)
}
return &a
}
// WithName is used to replace the default name of the driver.
func WithName(name string) optionApplier {
return nameOption(name)
}
// WithName is used to replace the default name of the driver.
func WithBaudRate(baudRate int) optionApplier {
return baudRateOption(baudRate)
}
// Name returns the adaptors name
func (a *Adaptor) Name() string {
return a.cfg.name
}
// SetName sets the adaptors name
// Deprecated: Please use option [serialport.WithName] instead.
func (a *Adaptor) SetName(n string) {
WithName(n).apply(a.cfg)
}
// Connect initiates a connection to the serial port.
func (a *Adaptor) Connect() error {
if a.sp != nil {
return fmt.Errorf("serial port is already connected, try reconnect or run disconnect first")
}
sp, err := a.connectFunc(a.port, a.cfg.baudRate)
if err != nil {
return err
}
a.sp = sp
return nil
}
// Finalize finalizes the adaptor by disconnect
func (a *Adaptor) Finalize() error {
return a.Disconnect()
}
// Disconnect terminates the connection to the port.
func (a *Adaptor) Disconnect() error {
if a.sp != nil {
if err := a.sp.Close(); err != nil {
return err
}
a.sp = nil
}
return nil
}
// Reconnect attempts to reconnect to the port. If the port is connected it will first close
// that connection and then establish a new connection.
func (a *Adaptor) Reconnect() error {
if a.sp != nil {
if err := a.Disconnect(); err != nil {
return err
}
}
return a.Connect()
}
// Port returns the adaptors port
func (a *Adaptor) Port() string { return a.port }
// IsConnected returns the connection state
func (a *Adaptor) IsConnected() bool {
return a.sp != nil
}
// SerialRead reads from the port to the given reference
func (a *Adaptor) SerialRead(pData []byte) (int, error) {
return a.sp.Read(pData)
}
// SerialWrite writes to the port
func (a *Adaptor) SerialWrite(data []byte) (int, error) {
return a.sp.Write(data)
}