Reputation: 1821
How to get the serial Number of USB device with golang ?
Is there any example code ?
Anyone who know !
Upvotes: 1
Views: 5211
Reputation: 461157
The libusb Go package has a method GetStringDesciptorASCII
that can be used to get the S/N of a USB device.
package main
import (
"log"
"github.com/gotmc/libusb"
)
func main() {
ctx, _ := libusb.Init()
defer ctx.Exit()
devices, _ := ctx.GetDeviceList()
for _, device := range devices {
usbDeviceDescriptor, _ := device.GetDeviceDescriptor()
handle, _ := device.Open()
defer handle.Close()
snIndex := usbDeviceDescriptor.SerialNumberIndex
serialNumber, _ := handle.GetStringDescriptorASCII(snIndex)
log.Printf("Found S/N: %s", serialNumber)
}
}
Upvotes: 0