英文:
How to get the serial Number of USB device with golang?
问题
如何使用golang获取USB设备的序列号?
有没有示例代码?
有人知道吗!
英文:
How to get the serial Number of USB device with golang ?
Is there any example code ?
Anyone who know !
答案1
得分: 1
你必须在golang中使用libusb的包装器(例如gousb)。但是这个包装器没有获取序列号的命令。所以你需要自己实现。在libusb中,用于执行此操作的命令是:
C.libusb_get_string_descriptor_ascii
英文:
You must use a wrapper of libusb in golang (an example is gousb).
But this wrapper doesn't have the command in order to get the serial number.
So you have to implement it. The command in libusb in order to do this is:
C.libusb_get_string_descriptor_ascii
答案2
得分: 0
libusb Go包中有一个方法GetStringDesciptorASCII
,可以用来获取USB设备的序列号。
不带错误处理的示例
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("找到序列号: %s", serialNumber)
}
}
英文:
The libusb Go package has a method GetStringDesciptorASCII
that can be used to get the S/N of a USB device.
Example without error handling
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论