-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
232 lines (213 loc) · 6.11 KB
/
main.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
//
// This file is part of serial-monitor.
//
// Copyright 2018-2021 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
//
// Package main implements the serial monitor
package main
import (
"errors"
"fmt"
"io"
"os"
"strconv"
monitor "github.com/arduino/pluggable-monitor-protocol-handler"
"github.com/arduino/serial-monitor/args"
"github.com/arduino/serial-monitor/version"
"go.bug.st/serial"
"golang.org/x/exp/slices"
)
func main() {
args.Parse()
if args.ShowVersion {
fmt.Printf("%s\n", version.VersionInfo)
return
}
monitorServer := monitor.NewServer(NewSerialMonitor())
if err := monitorServer.Run(os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
}
// SerialMonitor is the implementation of the serial ports pluggable-monitor
type SerialMonitor struct {
serialPort serial.Port
serialSettings *monitor.PortDescriptor
openedPort bool
}
// NewSerialMonitor will initialize and return a SerialMonitor
func NewSerialMonitor() *SerialMonitor {
return &SerialMonitor{
serialSettings: &monitor.PortDescriptor{
Protocol: "serial",
ConfigurationParameter: map[string]*monitor.PortParameterDescriptor{
"baudrate": {
Label: "Baudrate",
Type: "enum",
Values: []string{
"300", "600", "750",
"1200", "2400", "4800", "9600",
"19200", "31250", "38400", "57600", "74880",
"115200", "230400", "250000", "460800", "500000", "921600",
"1000000", "2000000"},
Selected: "9600",
},
"parity": {
Label: "Parity",
Type: "enum",
Values: []string{"none", "even", "odd", "mark", "space"},
Selected: "none",
},
"bits": {
Label: "Data bits",
Type: "enum",
Values: []string{"5", "6", "7", "8", "9"},
Selected: "8",
},
"stop_bits": {
Label: "Stop bits",
Type: "enum",
Values: []string{"1", "1.5", "2"},
Selected: "1",
},
"rts": {
Label: "RTS",
Type: "enum",
Values: []string{"on", "off"},
Selected: "on",
},
"dtr": {
Label: "DTR",
Type: "enum",
Values: []string{"on", "off"},
Selected: "on",
},
},
},
openedPort: false,
}
}
// Hello is the handler for the pluggable-monitor HELLO command
// revive:disable:unused-parameter
func (d *SerialMonitor) Hello(userAgent string, protocol int) error {
return nil
}
// Describe is the handler for the pluggable-monitor DESCRIBE command
func (d *SerialMonitor) Describe() (*monitor.PortDescriptor, error) {
return d.serialSettings, nil
}
// Configure is the handler for the pluggable-monitor CONFIGURE command
func (d *SerialMonitor) Configure(parameterName string, value string) error {
parameter, ok := d.serialSettings.ConfigurationParameter[parameterName]
if !ok {
return fmt.Errorf("could not find parameter named %s", parameterName)
}
if !slices.Contains(parameter.Values, value) {
return fmt.Errorf("invalid value for parameter %s: %s", parameterName, value)
}
// Set configuration
oldValue := parameter.Selected
parameter.Selected = value
// Apply configuration to port
var configErr error
if d.openedPort {
switch parameterName {
case "baudrate", "parity", "bits", "stop_bits":
configErr = d.serialPort.SetMode(d.getMode())
case "dtr":
configErr = d.serialPort.SetDTR(d.getDTR())
case "rts":
configErr = d.serialPort.SetRTS(d.getRTS())
default:
// Should never happen
panic("Invalid parameter: " + parameterName)
}
}
// If configuration failed, rollback settings
if configErr != nil {
parameter.Selected = oldValue
return configErr
}
return nil
}
// Open is the handler for the pluggable-monitor OPEN command
func (d *SerialMonitor) Open(boardPort string) (io.ReadWriter, error) {
if d.openedPort {
return nil, fmt.Errorf("port already opened: %s", boardPort)
}
serialPort, err := serial.Open(boardPort, d.getMode())
if err != nil {
return nil, err
}
d.openedPort = true
d.serialPort = serialPort
return d.serialPort, nil
}
// Close is the handler for the pluggable-monitor CLOSE command
func (d *SerialMonitor) Close() error {
if !d.openedPort {
return errors.New("port already closed")
}
err := d.serialPort.Close()
if err != nil {
return err
}
d.openedPort = false
return nil
}
// Quit is the handler for the pluggable-monitor QUIT command
func (d *SerialMonitor) Quit() {}
func (d *SerialMonitor) getMode() *serial.Mode {
baud, _ := strconv.Atoi(d.serialSettings.ConfigurationParameter["baudrate"].Selected)
var parity serial.Parity
switch d.serialSettings.ConfigurationParameter["parity"].Selected {
case "None":
parity = serial.NoParity
case "Even":
parity = serial.EvenParity
case "Odd":
parity = serial.OddParity
case "Mark":
parity = serial.MarkParity
case "Space":
parity = serial.SpaceParity
}
dataBits, _ := strconv.Atoi(d.serialSettings.ConfigurationParameter["bits"].Selected)
var stopBits serial.StopBits
switch d.serialSettings.ConfigurationParameter["stop_bits"].Selected {
case "1":
stopBits = serial.OneStopBit
case "1.5":
stopBits = serial.OnePointFiveStopBits
case "2":
stopBits = serial.TwoStopBits
}
mode := &serial.Mode{
BaudRate: baud,
Parity: parity,
DataBits: dataBits,
StopBits: stopBits,
InitialStatusBits: &serial.ModemOutputBits{
DTR: d.getDTR(),
RTS: d.getRTS(),
},
}
return mode
}
func (d *SerialMonitor) getDTR() bool {
return d.serialSettings.ConfigurationParameter["dtr"].Selected == "on"
}
func (d *SerialMonitor) getRTS() bool {
return d.serialSettings.ConfigurationParameter["rts"].Selected == "on"
}