小言_互联网的博客

libusb中的热插拔使用举例

557人阅读  评论(0)

以下为判断usb设备是插入还是拔出状态(热插拔)的测试代码: 在Windows下是不支持的,在Linux是支持的,下一个版本可能会支持Windows下的热插拔:


  
  1. #include <chrono>
  2. #include <thread>
  3. #include <iostream>
  4. #include <libusb.h>
  5. namespace {
  6. bool running = true;
  7. int LIBUSB_CALL hotplug_callback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, void* user_data)
  8. {
  9. struct libusb_device_descriptor desc;
  10. int ret = libusb_get_device_descriptor(dev, &desc);
  11. if (LIBUSB_SUCCESS != ret) {
  12. fprintf( stderr, "fail to get device descriptor: %d, %s\n", ret, libusb_error_name(ret));
  13. //return -1;
  14. }
  15. if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
  16. fprintf( stdout, "device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
  17. else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
  18. fprintf( stdout, "device detached: %04x:%04x\n", desc.idVendor, desc.idProduct);
  19. else
  20. fprintf( stderr, "Error: unsupported hotplug events\n");
  21. return 0;
  22. }
  23. void run()
  24. {
  25. for ( int i = 0; i < 60; ++i)
  26. std::this_thread::sleep_for( std::chrono::seconds( 1));
  27. running = false;
  28. }
  29. } // namespace
  30. int test_libusb_hotplug()
  31. {
  32. // reference: examples/hotplugtest.c
  33. #ifdef _MSC_VER
  34. const char* platform = "Windows";
  35. #else
  36. const char* platform = "Linux";
  37. #endif
  38. int ret = libusb_init( nullptr);
  39. if (ret != 0) {
  40. fprintf( stderr, "fail to init: %d, %s\n", ret, libusb_error_name(ret));
  41. return -1;
  42. }
  43. if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
  44. fprintf( stderr, "hotplug capabilites are not supported on this platform: %s\n", platform);
  45. libusb_exit( nullptr);
  46. return -1;
  47. }
  48. int vendor_id = 0x046d, product_id = 0x081b;
  49. libusb_hotplug_callback_handle hp[ 2];
  50. ret = libusb_hotplug_register_callback( nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_NO_FLAGS, vendor_id,
  51. product_id, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, nullptr, &hp[ 0]);
  52. if (LIBUSB_SUCCESS != ret) {
  53. fprintf( stderr, "fail to register callback arrived: %d, %s\n", ret, libusb_error_name(ret));
  54. libusb_exit( nullptr);
  55. return -1;
  56. }
  57. ret = libusb_hotplug_register_callback( nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_NO_FLAGS, vendor_id,
  58. product_id, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, nullptr, &hp[ 1]);
  59. if (LIBUSB_SUCCESS != ret) {
  60. fprintf( stderr, "fail to register callback left: %d, %s\n", ret, libusb_error_name(ret));
  61. libusb_exit( nullptr);
  62. return -1;
  63. }
  64. std:: thread th(run);
  65. while (running) {
  66. //ret = libusb_handle_events(nullptr);
  67. timeval tv = { 1, 0};
  68. ret = libusb_handle_events_timeout( nullptr, &tv);
  69. if (ret < 0)
  70. fprintf( stderr, "fail to libusb_handle_events: %d, %s\n", ret, libusb_error_name(ret));
  71. }
  72. libusb_exit( nullptr);
  73. th.join();
  74. return 0;
  75. }

在Windows下执行结果如下:

在Linux下执行结果如下:对usb视频设备进行反复插拔

GitHubhttps://github.com//fengbingchun/OpenCV_Test


转载:https://blog.csdn.net/fengbingchun/article/details/105767565
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场