-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.go
204 lines (168 loc) · 4.51 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
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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"github.com/jacobsa/go-serial/serial"
)
const (
// GqGmcClearConsoleInputChars try to read that many chars to get input buffer cleared
GqGmcClearConsoleInputChars = 128
)
type (
GqGmcDevice struct {
serialPort string
serialBaudRate uint
serialDataBits uint
serialStopBits uint
interCharacterTimeout uint
minimumReadSize uint
port io.ReadWriteCloser
}
)
func NewGqGmcDevice(port string, baudRate, dataBits, stopBits, InterCharacterTimeout, minimumReadSize uint) *GqGmcDevice {
return &GqGmcDevice{
serialPort: port,
serialBaudRate: baudRate,
serialDataBits: dataBits,
serialStopBits: stopBits,
interCharacterTimeout: InterCharacterTimeout,
minimumReadSize: minimumReadSize,
}
}
func (d *GqGmcDevice) Connect() {
// Set up options.
options := serial.OpenOptions{
PortName: d.serialPort,
BaudRate: d.serialBaudRate,
DataBits: d.serialDataBits,
StopBits: d.serialStopBits,
ParityMode: serial.PARITY_NONE,
InterCharacterTimeout: d.interCharacterTimeout,
MinimumReadSize: d.minimumReadSize,
}
// Open the port.
port, err := serial.Open(options)
if err != nil {
logger.Panicf("cannot open %v: %v", d.serialPort, err)
}
d.port = port
}
func (d *GqGmcDevice) Close() error {
return d.port.Close()
}
func (d *GqGmcDevice) write(command string) error {
logger.Debugf("sending %v command", command)
command = fmt.Sprintf("<%s>>", command)
_, err := d.port.Write([]byte(command))
return err
}
func (d *GqGmcDevice) read(chars uint) ([]byte, error) {
logger.Debugf("reading %v bytes", chars)
buf := make([]byte, chars)
n, err := d.port.Read(buf)
logger.Debugf("fetched %v bytes:\n%v", len(buf), hex.Dump(buf))
if err != nil {
if !errors.Is(err, io.EOF) {
return buf, err
}
} else {
return buf[:n], nil
}
return buf, nil
}
func (d *GqGmcDevice) readString(chars uint) (string, error) {
if buf, err := d.read(chars); err == nil {
buf = bytes.Trim(buf, "\x00")
ret := strings.TrimSpace(string(buf))
return ret, nil
} else {
return "", err
}
}
func (d *GqGmcDevice) ClearSerialConsole() {
logger.Debug("clear console input")
/* #nosec G104 -- we dont care about errors here */
d.read(GqGmcClearConsoleInputChars) // nolint
}
func (d *GqGmcDevice) GetHardwareModel() (hwModelName string, hwModelVersion string) {
if err := d.write("GETVER"); err != nil {
logger.Panicf("error sending command to serial port: %v", err)
}
if val, err := d.readString(7); err == nil {
hwModelName = val
} else {
logger.Panicf("error reading from serial port: %v", err)
}
if val, err := d.readString(7); err == nil {
hwModelVersion = val
} else {
logger.Panicf("error reading from serial port: %v", err)
}
return
}
func (d *GqGmcDevice) GetHardwareSerial() (hwSerial string) {
if err := d.write("GETSERIAL"); err != nil {
logger.Panicf("error sending command to serial port: %v", err)
}
if val, err := d.readString(7); err == nil {
hwSerial = val
} else {
logger.Panicf("error reading from serial port: %v", err)
}
return
}
func (d *GqGmcDevice) GetCpm() (cpm *float64) {
if err := d.write("GETCPM"); err != nil {
logger.Panicf("error sending command to serial port: %v", err)
}
if buf, err := d.read(2); err == nil {
if len(buf) == 2 {
val := float64(binary.BigEndian.Uint16(buf))
cpm = &val
}
} else {
logger.Panicf("error reading from serial port: %v", err)
}
return
}
func (d *GqGmcDevice) GetVoltage() (voltage *float64) {
if err := d.write("GETVOLT"); err != nil {
logger.Panicf("error sending command to serial port: %v", err)
}
if buf, err := d.read(1); err == nil {
if len(buf) == 1 {
val := float64(uint(buf[0]))
voltage = &val
}
} else {
logger.Panicf("error reading from serial port: %v", err)
}
return
}
func (d *GqGmcDevice) GetTemperature() (temp *float64) {
if err := d.write("GETTEMP"); err != nil {
logger.Panicf("error sending command to serial port: %v", err)
}
if buf, err := d.read(4); err == nil {
if len(buf) == 4 {
tempInt := int(buf[0])
tempDec := int(buf[1])
tempSign := int(buf[2])
// if sign is 0, temp is greater 0 and so positive
// if sign != 0, temp is below 0 and so negative
if tempSign != 0 {
tempSign = -1
}
calcTemp := float64(tempSign*(tempInt*1000) + tempDec)
temp = &calcTemp
}
} else {
logger.Panicf("error reading from serial port: %v", err)
}
return
}