如何从iserialnumber获得USB设备序列号?

时间:2020-12-25 09:18:53

I am student of Computer engineering dept of YeungNam University in Korea.

我是韩国YeungNam大学计算机工程系的学生。

First, sorry about my poor english skill.

首先,很抱歉我的英语水平很差。

I need help, please.

请我需要帮助。

I found USB API for Java.

我为Java找到了USB接口。

I need USB device's PID, VID and unique serial number because I need to do identify each USB device for my project(just mass storage device).

我需要USB设备的PID, VID和唯一的序列号,因为我需要为我的项目确定每个USB设备(仅仅是海量存储设备)。

I use below example code. (This is usb4java API example code.) This code show me some information about USB HUDs, connected devices.

我使用下面的示例代码。(这是usb4java API示例代码。)这段代码向我展示了一些关于USB接口、连接设备的信息。

package org.usb4java.javax.examples;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.usb.UsbConfiguration;
import javax.usb.UsbDevice;
import javax.usb.UsbDisconnectedException;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbPort;
import javax.usb.UsbServices;
/**
 * Dumps all devices by using the javax-usb API.
 * 
 * @author Klaus Reimer <k@ailis.de>
 */
public class DumpDevices
{
/**
 * Dumps the specified USB device to stdout.
 * 
 * @param device
 *            The USB device to dump.
 */
private static void dumpDevice(final UsbDevice device)
{
    // Dump information about the device itself
    System.out.println(device);
    final UsbPort port = device.getParentUsbPort();
    if (port != null)
    {
        System.out.println("Connected to port: " + port.getPortNumber());
        System.out.println("Parent: " + port.getUsbHub());
    }

    // Dump device descriptor
        System.out.println(device.getUsbDeviceDescriptor());

    // Process all configurations
    for (UsbConfiguration configuration: (List<UsbConfiguration>) device
        .getUsbConfigurations())
    {
        // Dump configuration descriptor
        System.out.println(configuration.getUsbConfigurationDescriptor());

        // Process all interfaces
        for (UsbInterface iface: (List<UsbInterface>) configuration
            .getUsbInterfaces())
        {
            // Dump the interface descriptor
            System.out.println(iface.getUsbInterfaceDescriptor());

            // Process all endpoints
            for (UsbEndpoint endpoint: (List<UsbEndpoint>) iface
                .getUsbEndpoints())
            {
                // Dump the endpoint descriptor
                System.out.println(endpoint.getUsbEndpointDescriptor());
            }
        }
    }

    System.out.println();

    // Dump child devices if device is a hub
    if (device.isUsbHub())
    {
        final UsbHub hub = (UsbHub) device;
        for (UsbDevice child: (List<UsbDevice>) hub.getAttachedUsbDevices())
        {
            dumpDevice(child);
        }

    }
}

/**
 * Main method.
 * 
 * @param args
 *            Command-line arguments (Ignored)
 * @throws UsbException
 *             When an USB error was reported which wasn't handled by this
 *             program itself.
 */
public static void main(final String[] args) throws UsbException
{
    // Get the USB services and dump information about them
    final UsbServices services = UsbHostManager.getUsbServices();
    System.out.println("USB Service Implementation: "
        + services.getImpDescription());
    System.out.println("Implementation version: "
        + services.getImpVersion());
    System.out.println("Service API version: " + services.getApiVersion());
    System.out.println();

    // Dump the root USB hub
    dumpDevice(services.getRootUsbHub());
}
}

and this code result like this:

这段代码的结果如下:


USB Service Implementation: usb4java
Implementation version: 1.2.0
Service API version: 1.0.2

usb4java root hub 1.0.0
Device Descriptor:
  bLength                 18
  bDescriptorType          1
  bcdUSB                1.01
  bDeviceClass             9 Hub
  bDeviceSubClass          0
  bDeviceProtocol          0
  bMaxPacketSize0          8
  idVendor            0xffff
  idProduct           0xffff
  bcdDevice             0.00
  iManufacturer            1
  iProduct                 2
  iSerial                  3
  bNumConfigurations       1

Configuration Descriptor:
  bLength                  9
  bDescriptorType          2
  wTotalLength            18
  bNumInterfaces           1
  bConfigurationValue      1
  iConfiguration           0
  bmAttributes          0x80
    (Bus Powered)
  bMaxPower                0mA

Interface Descriptor:
  bLength                  9
  bDescriptorType          4
  bInterfaceNumber         0
  bAlternateSetting        0
  bNumEndpoints            0
  bInterfaceClass          9 Hub
  bInterfaceSubClass       0
  bInterfaceProtocol       0
  iInterface               0


Bus 002 Device 007: ID 203a:fffa
Connected to port: 1
Parent: usb4java root hub 1.0.0
Device Descriptor:
  bLength                 18
  bDescriptorType          1
  bcdUSB                2.00
  bDeviceClass             0 Per Interface
  bDeviceSubClass          0
  bDeviceProtocol          0
  bMaxPacketSize0         64
  idVendor            0x203a
  idProduct           0xfffa
  bcdDevice             1.00
  iManufacturer            1
  iProduct                 2
  iSerial                  3
  bNumConfigurations       1

Configuration Descriptor:
  bLength                  9
  bDescriptorType          2
  wTotalLength            25
  bNumInterfaces           1
  bConfigurationValue      1
  iConfiguration           1
  bmAttributes          0xc0
    Self Powered
  bMaxPower                0mA

Interface Descriptor:
  bLength                  9
  bDescriptorType          4
  bInterfaceNumber         0
  bAlternateSetting        0
  bNumEndpoints            1
  bInterfaceClass          7 Printer
  bInterfaceSubClass       1
  bInterfaceProtocol       1
  iInterface               4

Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
 bEndpointAddress      0x01  EP 1 OUT
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize         512
  bInterval                0
.
.
.
.
Bus 002 Device 003: ID 152d:2329
Connected to port: 3
Parent: usb4java root hub 1.0.0
Device Descriptor:
  bLength                 18
  bDescriptorType          1
  bcdUSB                2.00
  bDeviceClass             0 Per Interface
  bDeviceSubClass          0
  bDeviceProtocol          0
  bMaxPacketSize0         64
  idVendor            0x152d
  idProduct           0x2329
  bcdDevice             1.00
  iManufacturer            1
  iProduct                 2
  iSerial                  5
  bNumConfigurations       1

Configuration Descriptor:
  bLength                  9
  bDescriptorType          2
  wTotalLength            32
  bNumInterfaces           1
  bConfigurationValue      1
  iConfiguration           4
  bmAttributes          0xc0
    Self Powered
  bMaxPower                2mA

Interface Descriptor:
  bLength                  9
  bDescriptorType          4
  bInterfaceNumber         0
  bAlternateSetting        0
  bNumEndpoints            2
  bInterfaceClass          8 Mass Storage
  bInterfaceSubClass       6
  bInterfaceProtocol      80
  iInterface               6
.
.
.
.
Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
  bEndpointAddress      0x02  EP 2 OUT
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize         512
  bInterval                0

Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
  bEndpointAddress      0x81  EP 1 IN
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize         512
  bInterval                0


Endpoint Descriptor:
  bLength                  7
  bDescriptorType          5
  bEndpointAddress      0x01  EP 1 OUT
  bmAttributes             2
    Transfer Type             Bulk
    Synch Type                None
    Usage Type                Data
  wMaxPacketSize         512
      bInterval                0

In result of code, there is mass storage and I can see PID and VID, but there is iserialnumber(index of serial number in device descriptor) instead of real serialnumber.

在代码的结果中,有大量的存储,我可以看到PID和VID,但是有iserialnumber(设备描述符中序列号的索引),而不是真正的serialnumber。

I think that to identify each USB mass storage device, need combination of PID, VID, S/N.

我认为要识别每个USB存储设备,需要PID、VID、S/N的组合。

How can I get serial number?

我怎样才能得到序列号?

In Usb4Java, Javax.usb, libusb, these APIs not include methods like 'getSerialnumber()'.

Usb4Java Javax。usb, libusb,这些api不包括像'getSerialnumber()'这样的方法。

Help me please.

请帮助我。

1 个解决方案

#1


1  

The "iSerial" is the index of the version string in the device's string table. You can retrieve the corresponding string using the getString(byte) method on the device. Same thing goes for "iManufacturer" and "iProduct".

“iSerial”是设备字符串表中版本字符串的索引。您可以在设备上使用getString(byte)方法检索相应的字符串。“iManufacturer”和“iProduct”也是一样。

Keep in mind that not all devices will have a unique serial number.

请记住,并非所有的设备都有唯一的序列号。

#1


1  

The "iSerial" is the index of the version string in the device's string table. You can retrieve the corresponding string using the getString(byte) method on the device. Same thing goes for "iManufacturer" and "iProduct".

“iSerial”是设备字符串表中版本字符串的索引。您可以在设备上使用getString(byte)方法检索相应的字符串。“iManufacturer”和“iProduct”也是一样。

Keep in mind that not all devices will have a unique serial number.

请记住,并非所有的设备都有唯一的序列号。