From 305443a5bea0ea991f574f403aae56c6756f6160 Mon Sep 17 00:00:00 2001 From: jindongwu Date: Wed, 4 Oct 2023 11:51:20 +0200 Subject: [PATCH] kd6rmx add windows support --- go.mod | 6 ++++++ go.sum | 6 ++++++ kd6rmx.go | 47 ++++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index e9f8208..5369d9f 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,9 @@ module github.com/northvolt/go-kd6rmx go 1.17 require github.com/peterbourgon/ff/v3 v3.1.0 + +require ( + github.com/creack/goselect v0.1.2 // indirect + go.bug.st/serial v1.6.1 // indirect + golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect +) diff --git a/go.sum b/go.sum index 5dc37d8..6ddca1d 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,13 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= +github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/peterbourgon/ff/v3 v3.1.0 h1:5JAeDK5j/zhKFjyHEZQXwXBoDijERaos10RE+xamOsY= github.com/peterbourgon/ff/v3 v3.1.0/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE= +go.bug.st/serial v1.6.1 h1:VSSWmUxlj1T/YlRo2J104Zv3wJFrjHIl/T3NeruWAHY= +go.bug.st/serial v1.6.1/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/kd6rmx.go b/kd6rmx.go index 6113849..fce7161 100644 --- a/kd6rmx.go +++ b/kd6rmx.go @@ -9,11 +9,14 @@ import ( "strconv" "strings" "time" + + "go.bug.st/serial" ) // Sensor is a wrapper for control functions for the KD6RMX contact image sensor. type Sensor struct { Port string + BaudRate int Logging bool FileLogging bool } @@ -24,10 +27,13 @@ func (cis Sensor) CommunicationSpeed(baud int) error { switch baud { case 9600: param = "00" + cis.BaudRate = 9600 case 19200: param = "01" + cis.BaudRate = 19200 case 115200: param = "02" + cis.BaudRate = 115200 default: return errors.New("invalid baud rate") } @@ -132,8 +138,7 @@ const ( // // For example: // -// cis.PixelOutputFormat(kd6rmx.PixelOutputBits10, kd6rmx.PixelOutputSerial, kd6rmx.PixelOutputBase, 1) -// +// cis.PixelOutputFormat(kd6rmx.PixelOutputBits10, kd6rmx.PixelOutputSerial, kd6rmx.PixelOutputBase, 1) func (cis Sensor) PixelOutputFormat(bits PixelOutputBits, i PixelOutputInterface, conf PixelOutputConfig, number int) error { var param string switch bits { @@ -304,10 +309,11 @@ func (cis Sensor) InternalSync(val int) error { // LoadSettings loads the sensor's active settings with one of the memory presets. // // Valid presets are: -// 0 (factory defaults) -// 1 (user settings 1) -// 2 (user settings 2) -// 3 (user settings 3) +// +// 0 (factory defaults) +// 1 (user settings 1) +// 2 (user settings 2) +// 3 (user settings 3) func (cis Sensor) LoadSettings(preset int) error { if preset < 0 || preset > 4 { return errors.New("invalid preset for LoadSettings") @@ -324,9 +330,10 @@ func (cis Sensor) LoadSettings(preset int) error { // SaveSettings saves the sensor's active settings to one of the memory presets. // // Valid presets are: -// 1 (user settings 1) -// 2 (user settings 2) -// 3 (user settings 3) +// +// 1 (user settings 1) +// 2 (user settings 2) +// 3 (user settings 3) func (cis Sensor) SaveSettings(preset int) error { if preset < 1 || preset > 4 { return errors.New("invalid preset for SaveSettings") @@ -343,10 +350,11 @@ func (cis Sensor) SaveSettings(preset int) error { // LEDControl sets the LED settings. You can do the following: // Turn on/off the A and B LEDS individually. // Set the pulse divider to be used for both LEDS so they are: -// 1 = on all of the time -// 2 = on one half of the time -// 4 = on one quarter of the time -// 8 = on one eighth of the time +// +// 1 = on all of the time +// 2 = on one half of the time +// 4 = on one quarter of the time +// 8 = on one eighth of the time func (cis Sensor) LEDControl(leds string, on bool, pulsedivider int) error { var pd int switch pulsedivider { @@ -584,12 +592,14 @@ func (cis Sensor) SoftwareReset() error { } func (cis Sensor) SendCommand(cmd string, params string) (string, error) { - - f, err := os.OpenFile(cis.Port, os.O_RDWR|os.O_APPEND, 0777) + mode := &serial.Mode{ + BaudRate: cis.BaudRate, + } + port, err := serial.Open(cis.Port, mode) if err != nil { return "", fmt.Errorf("error opening control port: %v", err) } - defer f.Close() + defer port.Close() write_string := cmd + params + "\r" dt_string := time.Now().Format("2006-01-02 15:04:05.000000000") @@ -614,7 +624,7 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) { fmt.Println("send: ", write_string) } - _, err = f.Write([]byte(write_string)) + _, err = port.Write([]byte(write_string)) if err != nil { return "", fmt.Errorf("error sending command: %v", err) } @@ -623,8 +633,7 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) { var result string start := time.Now() for { - - n, err := f.Read(buf) + n, err := port.Read(buf) if err != nil { if err == io.EOF { if time.Since(start) > time.Second*10 {