日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

libusb中的热插拔使用举例

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 libusb中的热插拔使用举例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下為判斷usb設備是插入還是拔出狀態(熱插拔)的測試代碼:?在Windows下是不支持的,在Linux是支持的,下一個版本可能會支持Windows下的熱插拔:

#include <chrono>
#include <thread>
#include <iostream>
#include <libusb.h>namespace {bool running = true;int LIBUSB_CALL hotplug_callback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, void* user_data)
{struct libusb_device_descriptor desc;int ret = libusb_get_device_descriptor(dev, &desc);if (LIBUSB_SUCCESS != ret) {fprintf(stderr, "fail to get device descriptor: %d, %s\n", ret, libusb_error_name(ret));//return -1;}if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)fprintf(stdout, "device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)fprintf(stdout, "device detached: %04x:%04x\n", desc.idVendor, desc.idProduct);elsefprintf(stderr, "Error: unsupported hotplug events\n");return 0;
}void run()
{for (int i = 0; i < 60; ++i)std::this_thread::sleep_for(std::chrono::seconds(1));running = false;
}} // namespaceint test_libusb_hotplug()
{// reference: examples/hotplugtest.c
#ifdef _MSC_VERconst char* platform = "Windows";
#elseconst char* platform = "Linux";
#endifint ret = libusb_init(nullptr);if (ret != 0) {fprintf(stderr, "fail to init: %d, %s\n", ret, libusb_error_name(ret));return -1;}if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {fprintf(stderr, "hotplug capabilites are not supported on this platform: %s\n", platform);libusb_exit(nullptr);return -1;}int vendor_id = 0x046d, product_id = 0x081b;libusb_hotplug_callback_handle hp[2];ret = libusb_hotplug_register_callback(nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_NO_FLAGS, vendor_id,product_id, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, nullptr, &hp[0]);if (LIBUSB_SUCCESS != ret) {fprintf(stderr, "fail to register callback arrived: %d, %s\n", ret, libusb_error_name(ret));libusb_exit(nullptr);return -1;}ret = libusb_hotplug_register_callback(nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_NO_FLAGS, vendor_id,product_id, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, nullptr, &hp[1]);if (LIBUSB_SUCCESS != ret) {fprintf(stderr, "fail to register callback left: %d, %s\n", ret, libusb_error_name(ret));libusb_exit(nullptr);return -1;}std::thread th(run);while (running) {//ret = libusb_handle_events(nullptr);timeval tv = {1, 0};ret = libusb_handle_events_timeout(nullptr, &tv);if (ret < 0)fprintf(stderr, "fail to libusb_handle_events: %d, %s\n", ret, libusb_error_name(ret));}libusb_exit(nullptr);th.join();return 0;
}

在Windows下執行結果如下:

在Linux下執行結果如下:對usb視頻設備進行反復插拔

GitHub:https://github.com//fengbingchun/OpenCV_Test

總結

以上是生活随笔為你收集整理的libusb中的热插拔使用举例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。