-
Notifications
You must be signed in to change notification settings - Fork 7
/
serial_linux.go
317 lines (294 loc) · 4.72 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package serial
import (
"errors"
"os"
"syscall"
"unsafe"
)
// bits/termios.h
const nccs = 32
type cc_t byte
type speed_t uint32
type tcflag_t uint32
type termios struct {
c_iflag tcflag_t // input specific flags (bitmask)
c_oflag tcflag_t // output specific flags (bitmask)
c_cflag tcflag_t // control flags (bitmask)
c_lflag tcflag_t // local flags (bitmask)
c_cc [nccs]cc_t // special characters
c_ispeed speed_t // input speed
c_ospeed speed_t // output speed
}
// bits/termios.h
const (
ignbrk = 1 << iota
brkint
ignpar
parmrk
inpck
istrip
inlcr
igncr
icrnl
iuclc
ixon
ixany
ixoff
imaxbeL
iutf8
)
// bits/termios.h
const (
opost = 1 << iota
olcuc
onlcr
ocrnl
onocr
onlret
ofill
ofdel
nldly
)
// bits/termios.h
const (
isig = 1 << iota
icanon
xcase
echo
echoe
echok
echonl
noflsh
tostop
echoctl
echoprt
echoke
flusho
_undef_
pendin
iexten
extproc
)
// bits/termios.h
var boud = map[int]tcflag_t{
0: 0000000,
50: 0000001,
75: 0000002,
110: 0000003,
134: 0000004,
150: 0000005,
200: 0000006,
300: 0000007,
600: 0000010,
1200: 0000011,
1800: 0000012,
2400: 0000013,
4800: 0000014,
9600: 0000015,
19200: 0000016,
38400: 0000017,
57600: 0010001,
115200: 0010002,
230400: 0010003,
460800: 0010004,
500000: 0010005,
576000: 0010006,
921600: 0010007,
1000000: 0010010,
1152000: 0010011,
1500000: 0010012,
2000000: 0010013,
2500000: 0010014,
3000000: 0010015,
3500000: 0010016,
4000000: 0010017,
}
// bits/termios.h
var bits = map[int]tcflag_t{
5: 0000000,
6: 0000020,
7: 0000040,
8: 0000060,
}
// bits/termios.h
const (
cstopb = 0000100 << iota
cread
parenb
parodd
hupcl
clocal
)
// bits/termios.h
const (
vintr = iota
vquit
verase
vkill
veof
vtime
vmin
vswtc
vstart
vstop
vsusp
veol
vreprint
vdiscard
vwerase
vlnext
veol2
)
// bits/termios.h
const (
cbaud = 0010017
cbaudex = 0010000
crtscts = 020000000000
)
// asm-generic/ioctls.h
const (
tcgets = 0x5401
tcsets = 0x5402
)
func (s *Serial) tcGetAttr(cfg *termios) error {
_, _, e := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(s.f.Fd()),
tcgets,
uintptr(unsafe.Pointer(cfg)),
)
if e != 0 {
return os.NewSyscallError("tcgetattr", e)
}
return nil
}
func (s *Serial) tcSetAttr(cfg *termios) error {
_, _, e := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(s.f.Fd()),
tcsets,
uintptr(unsafe.Pointer(cfg)),
)
if e != 0 {
return os.NewSyscallError("tcsetattr", e)
}
return nil
}
func (s *Serial) init() error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
t.c_iflag = 0
t.c_oflag = 0
t.c_lflag = 0
t.c_cflag = boud[9600] | bits[8] | clocal | cread | hupcl
t.c_cc[vmin] = 1
t.c_cc[vtime] = 0
t.c_ispeed = speed_t(boud[9600])
t.c_ospeed = speed_t(boud[9600])
if err := s.tcSetAttr(&t); err != nil {
return err
}
return syscall.SetNonblock(int(s.f.Fd()), false)
}
func (s *Serial) setSpeed(b int) error {
var t termios
bb, ok := boud[b]
if !ok {
return errors.New("Unknown boud rate")
}
if err := s.tcGetAttr(&t); err != nil {
return err
}
t.c_cflag &^= cbaud | cbaudex
t.c_cflag |= bb
t.c_ispeed = speed_t(b)
t.c_ospeed = speed_t(b)
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}
func (s *Serial) setParity(parity, odd bool) error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
if parity {
t.c_cflag |= parenb
} else {
t.c_cflag &^= parenb
}
if odd {
t.c_cflag |= parodd
} else {
t.c_cflag &^= parodd
}
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}
func (s *Serial) setStopBits2(two bool) error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
if two {
t.c_cflag |= cstopb
} else {
t.c_cflag &^= cstopb
}
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}
func (s *Serial) setFlowCtrl(hw, soft bool) error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
if hw {
t.c_cflag |= crtscts
} else {
t.c_cflag &^= crtscts
}
if soft {
t.c_iflag |= (ixon | ixoff | ixany)
} else {
t.c_iflag &^= (ixon | ixoff | ixany)
}
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}
func (s *Serial) setCanon(canon bool) error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
if canon {
t.c_lflag |= icanon
} else {
t.c_lflag &^= icanon
}
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}
func (s *Serial) setRawRead(vmin_, vtime_ int) error {
var t termios
if err := s.tcGetAttr(&t); err != nil {
return err
}
t.c_cc[vmin] = cc_t(vmin_)
t.c_cc[vtime] = cc_t(vtime_)
if err := s.tcSetAttr(&t); err != nil {
return err
}
return nil
}