Use better maintained go.bug.st/serial

This commit switches to serial port library to the better maintained
go.bug.st/serial library.
This commit is contained in:
Jan Dittberner 2020-04-17 19:39:06 +02:00
parent 42d1e6e991
commit 80b3309c7c
7 changed files with 51 additions and 65 deletions

View file

@ -2,39 +2,25 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"time"
"github.com/goburrow/serial"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "go.bug.st/serial"
"gopkg.in/yaml.v3"
"io/ioutil"
) )
type SerialConfig struct {
Address string `yaml:"address"`
BaudRate int `yaml:"baudrate"`
DataBits int `yaml:"databits"`
StopBits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
}
type ClientConfig struct { type ClientConfig struct {
Serial SerialConfig `yaml:"serial_config"` SerialAddress string `yaml:"serial_address"`
Paranoid bool `yaml:"paranoid"` BaudRate int `yaml:"serial_baudrate"`
Debug bool `yaml:"debug"` Paranoid bool `yaml:"paranoid"`
GNUPGBinary string `yaml:"gnupg_bin"` Debug bool `yaml:"debug"`
OpenSSLBinary string `yaml:"openssl_bin"` GNUPGBinary string `yaml:"gnupg_bin"`
MySQLDSN string `yaml:"mysql_dsn"` OpenSSLBinary string `yaml:"openssl_bin"`
MySQLDSN string `yaml:"mysql_dsn"`
} }
var defaultConfig = ClientConfig{ var defaultConfig = ClientConfig{
Serial: SerialConfig{ SerialAddress: "/dev/ttyUSB0",
Address: "/dev/ttyUSB0", BaudRate: 115200,
BaudRate: 115200,
DataBits: 8,
StopBits: 1,
Parity: "N",
},
Paranoid: false, Paranoid: false,
Debug: false, Debug: false,
OpenSSLBinary: "/usr/bin/openssl", OpenSSLBinary: "/usr/bin/openssl",
@ -72,13 +58,11 @@ func readConfig(configFile string) (config *ClientConfig, err error) {
return config, nil return config, nil
} }
func fillSerialConfig(clientConfig *ClientConfig) *serial.Config { func fillSerialMode(clientConfig *ClientConfig) *serial.Mode {
return &serial.Config{ return &serial.Mode{
Address: clientConfig.Serial.Address, BaudRate: clientConfig.BaudRate,
BaudRate: clientConfig.Serial.BaudRate, DataBits: 8,
DataBits: clientConfig.Serial.DataBits, StopBits: serial.OneStopBit,
StopBits: clientConfig.Serial.StopBits, Parity: serial.NoParity,
Parity: clientConfig.Serial.Parity,
Timeout: 30 * time.Second,
} }
} }

View file

@ -4,8 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/goburrow/serial"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"go.bug.st/serial"
"git.cacert.org/cacert-gosigner/datastructures" "git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared" "git.cacert.org/cacert-gosigner/shared"

View file

@ -5,11 +5,11 @@ import (
"fmt" "fmt"
"time" "time"
log "github.com/sirupsen/logrus"
"go.bug.st/serial"
"git.cacert.org/cacert-gosigner/datastructures" "git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared" "git.cacert.org/cacert-gosigner/shared"
"github.com/goburrow/serial"
log "github.com/sirupsen/logrus"
) )
func main() { func main() {
@ -19,20 +19,19 @@ func main() {
flag.Parse() flag.Parse()
var clientConfig *ClientConfig var clientConfig *ClientConfig
var serialConfig *serial.Config var serialConfig *serial.Mode
var err error var err error
if clientConfig, err = readConfig(configFile); err != nil { if clientConfig, err = readConfig(configFile); err != nil {
log.Panic(err) log.Panic(err)
} }
serialConfig = fillSerialConfig(clientConfig) serialConfig = fillSerialMode(clientConfig)
if clientConfig.Debug { if clientConfig.Debug {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
} }
log.Infof("connecting to %s", serialConfig.Address) log.Infof("connecting to %s using %+v", clientConfig.SerialAddress, serialConfig)
log.Tracef("serial parameters %v", serialConfig) port, err := serial.Open(clientConfig.SerialAddress, serialConfig)
port, err := serial.Open(serialConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

4
go.mod
View file

@ -3,7 +3,7 @@ module git.cacert.org/cacert-gosigner
go 1.14 go 1.14
require ( require (
github.com/goburrow/serial v0.1.0
github.com/sirupsen/logrus v1.5.0 github.com/sirupsen/logrus v1.5.0
gopkg.in/yaml.v2 v2.2.8 go.bug.st/serial v1.0.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
) )

15
go.sum
View file

@ -1,18 +1,29 @@
github.com/creack/goselect v0.1.1 h1:tiSSgKE1eJtxs1h/VgGQWuXUP0YS4CDIFMp6vaI1ls0=
github.com/creack/goselect v0.1.1/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q= github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.bug.st/serial v1.0.0 h1:ogEPzrllCsnG00EqKRjeYvPRsO7NJW6DqykzkdD6E/k=
go.bug.st/serial v1.0.0/go.mod h1:rpXPISGjuNjPTRTcMlxi9lN6LoIPxd1ixVjBd8aSk/Q=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -2,7 +2,7 @@ package shared
import ( import (
"errors" "errors"
"github.com/goburrow/serial" "go.bug.st/serial"
"io" "io"
"time" "time"
) )

View file

@ -4,41 +4,33 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"log"
"time" "time"
log "github.com/sirupsen/logrus"
"go.bug.st/serial"
"git.cacert.org/cacert-gosigner/datastructures" "git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared" "git.cacert.org/cacert-gosigner/shared"
"github.com/goburrow/serial"
) )
var ( var (
address string address string
baudrate int baudrate int
databits int
stopbits int
parity string
) )
func main() { func main() {
flag.StringVar(&address, "a", "/dev/ttyUSB0", "address") flag.StringVar(&address, "a", "/dev/ttyUSB0", "address")
flag.IntVar(&baudrate, "b", 115200, "baud rate") flag.IntVar(&baudrate, "b", 115200, "baud rate")
flag.IntVar(&databits, "d", 8, "data bits")
flag.IntVar(&stopbits, "s", 1, "stop bits")
flag.StringVar(&parity, "p", "N", "parity (N/E/O)")
flag.Parse() flag.Parse()
config := serial.Config{ serialMode := &serial.Mode{
Address: address,
BaudRate: baudrate, BaudRate: baudrate,
DataBits: databits, DataBits: 8,
StopBits: stopbits, StopBits: serial.OneStopBit,
Parity: parity, Parity: serial.NoParity,
Timeout: 5 * time.Minute,
} }
log.Printf("connecting %+v", config) log.Infof("connecting to %s using %+v", address, serialMode)
port, err := serial.Open(&config) port, err := serial.Open(address, serialMode)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }