diff options
author | xengineering <me@xengineering.eu> | 2024-05-30 16:13:04 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-05-30 16:13:04 +0200 |
commit | 0cfd378394b6c5d90e00c217b12113e0c3284031 (patch) | |
tree | a8f6f121abeef12aeaea081620bfcf3c09033a97 | |
parent | b8ef4d11fe0d00ce0884ccf982675845b20c3ce9 (diff) | |
download | iot-core-0cfd378394b6c5d90e00c217b12113e0c3284031.tar iot-core-0cfd378394b6c5d90e00c217b12113e0c3284031.tar.zst iot-core-0cfd378394b6c5d90e00c217b12113e0c3284031.zip |
software: Implement serial port printout
This validates that the code is able to get data from the serial port.
-rw-r--r-- | software/communication/physical.go | 31 | ||||
-rw-r--r-- | software/main.go | 10 |
2 files changed, 40 insertions, 1 deletions
diff --git a/software/communication/physical.go b/software/communication/physical.go index 25d5299..f79d382 100644 --- a/software/communication/physical.go +++ b/software/communication/physical.go @@ -1,6 +1,9 @@ package communication import ( + "fmt" + + "go.bug.st/serial" "go.bug.st/serial/enumerator" ) @@ -8,6 +11,10 @@ const ( ST_VID = `0483` ) +type Phy struct { + port serial.Port +} + func GetStSerials() ([]string, error) { retval := make([]string, 0) @@ -26,3 +33,27 @@ func GetStSerials() ([]string, error) { return retval, nil } + +func NewPhy(device string) (Phy, error) { + p := Phy{} + 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])) + } +} diff --git a/software/main.go b/software/main.go index 66fb8a6..48b71e7 100644 --- a/software/main.go +++ b/software/main.go @@ -16,5 +16,13 @@ func main() { log.Fatalf("Require exactly one serial port from STMicroelectronics but %d attached", len(ports)) } - log.Printf("Detected port: %s\n", ports[0]) + 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) + } } |