-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsers.go
155 lines (131 loc) · 3.41 KB
/
sers.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
// Copyright 2012 Michael Meier. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package sers offers serial port access. It is a stated goal of this
// package to allow for non-standard bit rates as the may be useful
// in a wide range of embedded projects.
package sers
import (
"fmt"
"io"
)
const (
N = 0 // no parity
E = 1 // even parity
O = 2 // odd parity
)
const (
NO_HANDSHAKE = 0
RTSCTS_HANDSHAKE = 1
)
// Serialport represents a serial port and offers configuration of baud
// rate, frame format, handshaking and read paramters as well as setting and
// clearing break conditions.
type SerialPort interface {
io.Reader
io.Writer
io.Closer
// SetMode sets the frame format and handshaking configuration.
// baudrate may be freely chosen, the driver is allowed to reject
// unachievable baud rates. databits may be any number of data bits
// supported by the driver. parity is one of (N|O|E) for none, odd
// or even parity. handshake is either NO_HANDSHAKE or
// RTSCTS_HANDSHAKE.
//
// Known bug on Windows: Only NO_HANDSHAKE is supported.
SetMode(baudrate, databits, parity, stopbits, handshake int) error
// GetMode retrieves the current mode settings.
//
// Known bug on OS X: GetMode only works after SetMode has been called
// before. If not, it returns an error.
GetMode() (Mode, error)
// SetReadParams sets the minimum number of bytes to read and a read
// timeout in seconds. These parameters roughly correspond to the
// UNIX termios concepts of VMIN and VTIME.
SetReadParams(minread int, timeout float64) error
// SetBreak turns on the generation of a break condition if on == true,
// otherwise it clear the break condition.
SetBreak(on bool) error
}
func SetModeStruct(sp SerialPort, mode Mode) error {
return sp.SetMode(mode.Baudrate, mode.DataBits, mode.Parity, mode.Stopbits, mode.Handshake)
}
type Mode struct {
Baudrate int
DataBits int
Parity int
Stopbits int
Handshake int
}
func (m Mode) Valid() bool {
if m.Baudrate < 0 {
return false
}
if m.DataBits < 5 || m.DataBits > 8 {
return false
}
if !(m.Parity == N || m.Parity == O || m.Parity == E) {
return false
}
if !(m.Stopbits == 1 || m.Stopbits == 2) {
return false
}
if !(m.Handshake == NO_HANDSHAKE || m.Handshake == RTSCTS_HANDSHAKE) {
return false
}
return true
}
func (m Mode) String() string {
if !m.Valid() {
return fmt.Sprintf("invalid_mode(%d,%d,%d,%d,%d)",
m.Baudrate,
m.DataBits,
m.Parity,
m.Stopbits,
m.Handshake)
}
parstring := ""
switch m.Parity {
case N:
parstring = "n"
case O:
parstring = "o"
case E:
parstring = "e"
default:
panic("unhandled parity setting")
}
hsstring := ""
switch m.Handshake {
case NO_HANDSHAKE:
hsstring = "none"
case RTSCTS_HANDSHAKE:
hsstring = "rtscts"
default:
panic("unhandled handshake setting")
}
return fmt.Sprintf("%d,%d%s%d,%s",
m.Baudrate,
m.DataBits,
parstring,
m.Stopbits,
hsstring)
}
type StringError string
func (se StringError) Error() string {
return string(se)
}
type ParameterError struct {
Parameter string
Reason string
}
func (pe *ParameterError) Error() string {
return fmt.Sprintf("error in parameter '%s': %s", pe.Parameter, pe.Reason)
}
type Error struct {
Operation string
UnderlyingError error
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %v", e.Operation, e.UnderlyingError)
}