usb4java库,在windows计算机上声明接口时出错

aurhwmvo  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(507)

使用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)

谢谢你的帮助。

soat7uwm

soat7uwm1#

我不知道这样做是否有帮助,尤其是在经历了这么长时间之后,但我遇到了同样的问题,并解决了如下问题:
首先,我使用的设备是一个符合phdc标准的continua健康设备。它没有标准的驱动程序。我有一个基于libusb-win32的驱动程序。我使用的usb4java使用libusbk,这就是问题所在。我下载了zadighttp://zadig.akeo.ie/ 并用libusbk驱动程序替换libusb-win32,我不再收到“不能声明接口权限不足”错误。
可能有一种方法可以将libusb-win32驱动程序与usb4java一起使用,但我没有尝试这种方法。我很高兴能找到工作!
我仍然无法获得插件事件,但哪种臭味。

相关问题