【问题标题】:Usb4java alternate settingUsb4java 备用设置
【发布时间】:2016-03-02 13:34:53
【问题描述】:

我有一个设备,我必须通过 USB 与之通信。

它有 1 个活动配置,有 1 个接口。

界面有更多的备用设置(IDLE、PROF1、PROF2)。默认情况下,IDLE 处于活动状态。

我的问题是,我怎样才能激活 PROF2 设置?

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.

【问题讨论】:

  • 如果您告诉我们您使用的是什么库,我们可能会更容易。

标签: java usb4java


【解决方案1】:

我认为这里的问题是高级 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 开发人员的说法,只要设备未拔出,配置就会保持活动状态。这是每个https://github.com/libusb/libusb/issues/158#issuecomment-190501281

最后,如果您还没有这样做,我建议您通过命令行LIBUSB_DEBUG=4 设置DEBUG 以从libusb 获取原始输出。这对我的故障排除工作有很大帮助。在这种情况下,您应该会看到以下内容:

[21.987087] [0000d903] libusb: debug [libusb_set_configuration] configuration 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多