forked from lnmx/serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.go
75 lines (59 loc) · 1.21 KB
/
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
package serial
// NewPort creates a Port.
//
func NewPort() (p *Port) {
return &Port{
config: Config{
BaudRate: BaudRate_9600,
DataBits: DataBits_8,
Parity: Parity_None,
StopBits: StopBits_1,
VMIN: 1,
VTIME: 0,
ReadIntervalTimeout: 2,
ReadTotalTimeoutConstant: 10,
ReadTotalTimeoutMultiplier: 0,
WriteTotalTimeoutMultiplier: 0,
WriteTotalTimeoutConstant: 0,
},
}
}
// Open creates and opens a Port.
//
func Open(device string, baud int) (p *Port, err error) {
p = NewPort()
config := p.Config()
config.Device = device
config.BaudRate = baud
err = p.Configure(config)
if err != nil {
return
}
err = p.Open()
if err != nil {
return
}
return
}
// Config retrieves a copy of the Port's current configuration
//
func (p *Port) Config() Config {
return p.config
}
// Configure applies a new configuration to the Port
//
func (p *Port) Configure(cfg Config) (err error) {
return p.configure(cfg)
}
func (p *Port) Open() (err error) {
return p.open()
}
func (p *Port) Close() (err error) {
return p.close()
}
func (p *Port) Read(b []byte) (n int, err error) {
return p.read(b)
}
func (p *Port) Write(b []byte) (n int, err error) {
return p.write(b)
}