1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}
|