This repository has been archived by the owner on Mar 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rs232_linux.go
92 lines (87 loc) · 2.05 KB
/
rs232_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
// rs232_linux.go, jpad 2013
// linux specific implementation
// see http://ulisse.elettra.trieste.it/services/doc/serial/config.html
package rs232
import (
"fmt"
"syscall"
"unsafe"
)
const _FIONREAD = syscall.TIOCINQ
func setTermios(fd uintptr, mrate, mfmt uint, vmin, vtime uint8) error {
// terminal settings -> raw mode
termios := syscall.Termios{
Iflag: syscall.IGNPAR, // ignore framing and parity errors
Cflag: uint32(mrate) | uint32(mfmt) | syscall.CREAD | syscall.CLOCAL,
Cc: [32]uint8{
syscall.VMIN: vmin, // minimum n bytes per transfer
syscall.VTIME: vtime, // read timeout
},
Ispeed: uint32(mrate),
Ospeed: uint32(mrate),
}
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
uintptr(syscall.TCSETS), // set immediately
uintptr(unsafe.Pointer(&termios)))
if errno != 0 {
return &Error{ERR_PARAMS, fmt.Sprintf("ioctl TCSETS %d", errno)}
}
return nil
}
func bitrate2mask(rate uint32) (uint, error) {
switch rate {
case 200:
return syscall.B200, nil
case 300:
return syscall.B300, nil
case 600:
return syscall.B600, nil
case 1200:
return syscall.B1200, nil
case 1800:
return syscall.B1800, nil
case 2400:
return syscall.B2400, nil
case 4800:
return syscall.B4800, nil
case 9600:
return syscall.B9600, nil
case 19200:
return syscall.B19200, nil
case 38400:
return syscall.B38400, nil
case 57600:
return syscall.B57600, nil
case 115200:
return syscall.B115200, nil
case 230400:
return syscall.B230400, nil
case 460800:
return syscall.B460800, nil
case 500000:
return syscall.B500000, nil
case 576000:
return syscall.B576000, nil
case 921600:
return syscall.B921600, nil
case 1000000:
return syscall.B1000000, nil
case 1152000:
return syscall.B1152000, nil
case 1500000:
return syscall.B1500000, nil
case 2000000:
return syscall.B2000000, nil
case 2500000:
return syscall.B2500000, nil
case 3000000:
return syscall.B3000000, nil
case 3500000:
return syscall.B3500000, nil
case 4000000:
return syscall.B4000000, nil
}
return 0, fmt.Errorf("invalid BitRate (%d)", rate)
}