@@ -30,47 +30,14 @@ import (
30
30
"go.bug.st/serial"
31
31
)
32
32
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
-
65
33
func main () {
66
34
args .Parse ()
67
35
if args .ShowVersion {
68
36
fmt .Printf ("%s\n " , version .VersionInfo )
69
37
return
70
38
}
71
39
72
- serialMonitor := & SerialMonitor {}
73
- monitorServer := monitor .NewServer (serialMonitor )
40
+ monitorServer := monitor .NewServer (NewSerialMonitor ())
74
41
if err := monitorServer .Run (os .Stdin , os .Stdout ); err != nil {
75
42
fmt .Fprintf (os .Stderr , "Error: %s\n " , err .Error ())
76
43
os .Exit (1 )
@@ -79,7 +46,44 @@ func main() {
79
46
80
47
// SerialMonitor is the implementation of the serial ports pluggable-monitor
81
48
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
+ }
83
87
}
84
88
85
89
// Hello is the handler for the pluggable-monitor HELLO command
@@ -89,24 +93,24 @@ func (d *SerialMonitor) Hello(userAgent string, protocol int) error {
89
93
90
94
// Describe is the handler for the pluggable-monitor DESCRIBE command
91
95
func (d * SerialMonitor ) Describe () (* monitor.PortDescriptor , error ) {
92
- return serialSettings , nil
96
+ return d . serialSettings , nil
93
97
}
94
98
95
99
// Configure is the handler for the pluggable-monitor CONFIGURE command
96
100
func (d * SerialMonitor ) Configure (parameterName string , value string ) error {
97
- if serialSettings .ConfigurationParameter [parameterName ] == nil {
101
+ if d . serialSettings .ConfigurationParameter [parameterName ] == nil {
98
102
return fmt .Errorf ("could not find parameter named %s" , parameterName )
99
103
}
100
- values := serialSettings .ConfigurationParameter [parameterName ].Values
104
+ values := d . serialSettings .ConfigurationParameter [parameterName ].Values
101
105
for _ , i := range values {
102
106
if i == value {
103
- if openedPort != nil {
104
- err := openedPort . SetMode (getMode ())
107
+ if d . openedPort {
108
+ err := d . serialPort . SetMode (d . getMode ())
105
109
if err != nil {
106
110
return errors .New (err .Error ())
107
111
}
108
112
}
109
- serialSettings .ConfigurationParameter [parameterName ].Selected = value
113
+ d . serialSettings .ConfigurationParameter [parameterName ].Selected = value
110
114
return nil
111
115
}
112
116
}
@@ -115,35 +119,36 @@ func (d *SerialMonitor) Configure(parameterName string, value string) error {
115
119
116
120
// Open is the handler for the pluggable-monitor OPEN command
117
121
func (d * SerialMonitor ) Open (boardPort string ) (io.ReadWriter , error ) {
118
- if openedPort != nil {
122
+ if d . openedPort {
119
123
return nil , fmt .Errorf ("port already opened: %s" , boardPort )
120
124
}
121
- openedPort , err := serial .Open (boardPort , getMode ())
125
+ serialPort , err := serial .Open (boardPort , d . getMode ())
122
126
if err != nil {
123
- openedPort = nil
124
127
return nil , err
125
128
126
129
}
127
- return openedPort , nil
130
+ d .openedPort = true
131
+ d .serialPort = serialPort
132
+ return d .serialPort , nil
128
133
}
129
134
130
135
// Close is the handler for the pluggable-monitor CLOSE command
131
136
func (d * SerialMonitor ) Close () error {
132
- if openedPort == nil {
137
+ if ! d . openedPort {
133
138
return errors .New ("port already closed" )
134
139
}
135
- openedPort .Close ()
136
- openedPort = nil
140
+ d . serialPort .Close ()
141
+ d . openedPort = false
137
142
return nil
138
143
}
139
144
140
145
// Quit is the handler for the pluggable-monitor QUIT command
141
146
func (d * SerialMonitor ) Quit () {}
142
147
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 )
145
150
var parity serial.Parity
146
- switch serialSettings .ConfigurationParameter ["parity" ].Selected {
151
+ switch d . serialSettings .ConfigurationParameter ["parity" ].Selected {
147
152
case "N" :
148
153
parity = serial .NoParity
149
154
case "E" :
@@ -155,9 +160,9 @@ func getMode() *serial.Mode {
155
160
case "S" :
156
161
parity = serial .SpaceParity
157
162
}
158
- dataBits , _ := strconv .Atoi (serialSettings .ConfigurationParameter ["bits" ].Selected )
163
+ dataBits , _ := strconv .Atoi (d . serialSettings .ConfigurationParameter ["bits" ].Selected )
159
164
var stopBits serial.StopBits
160
- switch serialSettings .ConfigurationParameter ["stop_bits" ].Selected {
165
+ switch d . serialSettings .ConfigurationParameter ["stop_bits" ].Selected {
161
166
case "1" :
162
167
stopBits = serial .OneStopBit
163
168
case "1.5" :
0 commit comments