diff options
author | xengineering <me@xengineering.eu> | 2024-05-30 16:47:29 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-05-30 16:48:39 +0200 |
commit | 96d159114af675d55fe0d7c0f22a69d65e9563fc (patch) | |
tree | 1d7b7e6a84b5d06765084ea6414874a21d3cbaa5 | |
parent | 0cfd378394b6c5d90e00c217b12113e0c3284031 (diff) | |
download | iot-core-96d159114af675d55fe0d7c0f22a69d65e9563fc.tar iot-core-96d159114af675d55fe0d7c0f22a69d65e9563fc.tar.zst iot-core-96d159114af675d55fe0d7c0f22a69d65e9563fc.zip |
software: communication: Add SerialInterface
This new type bundles the whole communication stack. This is easier to
use than handling types for each layer on the user side of the
communication package.
-rw-r--r-- | software/communication/interface.go | 52 | ||||
-rw-r--r-- | software/communication/physical.go | 28 | ||||
-rw-r--r-- | software/main.go | 16 |
3 files changed, 59 insertions, 37 deletions
diff --git a/software/communication/interface.go b/software/communication/interface.go new file mode 100644 index 0000000..d6111cc --- /dev/null +++ b/software/communication/interface.go @@ -0,0 +1,52 @@ +package communication + +import ( + "fmt" + "log" + + "go.bug.st/serial" +) + +type SerialInterface struct { + port serial.Port +} + +func NewSerialInterface() (SerialInterface, error) { + iface := SerialInterface{} + + devices, err := getStSerials() + if err != nil { + return iface, err + } + + if len(devices) != 1 { + return iface, fmt.Errorf("Require exactly one serial device from STMicroelectronics but %d attached", len(devices)) + } + device := devices[0] + + iface.port, err = openSerial(device) + if err != nil { + return iface, err + } + + return iface, nil +} + +func (i *SerialInterface) Log() { + for { + data, err := i.Read() + if err != nil { + log.Fatal(err) + } + log.Printf("RX: '%s'\n", string(data)) + } +} + +func (i *SerialInterface) Read() ([]byte, error) { + buff := make([]byte, 100) + n, err := i.port.Read(buff) + if err != nil { + return []byte{}, err + } + return buff[:n], nil +} diff --git a/software/communication/physical.go b/software/communication/physical.go index f79d382..853e4c4 100644 --- a/software/communication/physical.go +++ b/software/communication/physical.go @@ -1,8 +1,6 @@ package communication import ( - "fmt" - "go.bug.st/serial" "go.bug.st/serial/enumerator" ) @@ -11,11 +9,7 @@ const ( ST_VID = `0483` ) -type Phy struct { - port serial.Port -} - -func GetStSerials() ([]string, error) { +func getStSerials() ([]string, error) { retval := make([]string, 0) ports, err := enumerator.GetDetailedPortsList() @@ -34,26 +28,14 @@ func GetStSerials() ([]string, error) { return retval, nil } -func NewPhy(device string) (Phy, error) { - p := Phy{} +func openSerial(device string) (serial.Port, error) { + var port serial.Port mode := &serial.Mode{ BaudRate: 115200, } port, err := serial.Open(device, mode) if err != nil { - return p, err - } - p.port = port - return p, nil -} - -func (phy *Phy) Cat() error { - buff := make([]byte, 100) - for { - n, err := phy.port.Read(buff) - if err != nil { - return err - } - fmt.Printf("%s", string(buff[:n])) + return port, err } + return port, nil } diff --git a/software/main.go b/software/main.go index 48b71e7..d0d3a8c 100644 --- a/software/main.go +++ b/software/main.go @@ -7,22 +7,10 @@ import ( ) func main() { - ports, err := communication.GetStSerials() + iface, err := communication.NewSerialInterface() if err != nil { log.Fatal(err) } - if len(ports) != 1 { - log.Fatalf("Require exactly one serial port from STMicroelectronics but %d attached", len(ports)) - } - - log.Printf("Detected serial port: %s\n", ports[0]) - - phy, err := communication.NewPhy(ports[0]) - - log.Println("Starting to print out incoming data from serial port") - err = phy.Cat() - if err != nil { - log.Fatal(err) - } + iface.Log() } |