From 96d159114af675d55fe0d7c0f22a69d65e9563fc Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 30 May 2024 16:47:29 +0200 Subject: 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. --- software/communication/interface.go | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 software/communication/interface.go (limited to 'software/communication/interface.go') 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 +} -- cgit v1.2.3-70-g09d2