forked from tarm/serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_linux.go
211 lines (190 loc) · 4.2 KB
/
serial_linux.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// +build linux
package serial
import (
"os"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
func openPort(name string, baud int, databits byte, parity Parity, stopbits StopBits, readTimeout time.Duration) (p *Port, err error) {
f, err := os.OpenFile(name, unix.O_RDWR|unix.O_NOCTTY|unix.O_NONBLOCK, 0)
if err != nil {
return nil, err
}
defer func() {
if err != nil && f != nil {
f.Close()
}
}()
// Base settings
var cflagToUse uint32 = unix.CREAD | unix.CLOCAL | unix.BOTHER | unix.HUPCL
switch databits {
case 5:
cflagToUse |= unix.CS5
case 6:
cflagToUse |= unix.CS6
case 7:
cflagToUse |= unix.CS7
case 8:
cflagToUse |= unix.CS8
default:
return nil, ErrBadSize
}
// Stop bits settings
switch stopbits {
case Stop1:
// default is 1 stop bit
case Stop2:
cflagToUse |= unix.CSTOPB
default:
// Don't know how to set 1.5
return nil, ErrBadStopBits
}
// Parity settings
switch parity {
case ParityNone:
// default is no parity
case ParityOdd:
cflagToUse |= unix.PARENB
cflagToUse |= unix.PARODD
case ParityEven:
cflagToUse |= unix.PARENB
case ParityMark:
cflagToUse |= syscall.PARENB
cflagToUse |= unix.CMSPAR
cflagToUse |= syscall.PARODD
default:
return nil, ErrBadParity
}
fd := f.Fd()
vmin, vtime := posixTimeoutValues(readTimeout)
speed := uint32(baud)
t := unix.Termios{
Iflag: unix.IGNPAR,
Cflag: cflagToUse,
Ispeed: speed,
Ospeed: speed,
}
t.Cc[unix.VMIN] = vmin
t.Cc[unix.VTIME] = vtime
/*
Raise DTR (open modem) on open. DTR will be set low
again when the connection is closed due to HUPCL.
This should guarantee a correct "restart" action
on some devices.
*/
dtrFlag := unix.TIOCM_DTR
unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(unix.TIOCMBIS), // set/raise DTR pin
uintptr(unsafe.Pointer(&dtrFlag)))
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(fd),
uintptr(unix.TCSETS2),
uintptr(unsafe.Pointer(&t)),
); errno != 0 {
return nil, errno
}
if err = unix.SetNonblock(int(fd), false); err != nil {
return
}
return &Port{f: f}, nil
}
type Port struct {
// We intentionly do not use an "embedded" struct so that we
// don't export File
f *os.File
}
func (p *Port) Read(b []byte) (n int, err error) {
return p.f.Read(b)
}
func (p *Port) Write(b []byte) (n int, err error) {
return p.f.Write(b)
}
// Discards data written to the port but not transmitted,
// or data received but not read
func (p *Port) Flush() error {
const TCFLSH = 0x540B
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(TCFLSH),
uintptr(unix.TCIOFLUSH),
)
if errno == 0 {
return nil
}
return errno
}
// SendBreak sends a break (bus low value) for a given duration.
// In POSIX and linux implementations there are two cases for the duration value:
//
// if duration is zero there a break with at least 0.25 seconds
// but not more than 0.5 seconds will be send. If duration is not zero,
// than it's implementaion specific, which unit is used for duration.
// For more information tae a look at tcsendbreak(3) and ioctl_tty(2)
func (p *Port) SendBreak(d time.Duration) error {
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(unix.TCSBRK),
uintptr(d.Milliseconds()),
)
if errno == 0 {
return nil
}
return errno
}
func (p *Port) GetStatus() (n uint, err error) {
var status uint
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(unix.TIOCMGET),
uintptr(unsafe.Pointer(&status)),
); errno != 0 {
return status, errno
} else {
return status, nil
}
}
func (p *Port) SetDTR(v byte) (err error) {
req := unix.TIOCMBIS
if 0 == v {
req = unix.TIOCMBIC
}
var m uint = unix.TIOCM_DTR
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(req),
uintptr(unsafe.Pointer(&m)),
); errno != 0 {
return errno
} else {
return nil
}
}
func (p *Port) SetRTS(v byte) (err error) {
req := unix.TIOCMBIS
if 0 == v {
req = unix.TIOCMBIC
}
var m uint = unix.TIOCM_RTS
if _, _, errno := unix.Syscall(
unix.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(req),
uintptr(unsafe.Pointer(&m)),
); errno != 0 {
return errno
} else {
return nil
}
}
func (p *Port) Close() (err error) {
return p.f.Close()
}