Rename msgpackpp->msgpack, merge device+protocol->wire, absorb picoserial into client
This commit is contained in:
53
lib/client/serial.go
Normal file
53
lib/client/serial.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"go.bug.st/serial"
|
||||
"go.bug.st/serial/enumerator"
|
||||
)
|
||||
|
||||
func ListSerial() ([]string, error) {
|
||||
ports, err := enumerator.GetDetailedPortsList()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("enumerating ports: %w", err)
|
||||
}
|
||||
var result []string
|
||||
for _, p := range ports {
|
||||
if p.IsUSB {
|
||||
result = append(result, p.Name)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type serialTransport struct {
|
||||
port serial.Port
|
||||
}
|
||||
|
||||
func NewSerial(portName string, timeout time.Duration) (*Client, error) {
|
||||
port, err := serial.Open(portName, &serial.Mode{BaudRate: 115200})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("opening %s: %w", portName, err)
|
||||
}
|
||||
return &Client{transport: &serialTransport{port: port}, timeout: timeout}, nil
|
||||
}
|
||||
|
||||
func (t *serialTransport) Send(data []byte) error {
|
||||
_, err := t.port.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *serialTransport) SetReadTimeout(timeout time.Duration) {
|
||||
t.port.SetReadTimeout(timeout)
|
||||
}
|
||||
|
||||
func (t *serialTransport) Reader() io.Reader {
|
||||
return t.port
|
||||
}
|
||||
|
||||
func (t *serialTransport) Close() error {
|
||||
return t.port.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user