查找使用java插入的单个USB设备的详细信息

时间:2016-12-19 06:28:55

标签: java eclipse window usb libusb

请原谅我的英语,因为英语不是我的主要语言。

我目前是学生 - 坦率地说,我正在努力完成我的项目。

使用的语言是JAVA,IDE是eclipse。

目前我使用LIBUSB作为我从http://usb4java.org/下载的项目。它非常有用,我设法获得了我的设备的详细信息列表。 所以我的问题是......让我说我只想确定例如设备004。 对此有什么建议吗? ?我尝试扫描它两次并比较输出但由于给定的数据类型不匹配而失败。起初我认为数据类型可能是arraylist但不是。有没有其他方法将输出存储到文本文件,所以我可以做比较?

我从网上的libusb示例中获取的代码。

public class UsbBench {
/**
 * Main method.
 * 
 * @param args
 *            Command-line arguments (Ignored)
 * @throws IOException 
 */
public static void main(String[] args) throws IOException 
{
    // Create the libusb context
    Context context = new Context();

    // Initialize the libusb context
    int result = LibUsb.init(context);
    if (result < 0)
    {
        throw new LibUsbException("Unable to initialize libusb", result);
    }

    // Read the USB device list
    DeviceList list = new DeviceList();

    result = LibUsb.getDeviceList(context, list);
    if (result < 0)
    {
        throw new LibUsbException("Unable to get device list", result);
    }

    try
    {
        // Iterate over all devices and list them
        for (Device device: list)
        {
            int address = LibUsb.getDeviceAddress(device);
            int busNumber = LibUsb.getBusNumber(device);
            DeviceDescriptor descriptor = new DeviceDescriptor();
            result = LibUsb.getDeviceDescriptor(device, descriptor);
            if (result < 0)
            {
                throw new LibUsbException(
                    "Unable to read device descriptor", result);
            }

        }

    }
    finally
    {
        // Ensure the allocated device list is freed
        LibUsb.freeDeviceList(list, true);
    }



    // Deinitialize the libusb context
    LibUsb.exit(context);
}

}

它也很好用。输出:

Bus 002, Device 003: Vendor 1bcf, Product 2c6e
Bus 001, Device 002: Vendor 06cb, Product 2970
Bus 001, Device 003: Vendor 0461, Product 4ded
Bus 001, Device 001: Vendor 8086, Product 8c31
Bus 002, Device 001: Vendor 8086, Product 8c26
Bus 002, Device 004: Vendor 04ca, Product 300b
Bus 002, Device 005: Vendor 8087, Product 8000

所以我的问题是......让我说我只想确定例如设备004。 对此有什么建议吗? ?这也是我第一次在stackoverflow中。我谦虚地寻求你的专业知识。谢谢。感谢。

1 个答案:

答案 0 :(得分:0)

您可以从操作系统的设置中了解有关USB设备的一些信息,但我建议您下载此软件(http://www.nirsoft.net/)。

它允许您检查计算机上连接的每台设备,并了解有关它的几个信息(例如:VendorId&amp; ProductId),以便您识别设备。

example USB