gpt4 book ai didi

java - usb4java USB 错误 4 : Unable to open USB device:

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:45 29 4
gpt4 key购买 nike

我正在尝试连接 PS3 的 DS3 Controller 。我已经使用 libusb 的实现在 C# 中做到了这一点,但我决定将我的实现移至 java。不幸的是,我转向 Java 并不是那么顺利。该设备似乎已在设备列表中找到,但当我尝试打开它时出现以下错误 “USB 错误 4:无法打开 USB 设备:没有此类设备(它可能已断开连接)”

public class Main {
private static final short VID = 0x054c;
private static final short PID = 0x0268;

Context context;

public Main() {
context = new Context();
int result = LibUsb.init(context);

if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to initialize libusb.", result);
}

ByteBuffer data = ByteBuffer.allocate(49);
DeviceHandle ds3Handle = getDeviceHandle(findDevice(VID, PID));
LibUsb.controlTransfer(ds3Handle, (byte)0xa1, (byte)0x1, (short)0x101, (short)0, data, 1000L);

LibUsb.exit(context);
}

private Device findDevice(int vid, int pid) {
Device UsbDevice = null;
DeviceList list = new DeviceList();
int result = LibUsb.getDeviceList(context, list);

if (result < 0) {
throw new LibUsbException("Unable to get device list", result);
}

try {
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);
}

if (descriptor.idVendor() == vid && descriptor.idProduct() == pid) {
UsbDevice = device;
System.out.println("found");
}
}
} finally {
LibUsb.freeDeviceList(list, true);
}

return UsbDevice;
}

private DeviceHandle getDeviceHandle(Device device) {
DeviceHandle handle = new DeviceHandle();
int result = LibUsb.open(device, handle);

if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to open USB device", result);
}

return handle;
}

public static void main(String [] args){
new Main();
}
}

最佳答案

LibUsb.freeDeviceList(list, true);

true 就是问题所在。 “final boolean unrefDevices”显示在 javadoc 中。在您有机会打开设备之前,您的代码正在释放设备。

关于java - usb4java USB 错误 4 : Unable to open USB device:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26012951/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com