[英]Usb4java library, error while claiming an interface on windows machine
Using libusb and usb4java I am trying to communicate with a USB device. 使用libusb和usb4java我正在尝试与USB设备通信。 Below is the code with which I am trying to communicate with the USB device on Windows.
下面是我尝试与Windows上的USB设备通信的代码。
Code Snippet: 代码片段:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import org.usb4java.Context;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceHandle;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;
public class USBTest {
public static int VENDOR_ID = 0x0c45;
public static int PRODUCT_ID = 0x7406;
public static void main(final String[] args) {
final Device usbDevice = getUsbDevice();
final DeviceHandle handle = new DeviceHandle();
int result = LibUsb.open(usbDevice, handle);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to open USB device", result);
}
result = LibUsb.claimInterface(handle, 0);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("", result);
}
try {
ByteBuffer data = convertArrayToBuffer(new byte[] {(byte) 0xAA, (byte) 0xF4, (byte) 0xF4, (byte) 0xF4, (byte) 0xF4, (byte) 0xF4, (byte) 0xF4, (byte) 0xF4});
result = LibUsb.controlTransfer(handle, (byte) 0x09, (byte) (2 << 8 | 9), (short) 0, (short) 0, data, 1000);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to claim interface", result);
}
data = ByteBuffer.allocate(8);
final IntBuffer in = IntBuffer.allocate(8);
result = LibUsb.bulkTransfer(handle, (byte) 0, data, in, 1000);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to claim interface", result);
}
} catch(final Exception e) {
e.printStackTrace();
} finally {
result = LibUsb.releaseInterface(handle, 0);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to release interface", result);
}
}
}
private static ByteBuffer convertArrayToBuffer(final byte[] data) {
final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(data.length);
byteBuffer.order(ByteOrder.nativeOrder());
byteBuffer.put(data);
byteBuffer.position(0);
return byteBuffer;
}
private static Device getUsbDevice() {
Device usbDevice = null;
final Context context = new Context();
int result = LibUsb.init(context);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to initialize the usb device", result);
}
final DeviceList deviceList = new DeviceList();
result = LibUsb.getDeviceList(null, deviceList);
if(result < 0) {
throw new LibUsbException("Unable to get device list", result);
}
for(final Device device : deviceList) {
final DeviceDescriptor deviceDescriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, deviceDescriptor);
if(result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to get device descriptor : ", result);
}
if(deviceDescriptor.idProduct() == PRODUCT_ID && deviceDescriptor.idVendor() == VENDOR_ID) {
System.out.println("Product id and vendor id was matched");
usbDevice = device;
break;
}
}
return usbDevice;
}
}
Error: 错误:
Exception in thread "main" org.usb4java.LibUsbException: USB error 3: : Access denied (insufficient permissions)
at com.usb.device.USBTest.main(USBTest.java:30)
Thanks for any help in advance. 在此先感谢您的帮助。
I do not know if this will help, especially after all this time, but I ran into the same problem and solved it as follows: 我不知道这是否会有所帮助,特别是在这段时间之后,但我遇到了同样的问题并解决了如下问题:
First, the device I was working with is a Continua Health Device following the PHDC standard. 首先,我使用的设备是遵循PHDC标准的康体佳健康设备。 There is no standard driver for it.
它没有标准的驱动程序。 I have a driver based upon libusb-win32.
我有一个基于libusb-win32的驱动程序。 The usb4java that I have uses libusbK and that was the problem.
我使用的usb4java使用了libusbK,这就是问题所在。 I downloaded Zadig http://zadig.akeo.ie/ and replaced the libusb-win32 with the libusbK driver and I no longer got a 'cant claim interface insufficient permission' error.
我下载了Zadig http://zadig.akeo.ie/并用libusbK驱动程序替换了libusb-win32,我不再有“无法声明接口权限不足”的错误。
There is probably a way to use the libusb-win32 driver with usb4java but I did not try that approach. 可能有一种方法可以使用usb4java的libusb-win32驱动程序,但我没有尝试这种方法。 I was just happy to get something to work!
我很高兴得到一些工作!
I am still unable to get plug-in events however which kind of stinks. 我仍然无法获得插件事件,但是哪种情况很糟糕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.