Java – How to get USB device serial number from iserialnumber

javausb

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

First, sorry about my poor english skill.

I need help, please.

I found USB API for Java.

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

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

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.

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

How can I get serial number?

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

Help me please.

Best Answer

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".

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