2

我正在通过 Eclipse 使用 Windows 10 和 Java。我对编程很陌生,并且一直在从事一个需要从 USB 设备读取数据的项目。

当我运行 LibUsb.open(device, Handle) 时,我收到以下错误:USB 错误 3:无法打开 USB 设备:访问被拒绝(权限不足)

我已经运行了 zadig 程序来修复一些其他错误,但我似乎无法解决这个问题。希望有人能帮助我吗?

我的代码如下(FindDevice 工作正常,只是在运行结果 = LibUsb.open(device, handle) 时结果 = USB 错误 3:无法打开 USB 设备:访问被拒绝(权限不足)

我在谷歌上找不到太多帮助。

                    public void actionPerformed(ActionEvent arg0) {


             //Initialize the libusb
            context = new Context();
            int result = LibUsb.init(context);
            if (result != LibUsb.SUCCESS) {
                throw new LibUsbException("Unable to initialize libusb.", result);
            }
            //Find the Device on the System
            short vendorId = (short)1545;
            short productId = (short)797;
            Device device = findDevice(vendorId,productId);
            DeviceHandle handle = new DeviceHandle();
            //Create the DeviceHandle
            //Open the Test Device  

            result = LibUsb.open(device, handle);
            if (result != LibUsb.SUCCESS) {
                throw new LibUsbException("Unable to open USB device", result);
            }
            try
            {
                // Use device handle here
                System.out.println("Does this happen?");
                //claimDevice(vendorId, productId);
            }
            finally
            {
                LibUsb.close(handle);
            }           



            LibUsb.exit(context);
        }
    public Device findDevice(short vendorId, short productId)
{
    // Read the USB device list
    DeviceList list = new DeviceList();
    int result = LibUsb.getDeviceList(null, list);
    if (result < 0) throw new LibUsbException("Unable to get device list", result);

    try
    {
        // Iterate over all devices and scan for the right one
        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);

            //Below is only executed when trying to figure out the Vendor ID and Descripter ID.
            //System.out.println("VendorID:" + descriptor.idVendor());
            //System.out.println("Descripter ID:" + descriptor.idProduct());

            if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) {
                System.out.println("Found Device!");
                return device;

            }
        }
    }
    finally
    {
        // Ensure the allocated device list is freed

    }

    // Device not found
    return null;
}
4

1 回答 1

2

遇到了同样的问题。问题似乎是我没有触发LibUsb.freeDeviceList(devs, unref_devices);,当我手动断开USB设备并在新连接时成功连接它时我想通了。

于 2018-05-07T16:11:46.447 回答