Usb4java备用设置
我有一个设备,我必须通过USB与之通信 它有1个活动配置,即1个接口 该接口有更多的备用设置(IDLE、PROF1、PROF2)。默认情况下,IDLE处于活动状态 我的问题是,如何激活PROF2设置Usb4java备用设置,java,usb4java,Java,Usb4java,我有一个设备,我必须通过USB与之通信 它有1个活动配置,即1个接口 该接口有更多的备用设置(IDLE、PROF1、PROF2)。默认情况下,IDLE处于活动状态 我的问题是,如何激活PROF2设置 bNumConfigurations: 0x01 bNumInterfaces: 0x01 [IDLE] bInterfaceNumber: 0x00 bAlternateSetting: 0x00 [PROF1] bInterfaceNumber: 0x0
bNumConfigurations: 0x01
bNumInterfaces: 0x01
[IDLE]
bInterfaceNumber: 0x00
bAlternateSetting: 0x00
[PROF1]
bInterfaceNumber: 0x00
bAlternateSetting: 0x01
[PROF2]
bInterfaceNumber: 0x00
bAlternateSetting: 0x02
代码
UsbConfiguration config = (UsbConfiguration) device.getActiveUsbConfiguration();
UsbInterface iface = config.getUsbInterface((byte)0x00);
UsbInterface alt = iface.getSetting((byte)0x02); // <= Setting is not active.
UsbEndpoint endpoint = alt.getUsbEndpoint((byte)0x83);
UsbPipe pipe = endpoint.getUsbPipe();
pipe.open(); // <= Pipe is not active.
UsbConfiguration config=(UsbConfiguration)device.getActiveUsbConfiguration();
UsbInterface iface=config.getUsbInterface((字节)0x00);
UsbInterface alt=iface.getSetting((字节)0x02);// 我认为这里的问题在于,高级API根本不提供将活动配置或备用设置设置设置为默认设置以外的其他设置的方法
不过,低级别API不支持。。。我用的是:
// Iterate over the devices using low-level API to match a device + config combo from a high-level API
DeviceList list = new DeviceList();
LibUsb.getDeviceList(null, list);
for (Device d : list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
LibUsb.getDeviceDescriptor(d, descriptor);
if (descriptor.idVendor() == device.getUsbDeviceDescriptor().idVendor() &&
descriptor.idProduct() == device.getUsbDeviceDescriptor().idProduct()) {
Context context = new Context();
LibUsb.init(context);
DeviceHandle handle = new DeviceHandle();
LibUsb.open(d, handle);
LibUsb.setConfiguration(handle, 0x02); // or cfg.getUsbConfigurationDescriptor().bConfigurationValue()
LibUsb.setInterfaceAltSetting(handle, 0x00, 0x02);
LibUsb.claimInterface(handle, ifc); // valid test, can't claim unless active
LibUsb.releaseInterface(handle, ifc);
LibUsb.close(handle);
LibUsb.exit(context);
return; // break, etc
}
}
LibUsb.freeDeviceList(list, true); // usually in a finally block
当放置在助手函数中时,此逻辑对于设置活动配置应有效
根据libusb
devs,只要设备未拔出,配置将保持活动状态。这是每一个
最后,如果您还没有这样做,我建议通过命令行LIBUSB_DEBUG=4
设置DEBUG,以从LIBUSB
获取原始输出。这对我的故障排除工作有很大帮助。在这种情况下,您应该看到以下内容:
[21.987087] [0000d903] libusb: debug [libusb_set_configuration] configuration 2
如果你告诉我们你在用什么图书馆,也许对我们来说更容易些。