usb4java USB错误4:无法打开USB设备:
我正在尝试与PS3的DS3控制器接口。我使用usb4java USB错误4:无法打开USB设备:,java,usb,libusb,usb4java,Java,Usb,Libusb,Usb4java,我正在尝试与PS3的DS3控制器接口。我使用libusb的一个实现在C中成功地实现了这一点,但我决定将实现转移到java。不幸的是,我向java的迁移并不顺利。该设备似乎在设备列表中找到,但当我尝试打开它时,会出现以下错误“USB错误4:无法打开USB设备:没有此类设备(可能已断开连接)” 公共类主{ 专用静态最终短视频=0x054c; 专用静态最终短PID=0x0268; 语境; 公用干管(){ 上下文=新上下文(); int result=LibUsb.init(上下文); 如果(结果!=L
libusb
的一个实现在C
中成功地实现了这一点,但我决定将实现转移到java
。不幸的是,我向java的迁移并不顺利。该设备似乎在设备列表中找到,但当我尝试打开它时,会出现以下错误“USB错误4:无法打开USB设备:没有此类设备(可能已断开连接)”
公共类主{
专用静态最终短视频=0x054c;
专用静态最终短PID=0x0268;
语境;
公用干管(){
上下文=新上下文();
int result=LibUsb.init(上下文);
如果(结果!=LibUsb.SUCCESS){
抛出新的LibUsbException(“无法初始化libusb.”,结果);
}
ByteBuffer数据=ByteBuffer.allocate(49);
DeviceHandle ds3Handle=getDeviceHandle(findDevice(VID,PID));
控制传输(ds3Handle,(字节)0xa1,(字节)0x1,(短)0x101,(短)0,数据,1000L);
LibUsb.exit(上下文);
}
专用设备查找设备(int vid、int pid){
设备UsbDevice=null;
设备列表=新设备列表();
int result=LibUsb.getDeviceList(上下文,列表);
如果(结果<0){
抛出新的LibUsbException(“无法获取设备列表”,结果);
}
试一试{
用于(设备:列表){
DeviceDescriptor描述符=新的DeviceDescriptor();
结果=LibUsb.getDeviceDescriptor(设备,描述符);
如果(结果!=LibUsb.SUCCESS){
抛出新的LibUsbException(“无法读取设备描述符”,结果);
}
if(descriptor.idVendor()==vid&&descriptor.idProduct()==pid){
UsbDevice=设备;
System.out.println(“找到”);
}
}
}最后{
LibUsb.freeDeviceList(list,true);
}
返回UsbDevice;
}
专用设备句柄getDeviceHandle(设备设备){
DeviceHandle=新的DeviceHandle();
int result=LibUsb.open(设备、句柄);
如果(结果!=LibUsb.SUCCESS){
抛出新的libusbeexception(“无法打开USB设备”,结果);
}
返回手柄;
}
公共静态void main(字符串[]args){
新的Main();
}
}
LibUsb.freeDeviceList(list,true)
这就是问题所在。javadoc中显示了“final boolean unrefDevices”。在您有机会打开设备之前,您的代码正在释放该设备。仅更改为false是不够的,您还需要使用需要返回的设备调用refDevice 例如:
可以确认,这是OP问题的解决方案。刚刚使用
usb4java
页面中的相同示例代码遇到了相同的问题。非常感谢!他们的网站在GitHub上。发布了一个PR。看起来这个错误确实是由设备的发布引起的,但是我该如何解决这个问题呢?当我把我的“真”改为“假”时,它会产生另一个错误。
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();
}
}
} finally {
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, false);
}
if (deviceFound != null) {
// Device found
LibUsb.refDevice(deviceFound);
}
return deviceFound;