【问题标题】:Listing USB devices using libusb win32使用 libusb win32 列出 USB 设备
【发布时间】:2014-08-25 15:12:06
【问题描述】:

我要做的就是列出使用 libusb win32 的 USB 设备,但我遇到了很多麻烦。我正在使用 c++ 和 minGW。这是我的简单程序,可以编译但不会列出任何内容。如果有人能告诉我从这里去哪里,将不胜感激。

cout << "\nFinding Devices..." << endl;
    struct usb_bus *busses;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    busses = usb_get_busses();
    usb_set_debug(3);
    struct usb_bus *bus;
    int  c, i, a;
    for (bus = busses; bus; bus->next){
        struct usb_device *dev;

        for (dev = bus->devices; dev; dev = dev->next){
            if (dev->descriptor.bDeviceClass == 7){
                usb_dev_handle usb_open(struct usb_device *dev);
                int usb_set_configuration(usb_dev_handle *dev, int configuration);
            }}}

【问题讨论】:

  • 在第一个 for 循环中尝试 bus = bus-&gt;next
  • 我希望上面的代码可以无限循环(当然,假设您的计算机一个 USB 子系统...)
  • 我还建议检查函数的返回码是一种很好的编程习惯,而不是仅仅假设它们已经成功。 usb_find_bussesusb_find_devices 都返回他们找到的总线/设备的数量(在第一次调用时)。您可能应该检查这不是零。
  • 我检查了,我猜该程序没有找到任何总线。我显然已经将我的设备插入计算机,所以你能帮我解决这个问题。 @tom
  • 你修复了我指出的第一个错误吗?

标签: c++ libusb


【解决方案1】:

我猜可能发生的情况是您的设备具有类 0 - 这意味着该类是在接口级别定义的,而不是在设备级别。例如,这是lsusb 为我的 Microsoft 键盘报告的内容:

Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0         8
  idVendor           0x045e Microsoft Corp.
  idProduct          0x07f8 
  bcdDevice            3.00
  iManufacturer           1 
  iProduct                2 
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           59
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xa0
      (Bus Powered)
      Remote Wakeup
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      1 Boot Interface Subclass
      bInterfaceProtocol      1 Keyboard
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
...

查看设备如何将bDeviceClass 设置为0 (Defined at Interface level),并且每个接口都有自己的bInterfaceClass 设置?

找出发生了什么的最简单方法是找到适用于 Windows 的 libusb.exe 并使用 -v 标志运行它以转储每个设备的详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2018-08-25
    • 1970-01-01
    • 2013-08-01
    • 2012-11-05
    相关资源
    最近更新 更多