libusb如何获得vid和pid

Libusb how to get VID and PID

本文关键字:pid vid 何获得 libusb      更新时间:2023-10-16

我有一些代码,可以找到所有连接的USB设备,其供应商和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;
}

如果要通过vid and 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);