-
Notifications
You must be signed in to change notification settings - Fork 2
/
serial_posix.go
625 lines (559 loc) · 12.5 KB
/
serial_posix.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// +build linux darwin dragonfly freebsd netbsd openbsd
package serial
import (
"io/ioutil"
"math"
"os"
"regexp"
"runtime"
"strings"
"sync"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
type Port struct {
sync.Mutex
f *os.File
rl sync.Mutex
wl sync.Mutex
baudrate int
timeout_firstbyte int
timeout_interbyte int
}
func ListPortNames() ([]string, error) {
serial_list := make([]string, 0)
ls, err := ioutil.ReadDir("/dev")
if err != nil {
return serial_list, err
}
for _, info := range ls {
name := info.Name()
var is_serial bool
switch runtime.GOOS {
case "linux":
// http://tldp.org/HOWTO/Serial-HOWTO-10.html
// Some non-standard names are currently not supported.
is_serial = strings.HasPrefix(name, "ttyS") ||
strings.HasPrefix(name, "ttyUSB") ||
strings.HasPrefix(name, "ttyACM")
case "darwin":
is_serial = strings.HasPrefix(name, "tty.")
case "freebsd":
// https://www.freebsd.org/doc/handbook/serial.html
// Call-in ports are named /dev/ttyuN where N is the port number,
// starting from zero. If a terminal is connected to the first
// serial port (COM1), use /dev/ttyu0 to refer to the terminal.
is_serial, _ = regexp.MatchString("ttyu[0-9]+", name)
case "openbsd":
// http://www.openbsd.org/faq/faq7.html#SerCon
// OpenBSD numbers the serial ports starting at tty00, DOS/Windows
// labels them starting at COM1. So, keep in mind tty02 is COM3,
// not COM2.
is_serial, _ = regexp.MatchString("tty[0-9]+", name)
case "netbsd":
// https://www.netbsd.org/docs/guide/en/chap-net-practice.html
// For NetBSD, “com” is the name of the serial port driver (the one
// that is displayed by dmesg) and “tty” is the name of the port.
// Since numbering starts at 0, com0 is the driver for the first
// serial port, named tty00. In the DOS world, instead, COM1 refers
// to the first serial port (usually located at 0x3f8), COM2 to the
// second, and so on. Therefore COM1 (DOS) corresponds to
// /dev/tty00 (NetBSD).
is_serial, _ = regexp.MatchString("tty[0-9]+", name)
case "dragonfly":
// https://www.dragonflybsd.org/docs/docs/newhandbook/serial_communications/
// Call-in ports are named /dev/ttydN* where ***N*** is the port
// number, starting from zero. Generally, you use the call-in port
// for terminals. Call-in ports require that the serial line assert
// the data carrier detect (DCD) signal to work correctly.
is_serial, _ = regexp.MatchString("ttyd[0-9]+", name)
}
if is_serial {
serial_list = append(serial_list, "/dev/"+name)
}
}
return serial_list, nil
}
func Open(name string, c *Config) (p *Port, err error) {
// Open the serial port.
f, err := os.OpenFile(name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
f.Close()
}
}()
p = &Port{f: f}
// Remove O_NONBLOCK flag after the port is open.
err = p.setFlag(0)
if err != nil {
return nil, err
}
// Set the configurations of the serial port.
if c == nil {
c = &defaultConfig
}
if c.BaudRate == 0 {
c.BaudRate = defaultConfig.BaudRate
}
if c.DataBits == 0 {
c.DataBits = defaultConfig.DataBits
}
t, err := buildTermios(c)
if err != nil {
return
}
err = p.setTermios(t)
if err != nil {
return nil, err
}
// Save the baudrate
p.baudrate = c.BaudRate
// Set default timeout
p.SetTimeout(defaultTimeoutFirstByte, defaultTimeoutInterByte)
// Clear buffers.
p.Flush()
// Set a finalizer.
runtime.SetFinalizer(p, (*Port).Close)
return p, nil
}
func (p *Port) Close() error {
if p == nil || p.f == nil {
return nil
}
err := p.f.Close()
if err != nil {
return err
}
p.f = nil
runtime.SetFinalizer(p, nil)
return nil
}
func (p *Port) Read(buf []byte) (int, error) {
if p == nil || p.f == nil {
return 0, ErrInvalid
}
p.rl.Lock()
defer p.rl.Unlock()
if len(buf) == 0 {
return 0, nil
}
if len(buf) == 1 {
t, err := p.getTermios()
if err != nil {
return 0, err
}
t.SetTotalTimeout(p.timeout_firstbyte)
err = p.setTermios(t)
if err != nil {
return 0, err
}
// TODO support timeout longer than 25.5 s.
return p.f.Read(buf)
}
// When no interbyte timeout is specified, set it based on baudrate
timeout_interbyte := p.timeout_interbyte
if timeout_interbyte == 0 {
timeout_interbyte = int(math.Ceil(1000 / (float64(p.baudrate) / 10) * 1.5))
}
// Read first byte with total timeout
t, err := p.getTermios()
if err != nil {
return 0, err
}
t.SetTotalTimeout(p.timeout_firstbyte)
err = p.setTermios(t)
if err != nil {
return 0, err
}
// TODO support timeout longer than 25.5 s.
n, err := p.f.Read(buf)
if n == 0 || err != nil {
return n, err
}
// Data did arrive.
// Perform non-blocking read from OS,
// and manage interbyte timeout ourselves.
t.SetTotalTimeout(0)
err = p.setTermios(t)
if err != nil {
return 0, err
}
for n < len(buf) {
time.Sleep(time.Duration(timeout_interbyte) * time.Millisecond)
delta, err := p.f.Read(buf[n:len(buf)])
n += delta
if delta == 0 || err != nil {
return n, err
}
}
return n, nil
}
func (p *Port) Write(buf []byte) (int, error) {
if p == nil || p.f == nil {
return 0, nil
}
p.wl.Lock()
defer p.wl.Unlock()
return p.f.Write(buf)
}
// Flush discards data written to the port but not transmitted,
// or data received but not read.
func (p *Port) Flush() error {
if p == nil || p.f == nil {
return nil
}
return p.flush()
}
func (p *Port) SetBaudRate(baudrate int) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
t, err := p.getTermios()
if err != nil {
return err
}
err = t.SetBaudrate(baudrate)
if err != nil {
return err
}
p.baudrate = baudrate
return p.setTermios(t)
}
func (p *Port) SetDataBits(databits int) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
t, err := p.getTermios()
if err != nil {
return err
}
err = t.SetDataBits(databits)
if err != nil {
return err
}
return p.setTermios(t)
}
func (p *Port) SetParity(parity Parity) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
t, err := p.getTermios()
if err != nil {
return err
}
err = t.SetParity(parity)
if err != nil {
return err
}
return p.setTermios(t)
}
func (p *Port) SetStopBits(stopbits StopBits) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
t, err := p.getTermios()
if err != nil {
return err
}
err = t.SetStopBits(stopbits)
if err != nil {
return err
}
return p.setTermios(t)
}
func (p *Port) SetFlowControl(flowcontrol FlowControl) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
t, err := p.getTermios()
if err != nil {
return err
}
err = t.SetFlowControl(flowcontrol)
if err != nil {
return err
}
return p.setTermios(t)
}
func (p *Port) SetTimeout(timeout_ms_firstbyte, timeout_ms_interbyte int) error {
if p == nil || p.f == nil {
return nil
}
p.rl.Lock()
p.wl.Lock()
defer func() {
p.rl.Unlock()
p.wl.Unlock()
}()
if timeout_ms_firstbyte < -1 || timeout_ms_firstbyte > 25500 {
return ErrInvalidParam
}
if timeout_ms_interbyte < 0 {
return ErrInvalidParam
}
p.timeout_firstbyte = timeout_ms_firstbyte
p.timeout_interbyte = timeout_ms_interbyte
return nil
}
func (p *Port) BaudRate() (int, error) {
t, err := p.getTermios()
if err != nil {
return 0, nil
}
return t.GetBaudrate()
}
func (p *Port) DataBits() (int, error) {
t, err := p.getTermios()
if err != nil {
return 0, nil
}
switch t.Cflag & syscall.CSIZE {
case syscall.CS5:
return 5, nil
case syscall.CS6:
return 6, nil
case syscall.CS7:
return 7, nil
case syscall.CS8:
return 8, nil
}
return 8, ErrUnexpected
}
func (p *Port) Parity() (Parity, error) {
const CMSPAR = 0x40000000
t, err := p.getTermios()
if err != nil {
return 0, nil
}
if t.Cflag&syscall.PARENB == 0 {
return ParityNone, nil
}
if t.Cflag&CMSPAR == 0 {
if t.Cflag&syscall.PARODD != 0 {
return ParityOdd, nil
}
return ParityEven, nil
} else {
if t.Cflag&syscall.PARODD != 0 {
return ParityMark, nil
}
return ParitySpace, nil
}
return ParityNone, ErrUnexpected
}
func (p *Port) StopBits() (StopBits, error) {
t, err := p.getTermios()
if err != nil {
return 0, nil
}
if t.Cflag&syscall.CSTOPB != 0 {
return StopBitsTwo, nil
}
return StopBitsOne, nil
}
func (p *Port) FlowControl() FlowControl {
return 0
}
const (
termios_POSIX_VDISABLE = 0xff
termios_CCTS_OFLOW = 0x00010000
termios_CRTS_IFLOW = 0x00020000
termios_CDTR_IFLOW = 0x00040000
termios_CDSR_OFLOW = 0x00080000
termios_CMSPAR = 0x40000000
)
type termios unix.Termios
func (p *Port) getTermios() (*termios, error) {
var t termios
_, _, e := unix.Syscall6(unix.SYS_IOCTL,
p.f.Fd(),
ioctlReadTermios,
uintptr(unsafe.Pointer(&t)),
0, 0, 0)
if e != 0 {
return nil, e
}
return &t, nil
}
func (p *Port) setTermios(t *termios) error {
_, _, e := unix.Syscall6(unix.SYS_IOCTL,
p.f.Fd(),
ioctlWriteTermios,
uintptr(unsafe.Pointer(t)),
0, 0, 0)
if e != 0 {
return e
}
return nil
}
// setFlag performs a fcntl syscall for F_SETFL to set the file status flags.
// File access mode (O_RDONLY, O_WRONLY, O_RDWR) and file creation flags (i.e.,
// O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC) in arg are ignored.
func (p *Port) setFlag(flag int) error {
_, _, e := unix.Syscall6(unix.SYS_FCNTL,
p.f.Fd(),
uintptr(unix.F_SETFL),
uintptr(flag),
0, 0, 0)
if e != 0 {
return e
}
return nil
}
func buildTermios(c *Config) (*termios, error) {
t := &termios{}
for i := 0; i < len(t.Cc); i++ {
t.Cc[i] = termios_POSIX_VDISABLE
}
t.Cflag |= unix.CREAD
err := t.SetBaudrate(c.BaudRate)
if err != nil {
return nil, err
}
err = t.SetDataBits(c.DataBits)
if err != nil {
return nil, err
}
err = t.SetParity(c.Parity)
if err != nil {
return nil, err
}
err = t.SetStopBits(c.StopBits)
if err != nil {
return nil, err
}
err = t.SetFlowControl(c.FlowControl)
if err != nil {
return nil, err
}
return t, nil
}
func (t *termios) SetDataBits(databits int) error {
switch databits {
case 5:
t.Cflag = t.Cflag&^unix.CSIZE | unix.CS5
case 6:
t.Cflag = t.Cflag&^unix.CSIZE | unix.CS6
case 7:
t.Cflag = t.Cflag&^unix.CSIZE | unix.CS7
case 8:
t.Cflag = t.Cflag&^unix.CSIZE | unix.CS8
default:
return ErrInvalidParam
}
return nil
}
func (t *termios) SetParity(parity Parity) error {
switch parity {
case ParityNone:
t.Cflag &^= unix.PARENB
case ParityOdd:
t.Cflag |= unix.PARENB
t.Cflag &^= termios_CMSPAR
t.Cflag |= unix.PARODD
case ParityEven:
t.Cflag |= unix.PARENB
t.Cflag &^= termios_CMSPAR
t.Cflag &^= unix.PARODD
case ParityMark:
t.Cflag |= unix.PARENB
t.Cflag |= termios_CMSPAR
t.Cflag |= unix.PARODD
case ParitySpace:
t.Cflag |= unix.PARENB
t.Cflag |= termios_CMSPAR
t.Cflag &^= unix.PARODD
default:
return ErrInvalidParam
}
return nil
}
func (t *termios) SetStopBits(stopbits StopBits) error {
switch stopbits {
case StopBitsOne:
t.Cflag &^= unix.CSTOPB
case StopBitsOnePointFive:
return ErrNotSupported
case StopBitsTwo:
t.Cflag |= unix.CSTOPB
default:
return ErrInvalidParam
}
return nil
}
func (t *termios) SetFlowControl(flowcontrol FlowControl) error {
if flowcontrol&FlowControlRtsCts != 0 {
t.Cflag |= termios_CRTS_IFLOW | termios_CCTS_OFLOW
} else {
t.Cflag &^= termios_CRTS_IFLOW | termios_CCTS_OFLOW
}
if flowcontrol&FlowControlDtrDsr != 0 {
t.Cflag |= termios_CDTR_IFLOW | termios_CDSR_OFLOW
} else {
t.Cflag &^= termios_CDTR_IFLOW | termios_CDSR_OFLOW
}
t.Cc[unix.VSTART] = xON
t.Cc[unix.VSTOP] = xOFF
if flowcontrol&FlowControlXonXoff != 0 {
t.Iflag |= unix.IXON | unix.IXOFF
} else {
t.Iflag &^= unix.IXON | unix.IXOFF
}
return nil
}
// timeout_ms = -1 for infinity block
// timeout_ms = 0 for non-block
// timeout_ms > 0 for pure timed read (0 to 25.5 seconds in 0.1 s intervals)
func (t *termios) SetTotalTimeout(timeout_ms int) error {
switch {
case timeout_ms == -1:
t.Cc[unix.VMIN] = 1
t.Cc[unix.VTIME] = 0
case timeout_ms == 0:
t.Cc[unix.VMIN] = 0
t.Cc[unix.VTIME] = 0
case timeout_ms > 25500:
return ErrInvalidParam
case timeout_ms > 0:
t.Cc[unix.VMIN] = 0
t.Cc[unix.VTIME] = uint8(timeout_ms / 100)
default:
return ErrInvalidParam
}
return nil
}