-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.go
115 lines (99 loc) · 2.36 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package serialport
type Port interface {
SetMode(mode *Mode) error
Read(p []byte) (n int, err error)
Write(p []byte) (n int, err error)
ResetInputBuffer() error
ResetOutputBuffer() error
SetDTR(dtr bool) error
SetRTS(rts bool) error
GetModemStatusBits() (*ModemStatusBits, error)
Close() error
}
type ModemStatusBits struct {
CTS bool // ClearToSend status
DSR bool // DataSetReady status
RI bool // RingIndicator status
DCD bool // DataCarrierDetect status
}
func Open(portName string, mode *Mode) (Port, error) {
return nativeOpen(portName, mode)
}
func GetPortsList() ([]string, error) {
return nativeGetPortsList()
}
type Mode struct {
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)
}
type Parity int
const (
NoParity Parity = iota
OddParity
EvenParity
MarkParity
SpaceParity
)
type StopBits int
const (
OneStopBit StopBits = iota
OnePointFiveStopBits
TwoStopBits
)
type PortError struct {
code PortErrorCode
causedBy error
}
type PortErrorCode int
const (
PortBusy PortErrorCode = iota
PortNotFound
InvalidSerialPort
PermissionDenied
InvalidSpeed
InvalidDataBits
InvalidParity
InvalidStopBits
ErrorEnumeratingPorts
PortClosed
FunctionNotImplemented
)
func (e PortError) EncodedErrorString() string {
switch e.code {
case PortBusy:
return "Serial port busy"
case PortNotFound:
return "Serial port not found"
case InvalidSerialPort:
return "Invalid serial port"
case PermissionDenied:
return "Permission denied"
case InvalidSpeed:
return "Port speed invalid or not supported"
case InvalidDataBits:
return "Port data bits invalid or not supported"
case InvalidParity:
return "Port parity invalid or not supported"
case InvalidStopBits:
return "Port stop bits invalid or not supported"
case ErrorEnumeratingPorts:
return "Could not enumerate serial ports"
case PortClosed:
return "Port has been closed"
case FunctionNotImplemented:
return "Function not implemented"
default:
return "Other error"
}
}
func (e PortError) Error() string {
if e.causedBy != nil {
return e.EncodedErrorString() + ": " + e.causedBy.Error()
}
return e.EncodedErrorString()
}
func (e PortError) Code() PortErrorCode {
return e.code
}