forked from albenik/go-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.go
93 lines (80 loc) · 2.33 KB
/
serial.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
//
// Copyright 2014-2018 Cristian Maglie.
// Copyright 2019-2022 Veniamin Albaev <[email protected]>.
// 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
import (
"os"
)
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go syscall_windows.go
const (
// NoParity disable parity control (default).
NoParity Parity = iota
// OddParity enable odd-parity check.
OddParity
// EvenParity enable even-parity check.
EvenParity
// MarkParity enable mark-parity (always 1) check.
MarkParity
// SpaceParity enable space-parity (always 0) check.
SpaceParity
// OneStopBit sets 1 stop bit (default).
OneStopBit StopBits = iota
// OnePointFiveStopBits sets 1.5 stop bits.
OnePointFiveStopBits
// TwoStopBits sets 2 stop bits.
TwoStopBits
)
// StopBits describe a serial port stop bits setting.
type StopBits int
// Parity describes a serial port parity setting.
type Parity int
// ModemStatusBits contains all the modem status bits for a serial port (CTS, DSR, etc...).
// It can be retrieved with the Port.GetModemStatusBits() method.
type ModemStatusBits struct {
CTS bool // ClearToSend status
DSR bool // DataSetReady status
RI bool // RingIndicator status
DCD bool // DataCarrierDetect status
}
// Port is the interface for a serial Port.
type Port struct {
name string
opened bool
baudRate int // The serial port bitrate (aka Baudrate)
dataBits int // Size of the character (must be 5, 6, 7 or 8)
parity Parity // Parity (see Parity type for more info)
stopBits StopBits // Stop bits (see StopBits type for more info)
hupcl bool // Lower DTR line on close (hang up)
internal *port // os specific (implementation like os.File)
}
func (p *Port) String() string {
if p == nil {
return "Error: <nil> port instance"
}
return p.name
}
func (p *Port) checkValid() error {
if p == nil || p.internal == nil || !isHandleValid(p.internal.handle) {
return &PortError{code: PortClosed, wrapped: os.ErrInvalid}
}
if !p.opened {
return &PortError{code: PortClosed}
}
return nil
}
func newWithDefaults(n string, p *port) *Port {
return &Port{
name: n,
opened: true,
baudRate: 9600,
dataBits: 8,
parity: NoParity,
stopBits: OneStopBit,
hupcl: false,
internal: p,
}
}