forked from albenik/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
termios_unix.go
130 lines (118 loc) · 3.12 KB
/
termios_unix.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
//
// Copyright 2014-2017 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
//go:build linux || darwin || freebsd || openbsd
// +build linux darwin freebsd openbsd
package serial
import (
"golang.org/x/sys/unix"
)
func (s *settings) setParity(parity Parity) error {
switch parity {
case NoParity:
s.termios.Cflag &^= unix.PARENB
s.termios.Cflag &^= unix.PARODD
s.termios.Cflag &^= tcCMSPAR
s.termios.Iflag &^= unix.INPCK
case OddParity:
s.termios.Cflag |= unix.PARENB
s.termios.Cflag |= unix.PARODD
s.termios.Cflag &^= tcCMSPAR
s.termios.Iflag |= unix.INPCK
case EvenParity:
s.termios.Cflag |= unix.PARENB
s.termios.Cflag &^= unix.PARODD
s.termios.Cflag &^= tcCMSPAR
s.termios.Iflag |= unix.INPCK
case MarkParity:
if tcCMSPAR == 0 {
return &PortError{code: InvalidParity}
}
s.termios.Cflag |= unix.PARENB
s.termios.Cflag |= unix.PARODD
s.termios.Cflag |= tcCMSPAR
s.termios.Iflag |= unix.INPCK
case SpaceParity:
if tcCMSPAR == 0 {
return &PortError{code: InvalidParity}
}
s.termios.Cflag |= unix.PARENB
s.termios.Cflag &^= unix.PARODD
s.termios.Cflag |= tcCMSPAR
s.termios.Iflag |= unix.INPCK
default:
return &PortError{code: InvalidParity}
}
return nil
}
func (s *settings) setDataBits(bits int) error {
databits, ok := databitsMap[bits]
if !ok {
return &PortError{code: InvalidDataBits}
}
// Remove previous databits setting
s.termios.Cflag &^= unix.CSIZE
// Set requested databits
s.termios.Cflag |= databits
return nil
}
func (s *settings) setStopBits(bits StopBits) error {
switch bits {
case OneStopBit:
s.termios.Cflag &^= unix.CSTOPB
case OnePointFiveStopBits:
return &PortError{code: InvalidStopBits}
case TwoStopBits:
s.termios.Cflag |= unix.CSTOPB
default:
return &PortError{code: InvalidStopBits}
}
return nil
}
func (s *settings) setCtsRts(enable bool) {
if enable {
s.termios.Cflag |= tcCRTSCTS
} else {
s.termios.Cflag &^= tcCRTSCTS
}
}
func (s *settings) setRawMode(hupcl bool) {
// Set local mode
s.termios.Cflag |= unix.CREAD
s.termios.Cflag |= unix.CLOCAL
if hupcl {
s.termios.Cflag |= unix.HUPCL
} else {
s.termios.Cflag &^= unix.HUPCL
}
// Set raw mode
s.termios.Lflag &^= unix.ICANON
s.termios.Lflag &^= unix.ECHO
s.termios.Lflag &^= unix.ECHOE
s.termios.Lflag &^= unix.ECHOK
s.termios.Lflag &^= unix.ECHONL
s.termios.Lflag &^= unix.ECHOCTL
s.termios.Lflag &^= unix.ECHOPRT
s.termios.Lflag &^= unix.ECHOKE
s.termios.Lflag &^= unix.ISIG
s.termios.Lflag &^= unix.IEXTEN
s.termios.Iflag &^= unix.IXON
s.termios.Iflag &^= unix.IXOFF
s.termios.Iflag &^= unix.IXANY
s.termios.Iflag &^= unix.INPCK
s.termios.Iflag &^= unix.IGNPAR
s.termios.Iflag &^= unix.PARMRK
s.termios.Iflag &^= unix.ISTRIP
s.termios.Iflag &^= unix.IGNBRK
s.termios.Iflag &^= unix.BRKINT
s.termios.Iflag &^= unix.INLCR
s.termios.Iflag &^= unix.IGNCR
s.termios.Iflag &^= unix.ICRNL
s.termios.Iflag &^= tcIUCLC
s.termios.Oflag &^= unix.OPOST
// Block reads until at least one char is available (no timeout)
s.termios.Cc[unix.VMIN] = 1
s.termios.Cc[unix.VTIME] = 0
}