问题描述
我正在尝试使用 usb4java lib 从 HID 设备打开和读取数据,但出现以下错误。
org.usb4java.LibUsbException:USB 错误 4:无法打开 USB 设备:没有这样的设备(可能已断开连接)
使用以下代码:
DeviceList list = new DeviceList();
Context context = new Context();
int result = LibUsb.init(context);
if (result < 0) {
throw new LibUsbException("Unable to initialize libusb",result);
}
result = LibUsb.getDeviceList(context,list);
if (result < 0) {
throw new LibUsbException("Unable to get device list",result);
}
Device usbDevice = null;
try {
for (Device device : list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device,descriptor);
if (result == LibUsb.SUCCESS) {
if (descriptor.idProduct() == 17 && descriptor.idvendor() == 1155) {
System.out.println("device : " + device.toString());
System.out.println("desc : " + descriptor.dump());
usbDevice = device;
LibUsb.freeDeviceList(list,false);
break;
}
}
}
} catch (Exception e) {
e.printstacktrace();
}
if (usbDevice != null) {
DeviceHandle handle = new DeviceHandle();
result = LibUsb.open(usbDevice,handle);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to open USB device",result);
}
try {
int attached = LibUsb.kernelDriverActive(handle,1);
if (attached < 0) {
throw new LibUsbException(
"Unable to check kernel driver active",result);
}
result = LibUsb.detachKernelDriver(handle,1);
if (result != LibUsb.SUCCESS
&& result != LibUsb.ERROR_NOT_SUPPORTED
&& result != LibUsb.ERROR_NOT_FOUND) {
throw new LibUsbException("Unable to detach kernel driver",result);
}
result = LibUsb.claimInterface(handle,1);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to claim interface",result);
}
result = LibUsb.releaseInterface(handle,1);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to release interface",result);
}
if (attached == 1) {
LibUsb.attachKernelDriver(handle,1);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException(
"Unable to re-attach kernel driver",result);
}
}
LibUsb.exit(null);
} catch (Exception e) {
e.printstacktrace();
} finally {
LibUsb.close(handle);
}
}
我的设备是 HID 条码扫描仪,以下是其描述符详细信息:
b长度:18
bDescriptorType : 1
bcdUSB:1.10
bDeviceClass : 0 每个接口
bDeviceSubClass : 0
bDeviceProtocol : 0
bMaxPacketSize0 : 64
id供应商:0x0483
产品编号:0x0011
bcdDevice:0.01 iManufacturer : 1
iProduct : 2
iSerial : 0
bNumConfigurations : 1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)