sope
sope

Reputation: 1821

How to get the serial Number of USB device with golang?

How to get the serial Number of USB device with golang ?

Is there any example code ?

Anyone who know !

Upvotes: 1

Views: 5211

Answers (2)

Matthew Rankin
Matthew Rankin

Reputation: 461157

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

Upvotes: 0

Majonsi
Majonsi

Reputation: 414

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

Upvotes: 1

Related Questions