资源已经释放,还是open失败。
LibUsb.freeDeviceList(list, true);报错:USB error 4: No such device (it may have been disconnected)
LibUsb.freeDeviceList(list, false);报错:USB error 12: Operation not supported or unimplemented on this platform
public class Test3 {
private static final short idVendor = 0x4080;
private static final short idProduct = 0x00F0;
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 libusb.", result);
}
//获取对应的USB
Device device = findDevice(context);
System.out.println(device);
//打开设备
DeviceHandle handle = new DeviceHandle();
result = LibUsb.open(device, handle);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException(result);
}
}
public static Device findDevice(Context context){
DeviceList list = new DeviceList();
int result = LibUsb.getDeviceList(context, list);
try {
// 遍历设备列表
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("设备: VID="+descriptor.idVendor()+", PID="+descriptor.idProduct());
System.out.printf("设备: VID=0x%04X, PID=0x%04X%n", descriptor.idVendor(), descriptor.idProduct());
System.out.println("-------------------------------");
if (descriptor.idVendor() == idVendor && descriptor.idProduct() == idProduct){
return device;
}
}
}catch (Exception e){
e.printStackTrace();
}
finally {
// 释放资源
LibUsb.freeDeviceList(list, true);
LibUsb.exit(context);
}
return null;
}
}
相似问题