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);
	else
		fprintf(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;
}

} // namespace

int test_libusb_hotplug()
{
	// reference: examples/hotplugtest.c
#ifdef _MSC_VER
	const char* platform = "Windows";
#else
	const char* platform = "Linux";
#endif

	int 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视频设备进行反复插拔

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

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
libusb 是一个用户空间的 USB 库,它可以帮助我们在 Linux、Windows 和 Mac OS X 等操作系统上访问 USB 设备。下面是一个简单的示例程序,演示如何使用 libusb 读取一个 USB 设备的描述符信息。 首先,我们需要安装 libusb 库。在 Ubuntu 上,可以使用以下命令安装: ``` sudo apt-get install libusb-1.0-0-dev ``` 接下来,我们编写一个 C 语言程序。程序的主要流程如下: 1. 初始化 libusb 库。 2. 打开 USB 设备。 3. 读取设备描述符信息。 4. 关闭 USB 设备。 5. 释放 libusb 库。 程序代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libusb-1.0/libusb.h> int main(int argc, char** argv) { libusb_device_handle* handle = NULL; libusb_context* context = NULL; unsigned char descriptor[1024]; int r; // 初始化 libusb 库 r = libusb_init(&context); if (r < 0) { fprintf(stderr, "libusb_init error %d\n", r); return 1; } // 打开 USB 设备 handle = libusb_open_device_with_vid_pid(NULL, 0x1234, 0x5678); if (handle == NULL) { fprintf(stderr, "libusb_open_device_with_vid_pid error\n"); goto exit; } // 读取设备描述符信息 r = libusb_get_descriptor(handle, LIBUSB_DT_DEVICE, 0, descriptor, sizeof(descriptor)); if (r < 0) { fprintf(stderr, "libusb_get_descriptor error %d\n", r); goto close; } // 打印设备描述符信息 printf("Device Descriptor:\n"); for (int i = 0; i < r; i++) { printf("%02x ", descriptor[i]); if ((i + 1) % 16 == 0) { printf("\n"); } } printf("\n"); close: // 关闭 USB 设备 libusb_close(handle); exit: // 释放 libusblibusb_exit(context); return 0; } ``` 在上面的程序,我们使用 libusb_open_device_with_vid_pid 函数打开 USB 设备,其的 VID 和 PID 分别是设备的厂商 ID 和产品 ID。我们使用 libusb_get_descriptor 函数读取设备描述符信息。最后,我们使用 libusb_close 函数关闭 USB 设备,使用 libusb_exit 函数释放 libusb 库。 注意:在运行程序之前,请将 VID 和 PID 替换为你自己的设备的厂商 ID 和产品 ID。
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值