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
60
61
62
|
package serial
import (
"fmt"
"github.com/charmbracelet/log"
"go.bug.st/serial/enumerator"
)
type usbDevice struct {
VID string
PID string
}
var knownDevices = []usbDevice{
{VID: "239A", PID: "8029"}, // rak4631_19003
}
func GetPorts() []string {
ports, err := enumerator.GetDetailedPortsList()
if err != nil {
log.Fatal(err)
}
var foundDevices []string
if len(ports) == 0 {
fmt.Println("No serial ports found!")
return nil
}
for _, port := range ports {
//fmt.Printf("Found port: %s\n", port.SettingName)
if port.IsUSB {
for _, device := range knownDevices {
if device.VID != port.VID {
continue
}
if device.PID != port.PID {
continue
}
foundDevices = append(foundDevices, port.Name)
}
}
}
return foundDevices
}
func getUSB() {
ports, err := enumerator.GetDetailedPortsList()
if err != nil {
log.Fatal(err)
}
if len(ports) == 0 {
fmt.Println("No serial ports found!")
return
}
for _, port := range ports {
fmt.Printf("Found port: %s\n", port.Name)
if port.IsUSB {
fmt.Printf(" Product %s\n", port.Product)
fmt.Printf(" USB ID %s:%s\n", port.VID, port.PID)
fmt.Printf(" USB serial %s\n", port.SerialNumber)
}
}
}
|