2014-10-10 240 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