如何使用golang获取USB设备的序列号?

huangapple go评论92阅读模式
英文:

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)
	}
}

huangapple
  • 本文由 发表于 2016年2月17日 15:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/35450288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定