使用libusb和usb4java我正在尝试与USB设备通信。下面是我尝试与Windows上的USB设备通信的代码。
代码段:
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;
}
}
错误:
Exception in thread "main" org.usb4java.LibUsbException: USB error 3: : Access denied (insufficient permissions)
at com.usb.device.USBTest.main(USBTest.java:30)
感谢您提前提供任何帮助。
答案 0 :(得分:0)
我不知道这是否会有所帮助,特别是在这段时间之后,但我遇到了同样的问题并按如下方式解决了问题:
首先,我使用的设备是符合PHDC标准的康体佳健康设备。它没有标准的驱动程序。我有一个基于libusb-win32的驱动程序。我使用的usb4java使用了libusbK,这就是问题所在。我下载了Zadig http://zadig.akeo.ie/并用libusbK驱动程序替换了libusb-win32,我不再有“无法声明接口权限不足”的错误。
可能有一种方法可以使用usb4java的libusb-win32驱动程序,但我没有尝试这种方法。我很高兴得到一些工作!
我仍然无法获得插件事件,但是哪种情况很糟糕。