如何使用 java 检测 USB 供应商 ID 和产品 ID
How todetect usb vendor id and product id using java
我正在编写一个 java 检测 usb 的应用程序 device.When 我执行的程序实际上显示了 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);
}
}
}
这是我的输出:
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
您可以使用http://usb4java.org
还有这段代码:
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);
}
您可以通过尝试打开设备来测试设备是否已连接:
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;
}
我正在编写一个 java 检测 usb 的应用程序 device.When 我执行的程序实际上显示了 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);
}
}
}
这是我的输出:
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
您可以使用http://usb4java.org
还有这段代码:
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);
}
您可以通过尝试打开设备来测试设备是否已连接:
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;
}