forked from bugst/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serial_windows.go
436 lines (390 loc) · 11.7 KB
/
serial_windows.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
//
// Copyright 2014-2020 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.
//
package serial
/*
// MSDN article on Serial Communications:
// http://msdn.microsoft.com/en-us/library/ff802693.aspx
// (alternative link) https://msdn.microsoft.com/en-us/library/ms810467.aspx
// PySerial source code and docs:
// https://github.com/pyserial/pyserial
// https://pythonhosted.org/pyserial/
// Arduino Playground article on serial communication with Windows API:
// http://playground.arduino.cc/Interfacing/CPPWindows
*/
import (
"sync"
"syscall"
)
type windowsPort struct {
handle syscall.Handle
mode *Mode
timeouts *commTimeouts
mu sync.Mutex
}
func nativeGetPortsList() ([]string, error) {
subKey, err := syscall.UTF16PtrFromString("HARDWARE\\DEVICEMAP\\SERIALCOMM\\")
if err != nil {
return nil, &PortError{code: ErrorEnumeratingPorts}
}
var h syscall.Handle
if err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, subKey, 0, syscall.KEY_READ, &h); err != nil {
if errno, isErrno := err.(syscall.Errno); isErrno && errno == syscall.ERROR_FILE_NOT_FOUND {
return []string{}, nil
}
return nil, &PortError{code: ErrorEnumeratingPorts}
}
defer syscall.RegCloseKey(h)
var valuesCount uint32
if syscall.RegQueryInfoKey(h, nil, nil, nil, nil, nil, nil, &valuesCount, nil, nil, nil, nil) != nil {
return nil, &PortError{code: ErrorEnumeratingPorts}
}
list := make([]string, valuesCount)
for i := range list {
var data [1024]uint16
dataSize := uint32(len(data))
var name [1024]uint16
nameSize := uint32(len(name))
if regEnumValue(h, uint32(i), &name[0], &nameSize, nil, nil, &data[0], &dataSize) != nil {
return nil, &PortError{code: ErrorEnumeratingPorts}
}
list[i] = syscall.UTF16ToString(data[:])
}
return list, nil
}
func (port *windowsPort) Close() error {
port.mu.Lock()
defer func() {
port.handle = syscall.InvalidHandle
port.mu.Unlock()
}()
h := port.handle
if h == syscall.InvalidHandle {
return nil
}
port.handle = syscall.InvalidHandle
return syscall.CloseHandle(h)
}
func (port *windowsPort) Read(p []byte) (int, error) {
if port.handle == syscall.InvalidHandle {
return 0, &PortError{code: PortClosed, causedBy: nil}
}
handle := port.handle
errs := new(uint32)
stat := new(comstat)
if err := clearCommError(handle, errs, stat); err != nil {
return 0, &PortError{code: InvalidSerialPort, causedBy: err}
}
size := uint32(len(p))
var readSize uint32
if port.timeouts.ReadTotalTimeoutConstant == 0 && port.timeouts.ReadTotalTimeoutMultiplier == 0 {
if stat.inque < size {
readSize = stat.inque
} else {
readSize = size
}
} else {
readSize = size
}
if readSize > 0 {
var read uint32
overlapped, err := createOverlappedStruct()
if err != nil {
return 0, &PortError{code: OsError, causedBy: err}
}
defer syscall.CloseHandle(overlapped.HEvent)
err = syscall.ReadFile(handle, p[:readSize], &read, overlapped)
if err != nil && err != syscall.ERROR_IO_PENDING {
return 0, &PortError{code: OsError, causedBy: err}
}
err = getOverlappedResult(handle, overlapped, &read, true)
if err != nil && err != syscall.ERROR_OPERATION_ABORTED {
return 0, &PortError{code: OsError, causedBy: err}
}
return int(read), nil
} else {
return 0, nil
}
}
func (port *windowsPort) Write(p []byte) (int, error) {
if port.handle == syscall.InvalidHandle {
return 0, &PortError{code: PortClosed, causedBy: nil}
}
handle := port.handle
errs := new(uint32)
stat := new(comstat)
if err := clearCommError(handle, errs, stat); err != nil {
return 0, &PortError{code: InvalidSerialPort, causedBy: err}
}
overlapped, err := createOverlappedStruct()
if err != nil {
return 0, err
}
defer syscall.CloseHandle(overlapped.HEvent)
var written uint32
err = syscall.WriteFile(handle, p, &written, overlapped)
if err == nil || err == syscall.ERROR_IO_PENDING || err == syscall.ERROR_OPERATION_ABORTED {
err = getOverlappedResult(handle, overlapped, &written, true)
if err == nil || err == syscall.ERROR_OPERATION_ABORTED {
return int(written), nil
}
}
return int(written), err
}
func (port *windowsPort) ResetInputBuffer() error {
return purgeComm(port.handle, purgeRxClear|purgeRxAbort)
}
func (port *windowsPort) ResetOutputBuffer() error {
return purgeComm(port.handle, purgeTxClear|purgeTxAbort)
}
func (port *windowsPort) SetMode(mode *Mode) error {
port.mode = mode
return port.reconfigurePort()
}
func (port *windowsPort) SetDTR(dtr bool) error {
// Like for RTS there are problems with the escapeCommFunction
// observed behaviour was that DTR is set from false -> true
// when setting RTS from true -> false
// 1) Connect -> RTS = true (low) DTR = true (low) OKAY
// 2) SetDTR(false) -> RTS = true (low) DTR = false (heigh) OKAY
// 3) SetRTS(false) -> RTS = false (heigh) DTR = true (low) ERROR: DTR toggled
//
// In addition this way the CommState Flags are not updated
/*
var res bool
if dtr {
res = escapeCommFunction(port.handle, commFunctionSetDTR)
} else {
res = escapeCommFunction(port.handle, commFunctionClrDTR)
}
if !res {
return &PortError{}
}
return nil
*/
// The following seems a more reliable way to do it
params := &dcb{}
if err := getCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
params.Flags &= dcbDTRControlDisableMask
if dtr {
params.Flags |= dcbDTRControlEnable
}
if err := setCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
return nil
}
func (port *windowsPort) SetRTS(rts bool) error {
// It seems that there is a bug in the Windows VCP driver:
// it doesn't send USB control message when the RTS bit is
// changed, so the following code not always works with
// USB-to-serial adapters.
//
// In addition this way the CommState Flags are not updated
/*
var res bool
if rts {
res = escapeCommFunction(port.handle, commFunctionSetRTS)
} else {
res = escapeCommFunction(port.handle, commFunctionClrRTS)
}
if !res {
return &PortError{}
}
return nil
*/
// The following seems a more reliable way to do it
params := &dcb{}
if err := getCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
params.Flags &= dcbRTSControlDisableMask
if rts {
params.Flags |= dcbRTSControlEnable
}
if err := setCommState(port.handle, params); err != nil {
return &PortError{causedBy: err}
}
return nil
}
func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) {
var bits uint32
if !getCommModemStatus(port.handle, &bits) {
return nil, &PortError{}
}
return &ModemStatusBits{
CTS: (bits & msCTSOn) != 0,
DCD: (bits & msRLSDOn) != 0,
DSR: (bits & msDSROn) != 0,
RI: (bits & msRingOn) != 0,
}, nil
}
func createOverlappedStruct() (*syscall.Overlapped, error) {
if h, err := createEvent(nil, true, false, nil); err == nil {
return &syscall.Overlapped{HEvent: h}, nil
} else {
return nil, err
}
}
func nativeOpen(portName string, mode *Mode) (*windowsPort, error) {
path, err := syscall.UTF16PtrFromString("\\\\.\\" + portName)
if err != nil {
return nil, err
}
handle, err := syscall.CreateFile(
path,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0, // exclusive access
nil, // no security
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL|syscall.FILE_FLAG_OVERLAPPED,
0)
if err != nil {
switch err {
case syscall.ERROR_ACCESS_DENIED:
return nil, &PortError{code: PortBusy}
case syscall.ERROR_FILE_NOT_FOUND:
return nil, &PortError{code: PortNotFound}
}
return nil, err
}
// Create the serial port
port := &windowsPort{
handle: handle,
mode: mode,
timeouts: &commTimeouts{
// Legacy initial timeouts configuration: 1 sec read timeout
ReadIntervalTimeout: 0xFFFFFFFF,
ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
ReadTotalTimeoutConstant: 1000,
WriteTotalTimeoutMultiplier: 0,
WriteTotalTimeoutConstant: 0,
},
}
// Set port parameters
if err = port.reconfigurePort(); err != nil {
port.Close()
return nil, err
}
return port, nil
}
var parityMap = map[Parity]byte{
NoParity: 0,
OddParity: 1,
EvenParity: 2,
MarkParity: 3,
SpaceParity: 4,
}
var stopBitsMap = map[StopBits]byte{
OneStopBit: 0,
OnePointFiveStopBits: 1,
TwoStopBits: 2,
}
func (port *windowsPort) reconfigurePort() error {
if err := setCommTimeouts(port.handle, port.timeouts); err != nil {
port.Close()
return &PortError{code: InvalidSerialPort, causedBy: err}
}
if err := setCommMask(port.handle, evErr); err != nil {
port.Close()
return &PortError{code: InvalidSerialPort, causedBy: err}
}
params := &dcb{}
if err := getCommState(port.handle, params); err != nil {
port.Close()
return &PortError{code: InvalidSerialPort, causedBy: err}
}
params.Flags &= dcbRTSControlDisableMask
params.Flags |= dcbRTSControlEnable
params.Flags &= dcbDTRControlDisableMask
params.Flags |= dcbDTRControlEnable
params.Flags &^= dcbOutXCTSFlow
params.Flags &^= dcbOutXDSRFlow
params.Flags &^= dcbDSRSensitivity
params.Flags |= dcbTXContinueOnXOFF
params.Flags &^= dcbInX
params.Flags &^= dcbOutX
params.Flags &^= dcbErrorChar
params.Flags &^= dcbNull
params.Flags &^= dcbAbortOnError
params.XonLim = 2048
params.XoffLim = 512
params.XonChar = 17 // DC1
params.XoffChar = 19 // C3
mode := port.mode
if mode.BaudRate == 0 {
params.BaudRate = 9600 // Default to 9600
} else {
params.BaudRate = uint32(mode.BaudRate)
}
if mode.DataBits == 0 {
params.ByteSize = 8 // Default to 8 bits
} else {
params.ByteSize = byte(mode.DataBits)
}
params.StopBits = stopBitsMap[mode.StopBits]
params.Parity = parityMap[mode.Parity]
if err := setCommState(port.handle, params); err != nil {
port.Close()
return &PortError{code: InvalidSerialPort, causedBy: err}
}
return nil
}
func (port *windowsPort) SetReadTimeout(t int) error {
switch {
case t < 0: // Block until the buffer is full.
port.timeouts.ReadIntervalTimeout = 0
port.timeouts.ReadTotalTimeoutMultiplier = 0
port.timeouts.ReadTotalTimeoutConstant = 0
case t == 0: // Return immediately with or without data.
port.timeouts.ReadIntervalTimeout = 0xFFFFFFFF
port.timeouts.ReadTotalTimeoutMultiplier = 0
port.timeouts.ReadTotalTimeoutConstant = 0
case t > 0: // Block until the buffer is full or timeout occurs.
port.timeouts.ReadIntervalTimeout = 0
port.timeouts.ReadTotalTimeoutMultiplier = 0
port.timeouts.ReadTotalTimeoutConstant = uint32(t)
}
return port.reconfigurePort()
}
func (port *windowsPort) SetReadTimeoutEx(t, i uint32) error {
port.timeouts.ReadIntervalTimeout = i
port.timeouts.ReadTotalTimeoutMultiplier = 0
port.timeouts.ReadTotalTimeoutConstant = t
return port.reconfigurePort()
}
func (port *windowsPort) SetFirstByteReadTimeout(t uint32) error {
if t > 0 && t < 0xFFFFFFFF {
port.timeouts.ReadIntervalTimeout = 0xFFFFFFFF
port.timeouts.ReadTotalTimeoutMultiplier = 0xFFFFFFFF
port.timeouts.ReadTotalTimeoutConstant = t
return port.reconfigurePort()
} else {
return &PortError{code: InvalidTimeoutValue}
}
}
func (port *windowsPort) SetWriteTimeout(t int) error {
switch {
case t < 0:
port.timeouts.WriteTotalTimeoutMultiplier = 0
port.timeouts.WriteTotalTimeoutConstant = 0
case t == 0:
port.timeouts.WriteTotalTimeoutMultiplier = 0
port.timeouts.WriteTotalTimeoutConstant = 0xFFFFFFFF
case t > 0:
port.timeouts.WriteTotalTimeoutMultiplier = 0
port.timeouts.WriteTotalTimeoutConstant = uint32(t)
}
return port.reconfigurePort()
}
// enableRS485 enables RS485 functionality of driver via an ioctl if the config says so
func (port *windowsPort) enableRS485(config *RS485Config) error {
if !config.Enabled {
return &PortError{code: NoPlatformSupportForRS485, causedBy: nil}
}
return nil
}