繁体   English   中英

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

[英]How can I get USB device serial number from iserialnumber?

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

首先,对我的英语水平不好感到抱歉。

我需要帮助。

我找到了适用于Java的USB API。

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

我使用下面的示例代码。 (这是usb4java API示例代码。)此代码向我展示了有关USB HUD和所连接设备的一些信息。

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

此代码结果如下:


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

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

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

如何获得序列号?

在Usb4Java,Javax.usb,libusb中,这些API不包括“ getSerialnumber()”之类的方法。

请帮帮我。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM