【问题标题】:How to print list of connected devices如何打印已连接设备的列表
【发布时间】:2013-05-12 14:50:19
【问题描述】:

请看以下代码,该代码用于获取连接的 USB 设备列表

#include <lusb0_usb.h>
#include <iostream>

using namespace std;

int main()
{
    usb_init();
    usb_find_busses();
    usb_find_devices();

    struct usb_bus *busses = usb_get_busses();
    struct usb_bus *bus;
    struct usb_device *dev;


    for(bus=busses;bus;bus=bus->next)
    {
        for(dev=bus->devices;dev;dev->next)
        {
            cout << dev->descriptor.iProduct << endl;
        }
    }
}

当我运行这段代码时,我得到了

Starting C:\Users\yohan\Documents\QTPeojects\USB-build-Desktop_Qt_5_0_0_beta2_MSVC2010_32bit_SDK-Release\release\USB.exe...
C:\Users\yohan\Documents\QTPeojects\USB-build-Desktop_Qt_5_0_0_beta2_MSVC2010_32bit_SDK-Release\release\USB.exe exited with code 0

我相信我错误地执行了此代码。我该如何纠正这个问题?

【问题讨论】:

    标签: c++ windows visual-studio-2010 qt libusb


    【解决方案1】:

    它可能运行良好。您需要暂停控制台才能看到输出。

    尝试添加

    #include <conio.h> 
    
    _getch();
    

    或使用其他技术暂停并等待用户输入。

    【讨论】:

      【解决方案2】:

      以下代码在 Objective-C 中工作:

      libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
      ssize_t cnt; //holding number of devices in list
      //libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
      cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
      if(cnt < 0) {
          return false;
          //there was an error
      }
      //print total number of usb devices
      ssize_t i; //for iterating through the list
      for(i = 0; i < cnt; i++) {
          struct libusb_device_descriptor desc;
          if (libusb_get_device_descriptor(devs[i], &desc) >= 0) {
              if ((desc.idVendor == 0x04D8) && (desc.idProduct == 0x0204)){
                  libusb_free_device_list(devs, 1);
                  return true;
              }
          }
      }
      libusb_free_device_list(devs, 1); //free the list, unref the devices in it
      
      return false;
      

      我相信你需要改用“descriptor.i d Product”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-25
        • 1970-01-01
        • 1970-01-01
        • 2014-12-17
        • 2014-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多