我有一些代码,可以找到所有已连接的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;
}
答案 0 :(得分:0)
如果要通过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);