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
53
54
55
56
57
58
59
|
package communication
import (
"fmt"
"go.bug.st/serial"
"go.bug.st/serial/enumerator"
)
const (
ST_VID = `0483`
)
type Phy struct {
port serial.Port
}
func GetStSerials() ([]string, error) {
retval := make([]string, 0)
ports, err := enumerator.GetDetailedPortsList()
if err != nil {
return retval, err
}
for _, port := range ports {
if port.IsUSB {
if port.VID == ST_VID {
retval = append(retval, port.Name)
}
}
}
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]))
}
}
|