Move to go-serial

This commit is contained in:
Torsten Harenberg
2021-12-06 16:13:11 +01:00
parent 33afb49859
commit 7c7593c601
3 changed files with 40 additions and 32 deletions

43
usb.go
View File

@@ -4,8 +4,7 @@ import (
"fmt"
"log"
"github.com/google/gousb"
"github.com/google/gousb/usbid"
"github.com/albenik/go-serial/enumerator"
)
/*
@@ -21,34 +20,20 @@ import (
*/
func lsusb() {
// Only one context should be needed for an application. It should always be closed.
ctx := gousb.NewContext()
defer ctx.Close()
// OpenDevices is used to find the devices to open.
devs, err := ctx.OpenDevices(func(desc *gousb.DeviceDesc) bool {
// The usbid package can be used to print out human readable information.
fmt.Printf("%03d.%03d %s:%s %s\n", desc.Bus, desc.Address, desc.Vendor, desc.Product, usbid.Describe(desc))
fmt.Printf(" Protocol: %s\n", usbid.Classify(desc))
fmt.Println(desc.Configs)
return false
})
// All Devices returned from OpenDevices must be closed.
defer func() {
for _, d := range devs {
d.Close()
}
}()
// OpenDevices can occasionally fail, so be sure to check its return value.
ports, err := enumerator.GetDetailedPortsList()
if err != nil {
log.Fatalf("list: %s", err)
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(" USB ID %s:%s\n", port.VID, port.PID)
fmt.Printf(" USB serial %s\n", port.SerialNumber)
}
}
for _, dev := range devs {
// Once the device has been selected from OpenDevices, it is opened
// and can be interacted with.
_ = dev
}
}