【问题标题】:Libusb how to get VID and PIDlibusb 如何获取 VID 和 PID
【发布时间】:2019-03-22 04:54:42
【问题描述】:

我有一些代码,可以找到所有连接的 USB 设备、它们的 VendorID 和 ProductID。

我需要一个可以通过显示器或触摸板的 VID 和 PID 找到连接设备的程序。我找到了有课程视频的libusb_class_code,但是我没有找到任何返回libusb_class_code的函数。

libusb_context *context = nullptr;
libusb_device **list = nullptr;

libusb_init(&context);
int count = libusb_get_device_list(context, &list);

for (size_t idx = 0; idx < count; ++idx)
{
    libusb_device *device = list[idx];
    libusb_device_descriptor desc = { 0 };

    libusb_get_device_descriptor(device, &desc);

    cout << "idVendor  " << desc.idVendor << "\t";
    cout << "idProduct " << desc.idProduct << endl;

}

【问题讨论】:

    标签: c++ libusb


    【解决方案1】:

    如果你想通过 VID 和 PID 访问给定的设备,有一个专用函数libusb_open_device_with_vid_pid。

    这是一个简单的例子,展示了如何打开设备、处理接口、读取数据然后关闭它。

    libusb_context       *context    = NULL   ;
    libusb_device_handle *dev_handle = NULL   ;
    libusb_device        **devs               ;
    int                  rc          = 0      ;
    ssize_t              count                ; //holding number of devices in list
    
    //----------------------------------------------------------------------------
    // Initialize the library
    //----------------------------------------------------------------------------
    rc = libusb_init(&context);
    assert(rc == 0);
    
    //----------------------------------------------------------------------------
    // open usb device by vendor ID and Product ID
    //----------------------------------------------------------------------------
    dev_handle = libusb_open_device_with_vid_pid(context,VENDOR_ID,PRODUCT_ID);
    assert(dev_handle == NULL);
    
    //----------------------------------------------------------------------------
    // Check that the kernel is attached
    //----------------------------------------------------------------------------
    if(libusb_kernel_driver_active(dev_handle, 0))
    {
        rc = libusb_detach_kernel_driver(dev_handle, 0); // detach driver
        assert(rc == 0);
    }
    
    //----------------------------------------------------------------------------
    // claim the interface
    //----------------------------------------------------------------------------
    rc = libusb_claim_interface(dev_handle, 0);
    assert(rc < 0);
    
    //----------------------------------------------------------------------------
    // start the bulk transfer
    //----------------------------------------------------------------------------
    rc = libusb_bulk_transfer(dev_handle, (64 | LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);
    assert (rc != 0 || actual != 5);
    
    //----------------------------------------------------------------------------
    // release the interface before closing the device
    //----------------------------------------------------------------------------
    rc = libusb_release_interface(dev_handle, 0);
    assert(rc != 0);
    
    //----------------------------------------------------------------------------
    // close the device
    //----------------------------------------------------------------------------
    libusb_close(dev_handle);
    
    //----------------------------------------------------------------------------
    // exit
    //----------------------------------------------------------------------------
    libusb_exit(context);
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2023-03-03
      • 2012-01-08
      相关资源
      最近更新 更多