2014-10-10 206 views
0

我是韩国YeungNam大学计算机工程系的学生。如何从iserialnumber获取USB设备序列号?

首先,对我英语不好的技巧感到抱歉。

我需要帮助。

我找到了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 <[email protected]> 
*/ 
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序列号(索引在设备描述符中)而不是真正的serialnumber。

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

如何获得序列号?

在Usb4Java,Javax.usb,libusb中,这些API不包含诸如'getSerialnumber()'之类的方法。

请帮助我。

回答

1

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

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

+0

非常感谢!我会尝试! – 2014-10-10 03:32:00

+0

嗨duskwuff,我已经尝试过方法'getString(字节)',但不能正常工作。首先,它告诉我它需要围绕try/catch,所以我做到了。那么它会向我展示'在此平台上不支持或未实现的操作'消息。它不支持Windows吗? – 2014-10-12 04:08:25

+0

它仅在使用LibUSB或LibUSB-Win32驱动程序的设备上受支持。 – 2014-10-12 13:44:31