简体   繁体   English

如何使用java检测usb供应商ID和产品ID

[英]How todetect usb vendor id and product id using java

I am writing an java application that detects usb device.When i executes the program it is showing all the usb devices in pc actually but i want the connected usb device only this is my java code: 我正在编写一个检测usb设备的java应用程序。当我执行程序时,它实际显示了pc中的所有usb设备,但我想要连接的usb设备,这只是我的java代码:

package com.lakshman.sundeep;

import org.usb4java.Context;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;

public class UTMusb 
{
    public static int vid = 6790;
    public static int pid = 29987;

    public static void main(String[] args) 
    {
        Context context = new Context();
        int result = LibUsb.init(context);
        if(result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to initialize the usb device",result);
        }
        DeviceList list = new DeviceList();
        result = LibUsb.getDeviceList(null, list);
        if(result < 0 )throw new LibUsbException("Unable to get device list",result);
            try
            {
                for(Device device : list)
                {
                    DeviceDescriptor device_descriptor = new DeviceDescriptor();
                    result = LibUsb.getDeviceDescriptor(device, device_descriptor);
                    if(result != LibUsb.SUCCESS)throw new LibUsbException("Unable to get device descriptor : ",result);
                    System.out.println("Product id is : "+device_descriptor.idProduct()+" "+"Vendor id is : "+device_descriptor.idVendor());
                    if(device_descriptor.idProduct()==pid && device_descriptor.idVendor()==vid)
                    {
                        System.out.println("Product id and vendor id was matched");
                    }
                    else
                    {

                        System.out.println("Product id and vendor id was not matched");
                    }

                }

            }
            finally
            {
                LibUsb.freeDeviceList(list, true);
            }


    }

}

this is my output: 这是我的输出:

Product id is : -29658 Vendor id is : -32634
Product id and vendor id was not matched
Product id is : -29651 Vendor id is : -32634
Product id and vendor id was not matched
Product id is : -16294 Vendor id is : 1133
Product id and vendor id was not matched
Product id is : -15587 Vendor id is : 1133
Product id and vendor id was not matched
Product id is : 29987 Vendor id is : 6790
Product id and vendor id was matched
Product id is : -32768 Vendor id is : -32633
Product id and vendor id was not matched
Product id is : -32760 Vendor id is : -32633
Product id and vendor id was not matched
Product id is : -29647 Vendor id is : -32634
Product id and vendor id was not matched

You can use http://usb4java.org 您可以使用http://usb4java.org

And this code: 这段代码:

Context context = new Context();
int result = LibUsb.init(context);
if (result != LibUsb.SUCCESS) {
    throw new LibUsbException("Unable to initialize libusb.", result);
}
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(null, list);
if (result < 0) throw new LibUsbException("Unable to get device list", result);
try {
    // Iterate over all devices and scan for the right one
    for (Device device: list) {
        DeviceDescriptor descriptor = new DeviceDescriptor();
        result = LibUsb.getDeviceDescriptor(device, descriptor);
        if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
        System.out.println(descriptor.idVendor()+" "+descriptor.idProduct());
    }
} finally {
    // Ensure the allocated device list is freed
    LibUsb.freeDeviceList(list, true);
}

You can test if a device is connected by trying to open it: 您可以通过尝试打开设备来测试设备是否已连接:

    DeviceHandle handle = new DeviceHandle();
    result = LibUsb.open(device, handle);
    if (result < 0) {
        System.out.println(String.format("Unable to open device: %s. "
            + "Continuing without device handle.",
            LibUsb.strError(result)));
        handle = null;
    }

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

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