Skip to content

Commit 883e7f0

Browse files
committed
general enhancements
1 parent 3227c71 commit 883e7f0

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

main.go

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,14 @@ import (
3030
"go.bug.st/serial"
3131
)
3232

33-
var serialSettings = &monitor.PortDescriptor{
34-
Protocol: "serial",
35-
ConfigurationParameter: map[string]*monitor.PortParameterDescriptor{
36-
"baudrate": {
37-
Label: "Baudrate",
38-
Type: "enum",
39-
Values: []string{"300", "600", "750", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "500000", "921600", "1000000", "2000000"},
40-
Selected: "9600",
41-
},
42-
"parity": {
43-
Label: "Parity",
44-
Type: "enum",
45-
Values: []string{"N", "E", "O", "M", "S"},
46-
Selected: "N",
47-
},
48-
"bits": {
49-
Label: "Data bits",
50-
Type: "enum",
51-
Values: []string{"5", "6", "7", "8", "9"},
52-
Selected: "8",
53-
},
54-
"stop_bits": {
55-
Label: "Stop bits",
56-
Type: "enum",
57-
Values: []string{"1", "1.5", "2"},
58-
Selected: "1",
59-
},
60-
},
61-
}
62-
63-
var openedPort serial.Port
64-
6533
func main() {
6634
args.Parse()
6735
if args.ShowVersion {
6836
fmt.Printf("%s\n", version.VersionInfo)
6937
return
7038
}
7139

72-
serialMonitor := &SerialMonitor{}
73-
monitorServer := monitor.NewServer(serialMonitor)
40+
monitorServer := monitor.NewServer(NewSerialMonitor())
7441
if err := monitorServer.Run(os.Stdin, os.Stdout); err != nil {
7542
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
7643
os.Exit(1)
@@ -79,7 +46,44 @@ func main() {
7946

8047
// SerialMonitor is the implementation of the serial ports pluggable-monitor
8148
type SerialMonitor struct {
82-
closeChan chan<- bool //TODO maybe useless
49+
serialPort serial.Port
50+
serialSettings *monitor.PortDescriptor
51+
openedPort bool
52+
}
53+
54+
func NewSerialMonitor() *SerialMonitor {
55+
return &SerialMonitor{
56+
serialSettings: &monitor.PortDescriptor{
57+
Protocol: "serial",
58+
ConfigurationParameter: map[string]*monitor.PortParameterDescriptor{
59+
"baudrate": {
60+
Label: "Baudrate",
61+
Type: "enum",
62+
Values: []string{"300", "600", "750", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "500000", "921600", "1000000", "2000000"},
63+
Selected: "9600",
64+
},
65+
"parity": {
66+
Label: "Parity",
67+
Type: "enum",
68+
Values: []string{"N", "E", "O", "M", "S"},
69+
Selected: "N",
70+
},
71+
"bits": {
72+
Label: "Data bits",
73+
Type: "enum",
74+
Values: []string{"5", "6", "7", "8", "9"},
75+
Selected: "8",
76+
},
77+
"stop_bits": {
78+
Label: "Stop bits",
79+
Type: "enum",
80+
Values: []string{"1", "1.5", "2"},
81+
Selected: "1",
82+
},
83+
},
84+
},
85+
openedPort: false,
86+
}
8387
}
8488

8589
// Hello is the handler for the pluggable-monitor HELLO command
@@ -89,24 +93,24 @@ func (d *SerialMonitor) Hello(userAgent string, protocol int) error {
8993

9094
// Describe is the handler for the pluggable-monitor DESCRIBE command
9195
func (d *SerialMonitor) Describe() (*monitor.PortDescriptor, error) {
92-
return serialSettings, nil
96+
return d.serialSettings, nil
9397
}
9498

9599
// Configure is the handler for the pluggable-monitor CONFIGURE command
96100
func (d *SerialMonitor) Configure(parameterName string, value string) error {
97-
if serialSettings.ConfigurationParameter[parameterName] == nil {
101+
if d.serialSettings.ConfigurationParameter[parameterName] == nil {
98102
return fmt.Errorf("could not find parameter named %s", parameterName)
99103
}
100-
values := serialSettings.ConfigurationParameter[parameterName].Values
104+
values := d.serialSettings.ConfigurationParameter[parameterName].Values
101105
for _, i := range values {
102106
if i == value {
103-
if openedPort != nil {
104-
err := openedPort.SetMode(getMode())
107+
if d.openedPort {
108+
err := d.serialPort.SetMode(d.getMode())
105109
if err != nil {
106110
return errors.New(err.Error())
107111
}
108112
}
109-
serialSettings.ConfigurationParameter[parameterName].Selected = value
113+
d.serialSettings.ConfigurationParameter[parameterName].Selected = value
110114
return nil
111115
}
112116
}
@@ -115,35 +119,36 @@ func (d *SerialMonitor) Configure(parameterName string, value string) error {
115119

116120
// Open is the handler for the pluggable-monitor OPEN command
117121
func (d *SerialMonitor) Open(boardPort string) (io.ReadWriter, error) {
118-
if openedPort != nil {
122+
if d.openedPort {
119123
return nil, fmt.Errorf("port already opened: %s", boardPort)
120124
}
121-
openedPort, err := serial.Open(boardPort, getMode())
125+
serialPort, err := serial.Open(boardPort, d.getMode())
122126
if err != nil {
123-
openedPort = nil
124127
return nil, err
125128

126129
}
127-
return openedPort, nil
130+
d.openedPort = true
131+
d.serialPort = serialPort
132+
return d.serialPort, nil
128133
}
129134

130135
// Close is the handler for the pluggable-monitor CLOSE command
131136
func (d *SerialMonitor) Close() error {
132-
if openedPort == nil {
137+
if !d.openedPort {
133138
return errors.New("port already closed")
134139
}
135-
openedPort.Close()
136-
openedPort = nil
140+
d.serialPort.Close()
141+
d.openedPort = false
137142
return nil
138143
}
139144

140145
// Quit is the handler for the pluggable-monitor QUIT command
141146
func (d *SerialMonitor) Quit() {}
142147

143-
func getMode() *serial.Mode {
144-
baud, _ := strconv.Atoi(serialSettings.ConfigurationParameter["baudrate"].Selected)
148+
func (d *SerialMonitor) getMode() *serial.Mode {
149+
baud, _ := strconv.Atoi(d.serialSettings.ConfigurationParameter["baudrate"].Selected)
145150
var parity serial.Parity
146-
switch serialSettings.ConfigurationParameter["parity"].Selected {
151+
switch d.serialSettings.ConfigurationParameter["parity"].Selected {
147152
case "N":
148153
parity = serial.NoParity
149154
case "E":
@@ -155,9 +160,9 @@ func getMode() *serial.Mode {
155160
case "S":
156161
parity = serial.SpaceParity
157162
}
158-
dataBits, _ := strconv.Atoi(serialSettings.ConfigurationParameter["bits"].Selected)
163+
dataBits, _ := strconv.Atoi(d.serialSettings.ConfigurationParameter["bits"].Selected)
159164
var stopBits serial.StopBits
160-
switch serialSettings.ConfigurationParameter["stop_bits"].Selected {
165+
switch d.serialSettings.ConfigurationParameter["stop_bits"].Selected {
161166
case "1":
162167
stopBits = serial.OneStopBit
163168
case "1.5":

0 commit comments

Comments
 (0)