Skip to content

Commit 4a7b7a0

Browse files
committed
add first implementation
1 parent 3285564 commit 4a7b7a0

File tree

9 files changed

+118
-693
lines changed

9 files changed

+118
-693
lines changed

go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ module github.com/arduino/serial-monitor
22

33
require (
44
github.com/arduino/go-paths-helper v1.6.1 // indirect
5-
github.com/arduino/go-properties-orderedmap v1.6.0
6-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.0.1
5+
github.com/arduino/go-properties-orderedmap v1.6.0 // indirect
6+
github.com/arduino/pluggable-monitor-protocol-handler v0.9.0
77
github.com/davecgh/go-spew v1.1.1 // indirect
8-
github.com/s-urbaniak/uevent v1.0.1
9-
go.bug.st/serial v1.3.1
10-
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
8+
go.bug.st/serial v1.3.2
119
)
1210

1311
go 1.16

go.sum

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@ github.com/arduino/go-paths-helper v1.6.1/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4
44
github.com/arduino/go-properties-orderedmap v1.4.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
55
github.com/arduino/go-properties-orderedmap v1.6.0 h1:gp2JoWRETtqwsZ+UHu/PBuYWYH2x2+d+uipDxS4WmvM=
66
github.com/arduino/go-properties-orderedmap v1.6.0/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
7-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.0.1 h1:THeCPZWeGfnvtPvxAiCqgEdRhdfERgycHd35NPP9UMQ=
8-
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.0.1/go.mod h1:tQXPIAEHcc5k8HBsufLsWmju0D3WiJt07j+dFT55NGY=
7+
github.com/arduino/pluggable-monitor-protocol-handler v0.9.0 h1:N2wk5D//4wVQIGVtyLH2wMufJa6+q9bl0P1/G3DKYXM=
8+
github.com/arduino/pluggable-monitor-protocol-handler v0.9.0/go.mod h1:nWW6xPvKZH2PRGaIxolKOcYVBuh5mOCam3avYaaH7No=
99
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
1010
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1212
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
15+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
16+
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
17+
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
1418
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1519
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1620
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1721
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
18-
github.com/s-urbaniak/uevent v1.0.1 h1:Zy6g16gR1m6iRB+scDtyExP9KRYnuSdTWOYhBE5O620=
19-
github.com/s-urbaniak/uevent v1.0.1/go.mod h1:h83SVc1NQj/EubPaK4hWyiG217xLYr+fS+Jf10COrG0=
2022
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2123
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2224
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2325
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
24-
go.bug.st/serial v1.3.1 h1:ziLU+w7RzBZKMGacM/6iY6P7bhxsoyaoVLWcfAdt6w4=
25-
go.bug.st/serial v1.3.1/go.mod h1:8TT7u/SwwNIpJ8QaG4s+HTjFt9ReXs2cdOU7ZEk50Dk=
26-
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27-
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
28-
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26+
go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
27+
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
28+
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
29+
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2930
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3031
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
3132
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,141 @@
1818
package main
1919

2020
import (
21+
"errors"
2122
"fmt"
23+
"io"
2224
"os"
25+
"strconv"
2326

24-
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
27+
monitor "github.com/arduino/pluggable-monitor-protocol-handler"
2528
"github.com/arduino/serial-monitor/args"
26-
"github.com/arduino/serial-monitor/sync"
2729
"github.com/arduino/serial-monitor/version"
30+
"go.bug.st/serial"
2831
)
2932

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+
3065
func main() {
3166
args.Parse()
3267
if args.ShowVersion {
3368
fmt.Printf("%s\n", version.VersionInfo)
3469
return
3570
}
3671

37-
serialDisc := &SerialDiscovery{}
38-
disc := discovery.NewServer(serialDisc)
39-
if err := disc.Run(os.Stdin, os.Stdout); err != nil {
72+
serialMonitor := &SerialMonitor{}
73+
monitorServer := monitor.NewServer(serialMonitor)
74+
if err := monitorServer.Run(os.Stdin, os.Stdout); err != nil {
4075
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
4176
os.Exit(1)
4277
}
4378
}
4479

45-
// SerialDiscovery is the implementation of the serial ports pluggable-discovery
46-
type SerialDiscovery struct {
47-
closeChan chan<- bool
80+
// SerialMonitor is the implementation of the serial ports pluggable-monitor
81+
type SerialMonitor struct {
82+
closeChan chan<- bool //TODO maybe useless
4883
}
4984

50-
// Hello is the handler for the pluggable-discovery HELLO command
51-
func (d *SerialDiscovery) Hello(userAgent string, protocolVersion int) error {
85+
// Hello is the handler for the pluggable-monitor HELLO command
86+
func (d *SerialMonitor) Hello(userAgent string, protocol int) error {
5287
return nil
5388
}
5489

55-
// Quit is the handler for the pluggable-discovery QUIT command
56-
func (d *SerialDiscovery) Quit() {
90+
// Describe is the handler for the pluggable-monitor DESCRIBE command
91+
func (d *SerialMonitor) Describe() (*monitor.PortDescriptor, error) {
92+
return serialSettings, nil
5793
}
5894

59-
// Stop is the handler for the pluggable-discovery STOP command
60-
func (d *SerialDiscovery) Stop() error {
61-
if d.closeChan != nil {
62-
d.closeChan <- true
63-
close(d.closeChan)
64-
d.closeChan = nil
95+
// Configure is the handler for the pluggable-monitor CONFIGURE command
96+
func (d *SerialMonitor) Configure(parameterName string, value string) error {
97+
if serialSettings.ConfigurationParameter[parameterName] == nil {
98+
return fmt.Errorf("could not find parameter named %s", parameterName)
6599
}
66-
return nil
100+
values := serialSettings.ConfigurationParameter[parameterName].Values
101+
for _, i := range values {
102+
if i == value {
103+
serialSettings.ConfigurationParameter[parameterName].Selected = value
104+
if openedPort != nil {
105+
err := openedPort.SetMode(getMode())
106+
if err != nil {
107+
return errors.New(err.Error())
108+
109+
}
110+
}
111+
return nil
112+
}
113+
}
114+
return fmt.Errorf("invalid value for parameter %s: %s", parameterName, value)
67115
}
68116

69-
// StartSync is the handler for the pluggable-discovery START_SYNC command
70-
func (d *SerialDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
71-
close, err := sync.Start(eventCB, errorCB)
117+
// Open is the handler for the pluggable-monitor OPEN command
118+
func (d *SerialMonitor) Open(boardPort string) (io.ReadWriter, error) {
119+
if openedPort != nil {
120+
return nil, fmt.Errorf("port already opened: %s", boardPort)
121+
}
122+
openedPort, err := serial.Open(boardPort, getMode())
72123
if err != nil {
73-
return err
124+
fmt.Print(boardPort)
125+
openedPort = nil
126+
return nil, errors.New(err.Error())
127+
74128
}
75-
d.closeChan = close
129+
return openedPort, nil
130+
}
131+
132+
// Close is the handler for the pluggable-monitor CLOSE command
133+
func (d *SerialMonitor) Close() error {
134+
if openedPort == nil {
135+
return errors.New("port already closed")
136+
}
137+
openedPort.Close()
138+
openedPort = nil
76139
return nil
77140
}
141+
142+
// Quit is the handler for the pluggable-monitor QUIT command
143+
func (d *SerialMonitor) Quit() {}
144+
145+
func getMode() *serial.Mode {
146+
baud, _ := strconv.Atoi(serialSettings.ConfigurationParameter["baudrate"].Selected)
147+
parity, _ := strconv.Atoi(serialSettings.ConfigurationParameter["parity"].Selected)
148+
dataBits, _ := strconv.Atoi(serialSettings.ConfigurationParameter["bits"].Selected)
149+
stopBits, _ := strconv.Atoi(serialSettings.ConfigurationParameter["stop_bits"].Selected)
150+
151+
mode := &serial.Mode{
152+
BaudRate: baud,
153+
Parity: serial.Parity(parity),
154+
DataBits: dataBits,
155+
StopBits: serial.StopBits(stopBits),
156+
}
157+
return mode
158+
}

sync/sync.go

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)