forked from dustin/go-rs232
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsio.go
248 lines (209 loc) · 5.96 KB
/
sio.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
// +build linux
/*
1) Original: Copyright (c) 2005-2008 Dustin Sallings <[email protected]>.
2) Mods: Copyright (c) 2012 Schleibinger Geräte Teubert u. Greim GmbH
<[email protected]>. Blame: Jan Mercl
All rights reserved. Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file.
*/
// Package sio supports communication using a serial port. Currently works only
// on Linux. Cgo is not used.
package sio
import (
"os"
"syscall"
"time"
"unsafe"
)
// Addr represents a network end point address.
type Addr interface {
Network() string // name of the network
String() string // string form of address
}
type addr struct {
net string
str string
}
// Implementation of Addr
func (a *addr) Network() string {
return a.net
}
// Implementation of Addr
func (a *addr) String() string {
return a.str
}
type Port struct {
f *os.File
a *addr
}
// Open returns a Port implementing net.Conn or an error if any. The Port
// behavior is like of the merged returns of net.DialTCP and
// net.ListenTCP.Accept, i.e. the net.Conn represents a bidirectional byte
// stream. The only supported mode ATM is 8N1. The serial line is put into raw
// mode (e.g. no HW nor XON/XOFF flow control).
//
// Ex.: sio.Open("/dev/ttyS0", syscall.B115200)
func Open(dev string, rate uint32) (p *Port, err error) {
var f *os.File
defer func() {
if err != nil && f != nil {
f.Close()
}
}()
f, err = os.OpenFile(dev, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NDELAY, 0666)
if err != nil {
return nil, err
}
fd := f.Fd()
t := syscall.Termios{
Iflag: syscall.IGNPAR,
Cflag: syscall.CS8 | syscall.CREAD | syscall.CLOCAL | rate,
Cc: [32]uint8{syscall.VMIN: 1},
Ispeed: rate,
Ospeed: rate,
}
if _, _, errno := syscall.Syscall6(
syscall.SYS_IOCTL,
uintptr(fd),
uintptr(syscall.TCSETS),
uintptr(unsafe.Pointer(&t)),
0,
0,
0,
); errno != 0 {
return nil, errno
}
if err = syscall.SetNonblock(int(fd), false); err != nil {
return
}
return &Port{f, &addr{dev, dev}}, nil
}
// Implementation of net.Conn
func (p *Port) Read(b []byte) (n int, err error) {
return p.f.Read(b)
}
// Implementation of net.Conn
func (p *Port) Write(b []byte) (n int, err error) {
return p.f.Write(b)
}
// Implementation of net.Conn
func (p *Port) Close() error {
return p.f.Close()
}
// Implementation of net.Conn
func (p *Port) LocalAddr() Addr {
return p.a
}
// Implementation of net.Conn
func (p *Port) RemoteAddr() Addr {
return &addr{} // Ignored
}
// Implementation of net.Conn
func (p *Port) SetDeadline(t time.Time) error {
return nil // Ignored
}
// Implementation of net.Conn
func (p *Port) SetReadDeadline(t time.Time) error {
return nil // Ignored
}
// Implementation of net.Conn
func (p *Port) SetWriteDeadline(t time.Time) error {
return nil // Ignored
}
func (p *Port) setCtrlSignal(sig int, on bool) (err error) {
var state int
fd := p.f.Fd()
if _, _, errno := syscall.Syscall6(
syscall.SYS_IOCTL,
uintptr(fd),
uintptr(syscall.TIOCMGET),
uintptr(unsafe.Pointer(&state)),
0,
0,
0,
); errno != 0 {
return errno
}
switch on {
case true:
state |= sig
case false:
state &^= sig
}
if _, _, errno := syscall.Syscall6(
syscall.SYS_IOCTL,
uintptr(fd),
uintptr(syscall.TIOCMSET),
uintptr(unsafe.Pointer(&state)),
0,
0,
0,
); errno != 0 {
err = errno
}
return
}
func (p *Port) getCtrlSignal(sig int) (on bool, err error) {
var state int
if _, _, errno := syscall.Syscall6(
syscall.SYS_IOCTL,
uintptr(p.f.Fd()),
uintptr(syscall.TIOCMGET),
uintptr(unsafe.Pointer(&state)),
0,
0,
0,
); errno != 0 {
return false, errno
}
on = (state & sig) != 0
return
}
// GetDTR return the state of p's DTR or an error if any. Depending on the
// setup this signal may have the opposite direction than expected. In such
// case this function should not be used.
func (p *Port) GetDTR() (on bool, err error) {
return p.getCtrlSignal(syscall.TIOCM_DTR)
}
// GetDSR return the state of p's DSR or an error if any. Depending on the
// setup this signal may have the opposite direction than expected. In such
// case this function should not be used.
func (p *Port) GetDSR() (on bool, err error) {
return p.getCtrlSignal(syscall.TIOCM_DSR)
}
// GetCTS return the state of p's CTS or an error if any. Depending on the
// setup this signal may have the opposite direction than expected. In such
// case this function should not be used.
func (p *Port) GetCTS() (on bool, err error) {
return p.getCtrlSignal(syscall.TIOCM_CTS)
}
// GetRTS return the state of p's RTS or an error if any. Depending on the
// setup this signal may have the opposite direction than expected. In such
// case this function should not be used.
func (p *Port) GetRTS() (on bool, err error) {
return p.getCtrlSignal(syscall.TIOCM_RTS)
}
// SetDTR sets the state of p's DTR to `on`. A non nil error is returned on
// failure. Depending on the setup this signal may have the opposite direction
// than expected. In such case this function should not be used.
func (p *Port) SetDTR(on bool) error {
return p.setCtrlSignal(syscall.TIOCM_DTR, on)
}
// SetDSR sets the state of p's DSR to `on`. A non nil error is returned on
// failure. Depending on the setup this signal may have the opposite direction
// than expected. In such case this function should not be used.
func (p *Port) SetDSR(on bool) error {
return p.setCtrlSignal(syscall.TIOCM_DSR, on)
}
// SetCTS sets the state of p's CTS to `on`. A non nil error is returned on
// failure. Depending on the setup this signal may have the opposite direction
// than expected. In such case this function should not be used.
func (p *Port) SetCTS(on bool) error {
return p.setCtrlSignal(syscall.TIOCM_CTS, on)
}
// SetRTS sets the state of p's RTS to `on`. A non nil error is returned on
// failure. Depending on the setup this signal may have the opposite direction
// than expected. In such case this function should not be used.
func (p *Port) SetRTS(on bool) error {
return p.setCtrlSignal(syscall.TIOCM_RTS, on)
}