(libusb) usb口自动刷新

文章目录

  • libusb
  • 自动刷新程序Code
    • 目录结构
    • Code
      • 项目文件
      • `usb`包
      • `code`包
  • 效果
  • 描述
    • 重置reset
    • 热拔插
    • 使用
  • END

libusb

在操作USB相关内容时,有一个比较著名的库就是libusb

官方网址:libusb

在这里插入图片描述

下载

  • 下载源码
  • 官方编好的库
  • github:Releases · libusb/libusb (github.com)

在这里插入图片描述

在这里插入图片描述

使用:libusb: Application Programming Interface (sourceforge.io)

  • Functions
  • Structures
  • Enums

在这里插入图片描述

自动刷新程序Code

这里介绍一个基于libusb自动刷新usb的demo。

目录结构

  • 3rdparty
    • libusb的头文件和库
    • 这里采用官方编好的现成的库
  • usb
    • 基于C++对libusb的封装
    • 此包为纯C++代码
  • code
    • 基于Qt的ui和线程
E:.
└─usbReset│  main.cpp│  usbReset.pro│├─3rdparty│  └─libusb│      ├─include│      │  └─libusb-1.0│      │          libusb.h│      ││      └─MinGW32│          ├─dll│          │      libusb-1.0.dll│          │      libusb-1.0.dll.a│          ││          └─static│                  libusb-1.0.a│├─code│      THREAD_TimerUsb.cpp│      THREAD_TimerUsb.h│      WIDGET_Main.cpp│      WIDGET_Main.h│      WIDGET_Main.ui│└─usbusb.priUSB_Hotplug.cppUSB_Hotplug.hUSB_Reset.cppUSB_Reset.h

Code

项目文件

usbReset.pro

QT += core
QT += widgets#CONFIG += console
CONFIG += c++17DESTDIR = $$PWD/bininclude($$PWD/usb/usb.pri)INCLUDEPATH += $$PWD/code
HEADERS += \code/THREAD_TimerUsb.h \code/WIDGET_Main.hSOURCES += \main.cpp \code/THREAD_TimerUsb.cpp \code/WIDGET_Main.cppFORMS += \code/WIDGET_Main.ui

usb/usb.pri

# 链接到libusb(采用静态库)
INCLUDEPATH += $$PWD/../3rdparty/libusb/include
HEADERS += $$PWD/../3rdparty/include/libusb/libusb-1.0/libusb.h
LIBS += -L$$PWD/../3rdparty/libusb/MinGW32/static/ -llibusb-1.0# 当前封装的包
INCLUDEPATH += $$PWD/..
HEADERS += \$$PWD/USB_Hotplug.h \$$PWD/USB_Reset.hSOURCES += \$$PWD/USB_Hotplug.cpp \$$PWD/USB_Reset.cpp

main.cpp

#include <QApplication>#include "WIDGET_Main.h"int main(int argc, char *argv[]) {QApplication app(argc, argv);MainWidget form;form.show();return app.exec();
}

usb

USB_Reset.h

#ifndef MYUSB_H_1682212693
#define MYUSB_H_1682212693extern "C" {
#include "libusb-1.0/libusb.h"
}
#include <string>
#include <vector>namespace USB {class Reset final {
public:inline static const int EMPTY_POINTER_INT = 114514;private:/// for init & exitstatic libusb_context* context;static uint32_t        obj_count;public:static int Reset_context();private:char str[1024]{};public:/// libusb_init()Reset();/// libusb_exit()~Reset();public:::std::vector<::std::string> Show_device();public:libusb_device_descriptor Find_descriptByidVendor(uint32_t Vendor);public:/*** vendor -> device* -> handle** handle* -> reset* handle* -> close*/libusb_device*        Find_deviceByidVendor(uint32_t Vendor);libusb_device_handle* Get_handleByOpenDevice(libusb_device* device);int  Reset_usbByHandle(libusb_device_handle* device_handle);void Close_deviceByHandle(libusb_device_handle* device_handle);
};}  // namespace USB
#endif  // MYUSB_H_1682212693

USB_Reset.cpp

#include "USB_Reset.h"namespace USB {
/*** @brief UsbBase::context* static data* context: 所有对象共享这一个全局的上下文对象* obj_count: 引用计数*/
libusb_context* Reset::context   = nullptr;
uint32_t        Reset::obj_count = 0;/*** @brief UsbBase::Reset_context* @return* static 重置上下文*/
int Reset::Reset_context() {/// 有实体对象才重置 contextif (0 != obj_count) {if (nullptr != context) {libusb_exit(context);context = nullptr;}return libusb_init(&context);}return LIBUSB_SUCCESS;
}/*** @brief UsbBase::UsbBase* constructor* 上下文的init* 维护引用计数*/
Reset::Reset() {/// 查看版本号const struct libusb_version* version = libusb_get_version();printf(">>>Libusb-Version:%s\n", version->describe);printf(">>>Libusb-Version:%d.%d.%d.%d\n",version->major,version->minor,version->micro,version->nano);/// 第一个对象,或者之前没有注册成功if (0 == obj_count || nullptr == context) {if (int res = libusb_init(&context); res != 0) {sprintf(str, "fail to init: %d\n", res);/// TODO/// 根据实际情况,日志、断言、异常等/// TODO}}obj_count += 1;
}/*** @brief UsbBase::~UsbBase* distructor* 维护引用计数* 上下文的退出*/
Reset::~Reset() {obj_count += -1;if (0 == obj_count) {if (nullptr != context) {libusb_exit(context);context = nullptr;}}
}/*** @brief UsbBase::show_device* just show device-list message*/
::std::vector<::std::string> Reset::Show_device() {::std::vector<::std::string> messageList;messageList.push_back("********************** show device-list message BEGIN ""**********************");/// help datalibusb_device** deviceList = nullptr;libusb_get_device_list(nullptr, &deviceList);for (int i = 0; deviceList[i] != nullptr; i += 1) {auto                            device = deviceList[i];struct libusb_device_descriptor descript;int ret = libusb_get_device_descriptor(device, &descript);if (LIBUSB_SUCCESS != ret) {continue;}sprintf(str,"*""idVendor:%6d ""idProduct:%6d ""bDeviceClass:%6d ""(bus:%3d, device:%3d)""*",descript.idVendor, descript.idProduct, descript.bDeviceClass,libusb_get_bus_number(device),libusb_get_device_address(device));messageList.push_back(str);}messageList.push_back("********************** show device-list message END ""************************");return messageList;
}/*** @brief MyUsb::Find_descriptByidVendor* @param idVendor* @return* 获取设备描述对象*/
libusb_device_descriptor Reset::Find_descriptByidVendor(uint32_t idVendor) {/// retlibusb_device_descriptor descriptor;/// help datalibusb_device_handle* deviceHandle = nullptr;libusb_device**       deviceList   = nullptr;libusb_get_device_list(nullptr, &deviceList);for (int i = 0; deviceList[i] != nullptr; i += 1) {auto device = deviceList[i];int  ret    = libusb_get_device_descriptor(device, &descriptor);if (LIBUSB_SUCCESS != ret) {continue;}if (descriptor.idVendor == idVendor) {const int isOpen = libusb_open(device, &deviceHandle);if (LIBUSB_SUCCESS == isOpen) {libusb_close(deviceHandle);break;}}}return descriptor;
}/*** @brief UsbBase::Find_deviceByidVendor* @param Vendor* @return* 获取device*/
libusb_device* Reset::Find_deviceByidVendor(uint32_t Vendor) {/// retlibusb_device* device = nullptr;/// help datalibusb_device_handle* deviceHandle = nullptr;libusb_device**       deviceList   = nullptr;libusb_get_device_list(nullptr, &deviceList);for (int i = 0; deviceList[i] != nullptr; i += 1) {device = deviceList[i];libusb_device_descriptor descriptor;int ret = libusb_get_device_descriptor(device, &descriptor);if (LIBUSB_SUCCESS != ret) {continue;}if (descriptor.idVendor == Vendor) {const int isOpen = libusb_open(device, &deviceHandle);if (LIBUSB_SUCCESS == isOpen) {libusb_close(deviceHandle);break;} else {device = nullptr;}} else {device = nullptr;}}return device;
}/*** @brief UsbBase::Get_handleByOpenDevice* @param device* @return* 根据传入的设备指针* 通过open操作* 获取句柄指针*/
libusb_device_handle* Reset::Get_handleByOpenDevice(libusb_device* device) {if (nullptr == device) {return nullptr;}libusb_device_handle* deviceHandle = nullptr;const int             isOpen       = libusb_open(device, &deviceHandle);if (LIBUSB_SUCCESS == isOpen) {return deviceHandle;} else {return nullptr;}
}/*** @brief UsbBase::Reset_usbByHandle* @param device_handle* @return* 通过句柄重置设备* 为0则表示成功*/
int Reset::Reset_usbByHandle(libusb_device_handle* device_handle) {if (nullptr == device_handle) {return EMPTY_POINTER_INT;}const int isReset = libusb_reset_device(device_handle);//! TODO if (isReset ?= 0) => logreturn isReset;
}/*** @brief UsbBase::Close_deviceByHandle* @param device_handle* 手动通过句柄指针关闭设备*/
void Reset::Close_deviceByHandle(libusb_device_handle* device_handle) {if (nullptr == device_handle) {return;}libusb_close(device_handle);
}
}  // namespace USB

USB_Hotplug.h

#ifndef HOTPLUG_H_27452998650
#define HOTPLUG_H_27452998650extern "C" {
#include "libusb-1.0/libusb.h"
}namespace USB {
class Hotplug final {
private:static libusb_device_handle *m_deviceHandle;private:static int LIBUSB_CALL usb_arrived_callback(struct libusb_context *ctx,struct libusb_device  *dev,libusb_hotplug_event   event,void *userdata);static int LIBUSB_CALL usb_left_callback(struct libusb_context *ctx,struct libusb_device  *dev,libusb_hotplug_event   event,void                  *userdata);private:char str[1024]{};private:libusb_hotplug_callback_handle usb_arrived_handle;libusb_hotplug_callback_handle usb_left_handle;libusb_context                *context = nullptr;private:uint32_t m_regiest_vendor      = LIBUSB_HOTPLUG_MATCH_ANY;uint32_t m_regiest_product     = LIBUSB_HOTPLUG_MATCH_ANY;uint32_t m_regiest_deviceClass = LIBUSB_HOTPLUG_MATCH_ANY;private:Hotplug();public:Hotplug(uint32_t vendor, uint32_t product, uint32_t deviceClass);~Hotplug();public:int Register_arrived();int Register_left();
};
}  // namespace USB#endif  // HOTPLUG_H_27452998650

USB_Hotplug.cpp

#include "USB_Hotplug.h"#include <iostream>namespace USB {
/*** @brief Hotplug::m_deviceHandle* 主要用于静态的回调函数*/
libusb_device_handle *Hotplug::m_deviceHandle = nullptr;/*** @brief Hotplug::usb_arrived_callback* @param ctx* @param dev* @param event* @param userdata* @return* 热拔插 arrive 的回调*/
int Hotplug::usb_arrived_callback(libusb_context *ctx, libusb_device *dev,libusb_hotplug_event event, void *userdata) {struct libusb_device_handle    *handle;struct libusb_device_descriptor desc;unsigned char                   buf[512];int                             rc;libusb_get_device_descriptor(dev, &desc);printf("Add usb device: \n");printf("\tCLASS(0x%x) SUBCLASS(0x%x) PROTOCOL(0x%x)\n", desc.bDeviceClass,desc.bDeviceSubClass, desc.bDeviceProtocol);printf("\tVENDOR(0x%x) PRODUCT(0x%x)\n", desc.idVendor, desc.idProduct);rc = libusb_open(dev, &handle);if (LIBUSB_SUCCESS != rc) {printf("Could not open USB device\n");return 0;}memset(buf, 0, sizeof(buf));rc = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, buf,sizeof(buf));if (rc < 0) {printf("Get Manufacturer failed\n");} else {printf("\tManufacturer: %s\n", buf);}memset(buf, 0, sizeof(buf));rc = libusb_get_string_descriptor_ascii(handle, desc.iProduct, buf,sizeof(buf));if (rc < 0) {printf("Get Product failed\n");} else {printf("\tProduct: %s\n", buf);}memset(buf, 0, sizeof(buf));rc = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf,sizeof(buf));if (rc < 0) {printf("Get SerialNumber failed\n");} else {printf("\tSerialNumber: %s\n", buf);}libusb_close(handle);return 0;
}/*** @brief Hotplug::usb_left_callback* @param ctx* @param dev* @param event* @param userdata* @return* 热拔插left的回调*/
int Hotplug::usb_left_callback(libusb_context *ctx, libusb_device *dev,libusb_hotplug_event event, void *userdata) {struct libusb_device_descriptor desc;libusb_get_device_descriptor(dev, &desc);const int isReset = libusb_reset_device(m_deviceHandle);return isReset;
}/*** @brief Hotplug::Hotplug* 构造的时候init*/
Hotplug::Hotplug() {libusb_init(&context);
}/*** @brief Hotplug::Hotplug* @param vendor* @param product* 委托构造*/
Hotplug::Hotplug(uint32_t vendor, uint32_t product, uint32_t deviceClass) : Hotplug() {/// data{m_regiest_vendor      = vendor;m_regiest_product     = product;m_regiest_deviceClass = deviceClass;}/// find{libusb_device **m_deviceList;libusb_get_device_list(nullptr, &m_deviceList);for (int i = 0; m_deviceList[i] != nullptr; i += 1) {auto                            device = m_deviceList[i];struct libusb_device_descriptor descript;int ret = libusb_get_device_descriptor(device, &descript);if (ret < 0) {sprintf(str,"Error libusb_get_device_descriptor idx = %d res = %d\n", i,ret);}if (descript.idVendor == vendor && descript.idProduct == product) {const int isOpen =libusb_open(m_deviceList[i], &m_deviceHandle);if (LIBUSB_SUCCESS == isOpen) {break;}}}}  /// find/// test{Register_arrived();Register_left();}
}/*** @brief Hotplug::~Hotplug* 析构的时候注销和释放句柄*/
Hotplug::~Hotplug() {libusb_hotplug_deregister_callback(context, usb_arrived_handle);libusb_hotplug_deregister_callback(context, usb_left_handle);libusb_exit(context);
}/*** @brief Hotplug::Register_arrived* @return* 注册热拔插arrive*/
int Hotplug::Register_arrived() {int res = libusb_hotplug_register_callback(context, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_NO_FLAGS,m_regiest_vendor, m_regiest_product, m_regiest_deviceClass,Hotplug::usb_arrived_callback, NULL, &usb_arrived_handle);return res;
}/*** @brief Hotplug::Register_left* @return* 注册热拔插left*/
int Hotplug::Register_left() {int res = libusb_hotplug_register_callback(context, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_NO_FLAGS,m_regiest_vendor, m_regiest_product, m_regiest_deviceClass,Hotplug::usb_left_callback, NULL, &usb_left_handle);return res;
}
}  // namespace USB

code

WIDGET_Main.h

#ifndef FORM_H_27453073957
#define FORM_H_27453073957#include <QScopedPointer>
#include <QVector>
#include <QWidget>#include "THREAD_TimerUsb.h"namespace Ui {
class MainWidget;
}class MainWidget : public QWidget {Q_OBJECTprivate:QScopedPointer<Ui::MainWidget> ui;private:TimerUsb m_usb;public:explicit MainWidget(QWidget *parent = nullptr);~MainWidget();private:void show_usbDeviceMsgAll();void start_work();
};#endif  // FORM_H_27453073957

WIDGET_Main.cpp

#include "WIDGET_Main.h"#include <QDebug>
#include <QMutex>
#include <QThread>
#include <QTimer>#include "ui_WIDGET_Main.h"
#include "usb/USB_Reset.h"QWidget         *g_widgetDisplayBoard = nullptr;
QtMessageHandler g_oldMessageHandler  = nullptr;
QMutex           g_dispalyMutex;/*** debug 重定向*/
void newDebugHandlerFunc(QtMsgType type, const QMessageLogContext &context, const QString &msg) {QMutexLocker locker(&g_dispalyMutex);if (g_widgetDisplayBoard) {dynamic_cast<QTextEdit *>(g_widgetDisplayBoard)->append(msg);} else {g_oldMessageHandler(type, context, msg);}
}/*** @brief MainWidget::MainWidget* @param parent* 1. debug重定向* 2. connect*/
MainWidget::MainWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MainWidget{}) {ui->setupUi(this);setWindowTitle("usb controller");/// debug -> widget{g_widgetDisplayBoard       = ui->textEdit;g_oldMessageHandler = qInstallMessageHandler(newDebugHandlerFunc);}/// connect{connect(ui->btn_showAll, &QPushButton::clicked, this,&MainWidget::show_usbDeviceMsgAll);connect(ui->btn_clear, &QPushButton::clicked, ui->textEdit,&QTextEdit::clear);connect(ui->btn_start, &QPushButton::clicked, this,&MainWidget::start_work);connect(&m_usb, &TimerUsb::signal_message, this,[](const QString &msg) { qInfo() << msg; });}/// before exe{ show_usbDeviceMsgAll(); }
}/*** @brief MainWidget::~MainWidget* 将线程安全关闭*/
MainWidget::~MainWidget() {if (m_usb.isRunning()) {m_usb.quit();m_usb.wait();}
}/*** @brief MainWidget::show_usbDeviceMsgAll* 展示所有usb设备的信息* 因为设计的是通用库* 因此返回的是std::vector<std::string>*/
void MainWidget::show_usbDeviceMsgAll() {for (auto &&s : USB::Reset().Show_device()) {qDebug() << s.c_str();}
}/*** @brief MainWidget::start_work* 检测线程启动*/
void MainWidget::start_work() {uint   vendor = ui->edit_Vendor->text().toUInt();double time   = ui->edit_timeout->text().toDouble();m_usb.Set_usbDeviceVendor(vendor);m_usb.Set_timerInterval(time * 1000);m_usb.Set_timerStart();if (false == m_usb.isRunning()) {qDebug() << "=== start before ===";m_usb.start();qDebug() << "=== start after ===";}
}

WIDGET_Main.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWidget</class><widget class="QWidget" name="MainWidget"><property name="geometry"><rect><x>0</x><y>0</y><width>726</width><height>480</height></rect></property><property name="windowTitle"><string>UsbForm</string></property><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QTextEdit" name="textEdit"><property name="styleSheet"><string notr="true"/></property></widget></item><item><widget class="QWidget" name="widget" native="true"><layout class="QGridLayout" name="gridLayout"><item row="0" column="1"><widget class="QLineEdit" name="edit_Vendor"><property name="font"><font><pointsize>12</pointsize></font></property><property name="text"><string>8746</string></property></widget></item><item row="0" column="0"><widget class="QLabel" name="label_Vendor"><property name="font"><font><pointsize>12</pointsize></font></property><property name="text"><string>目标Vendor</string></property></widget></item><item row="1" column="0"><widget class="QLabel" name="label_timeout"><property name="font"><font><pointsize>12</pointsize></font></property><property name="text"><string>倒计时(秒)</string></property></widget></item><item row="1" column="1"><widget class="QLineEdit" name="edit_timeout"><property name="font"><font><pointsize>12</pointsize></font></property><property name="text"><string>2</string></property></widget></item><item row="2" column="0"><widget class="QPushButton" name="btn_showAll"><property name="font"><font><pointsize>18</pointsize></font></property><property name="text"><string>设备usb列表</string></property></widget></item><item row="2" column="1"><widget class="QPushButton" name="btn_clear"><property name="font"><font><pointsize>18</pointsize></font></property><property name="text"><string>清空列表</string></property></widget></item></layout></widget></item><item><widget class="QPushButton" name="btn_start"><property name="font"><font><pointsize>18</pointsize></font></property><property name="text"><string>开始</string></property></widget></item></layout></widget><resources/><connections/>
</ui>

THREAD_TimerUsb.h

#ifndef THREADUSB_H_70403004403
#define THREADUSB_H_70403004403#include <QThread>
#include <QTimer>class TimerUsb : public QThread {Q_OBJECT
private:uint32_t m_usbDeviceVendor = 0;private:QTimer m_timer;int    m_timerIntervalMsec = 1500;public:TimerUsb();public:void Set_usbDeviceVendor(uint32_t vendor);public:void Set_timerInterval(int msec = 1500);void Set_timerStart();void Set_timerStop();bool Is_timerRunning();protected:void run() override;signals:void signal_message(const QString&);
};#endif  // THREADUSB_H_70403004403

THREAD_TimerUsb.cpp

#include "THREAD_TimerUsb.h"#include <QDebug>
#include <QTimer>#include "usb/USB_Reset.h"#if 0
#define DEBUG_MODEL qInfo() <<
#else
#define DEBUG_MODEL emit this->signal_message
#endif/*** @brief ThreadUsb::ThreadUsb* construct*/
TimerUsb::TimerUsb() {Set_timerInterval();
}/*** @brief ThreadUsb::Set_usbDeviceVendor* @param vendor* 根据 vendor 查询设备*/
void TimerUsb::Set_usbDeviceVendor(uint32_t vendor) {this->m_usbDeviceVendor = vendor;
}/*** @brief ThreadUsb::Set_timerInterval* @param msec* 设置reset间隔*/
void TimerUsb::Set_timerInterval(int msec) {this->m_timerIntervalMsec = msec;m_timer.setInterval(m_timerIntervalMsec);
}/*** @brief TimerUsb::Set_timerStart* 启动定时器,但不启动线程*/
void TimerUsb::Set_timerStart() {this->m_timer.start();
}/*** @brief TimerUsb::Set_timerStop* 关闭定时器,但不关闭线程*/
void TimerUsb::Set_timerStop() {this->m_timer.stop();
}/*** @brief TimerUsb::Is_timerRunning* @return* 定时器是否运行*/
bool TimerUsb::Is_timerRunning() {return this->m_timer.isActive();
}/*** @brief ThreadUsb::run* 定时器timeout一次,usb-reset一次*/
void TimerUsb::run() {USB::Reset     usb;libusb_device* device = nullptr;/// 为了防止刚启动的时候没有获得/// 高强度轮询获取const size_t loopDeviceCount   = 1e5 + 10;const size_t loopContextPeriod = 1e3;for (size_t i = 0; i < loopDeviceCount; i += 1) {device = usb.Find_deviceByidVendor(this->m_usbDeviceVendor);if (nullptr != device) {break;} else {if (i % loopContextPeriod == 0) {DEBUG_MODEL("device is null & context resert");USB::Reset::Reset_context();}}}if (nullptr == device) {DEBUG_MODEL("libusb_device is null & Thread end!");return;} else {DEBUG_MODEL("libusb_device require ok!");}libusb_device_handle* handle = usb.Get_handleByOpenDevice(device);if (handle == nullptr) {DEBUG_MODEL("libusb_device require is null & Thread end!");return ;} else {DEBUG_MODEL("libusb_device_handle require ok!");}auto con = connect(&this->m_timer, &QTimer::timeout, [&]() {int res = usb.Reset_usbByHandle(handle);if (LIBUSB_SUCCESS == res) {DEBUG_MODEL("reset Success");} else {DEBUG_MODEL("reset Error; errorType = " + QString::number(res));}/// 句柄不归还,持续重置// usb.Close_deviceByHandle(handle);});/// 开启事件循环exec();this->m_timer.stop();disconnect(con);
}

效果

在这里插入图片描述

界面功能比较简单,基本就是widget中的代码,设置好vendor和倒计时后点击开始即可。

目前身边没有可以测试的usb设备,因此不展示具体效果。

其中USB::Reset是经过测试可用的。

描述

本demo主要就是libusb的封装,然后是对于封装的简单调用。

重置reset

基本思路:vendor->device*->handle*

然后使用handle*进行reset和最后的close

  • 获取device*
    • 获取设备序列libusb_get_device_list()
    • 遍历序列,获取每个设备的描述信息libusb_get_device_descriptor()
    • 对比描述信息,确认是哪个device*。并测试是否能正常open。
  • 获取handle*
    • 通过libusb_open()即测试打开的同时就能获取
  • 使用handle*进行reset
    • 使用libusb_reset_device()
  • 关闭handle*
    • 使用libusb_close()

注意:有的vendor是一样的编号,请根据实际的情景和需求改变具体的查找规则。

热拔插

热拔插部分没有测试,不做重点描述。

但是基本原理就是注册拔&插的回调函数。

libusb_hotplug_register_callback()

  • 标记:LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
  • 标记:LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT

使用

全在QThread::run()函数中。

在实际作用时,可能因为物理设备实际问题,导致设备指针和句柄的获取失败。

因此可以设置一个比较大的循环,无脑获取多次,直到获取成功,但若多次获取失败,则直接视为失败了。

然后启动一个定时器,

注意请不要close句柄。因为设备的实际请款,可能关闭后就再获取不到了,只要不随便乱插,设备标号和句柄是不会变的,因此直接保留好。直到真正不需要时再关闭(根据实际业务和逻辑需求)。




END

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/499678.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

@Slf4j 变量log找不到符号,可能是 Gradle 配置文件写得有问题

Slf4j 变量log找不到符号 鄙人在学习 Java 的 spring boot 项目时, 常常因为 maven 配置文件使用 xml 格式过于复杂, 所以更倾向于使用 gradle 作为构建工具. 然而, 在使用 gradle 作为构建工具时, 又需要引用 Lombok 依赖. 有时忘记在初始化项目时添加上 Lombok 依赖, 所以经…

本届挑战赛亚军方案:基于大模型和多AGENT协同的运维

“轻舟已过万重山团队”荣获本届挑战赛亚军&#xff0c;该团队来自华为集团IT-UniAI 产品和openEuler系统智能团队。 方案介绍 自ChatGPT问世以来&#xff0c;AI迎来了奇点iPhone时刻&#xff0c;这一年来大模型深入影响企业办公&#xff0c;金融&#xff0c;广告&#xff0c;…

SDWAN异地组网难在哪?怎么解决?

SD-WAN作为一种先进的网络技术&#xff0c;为企业提供了更加灵活和高效的网络连接方案。然而&#xff0c;在异地组网的过程中&#xff0c;SD-WAN也面临一些挑战。本文将探讨SD-WAN异地组网所面临的难题&#xff0c;并提供相应的解决方案。 挑战一&#xff1a;网络延迟和不稳定性…

ptython迭代器与生成器

迭代器 Python中的迭代器&#xff08;Iterator&#xff09;是一种强大的工具&#xff0c;用于访问集合元素。它是一种可以记住遍历位置的对象&#xff0c;这意味着迭代器不会一次性生成所有的元素&#xff0c;而是可以等到需要的时候才生成&#xff0c;从而节省了大量的内存资…

智慧城市:打造宜居环境,引领未来可持续发展

随着科技的不断进步与创新&#xff0c;我们的城市正步入一个崭新的时代——智慧城市。智慧城市是指运用信息技术和大数据等现代科技手段&#xff0c;对城市基础设施、公共服务和社会管理进行智能化改造&#xff0c;实现城市各领域的智能化、信息化和高效化。今天&#xff0c;就…

SQL函数学习记录

聚合函数 函数是编程语言的基础之一&#xff0c;在对数字的运算中&#xff0c;我们用的最多的就是聚合函数&#xff0c;本篇接下来就详细阐述下SQL中聚合函数的运用。 什么是聚合函数&#xff08;aggregate function&#xff09;&#xff1f; 聚合函数指的是对一组值执行计算…

Pytorch模型训练后静态量化并加载int8量化模型推理

目录 一、源码包准备1.1 源码包获取1.2 代表性验证集1.3 Pytorch模型1.4 推理测试图片 二、环境准备三、模型转换3.1 参数修改3.2 代码3.3 量化转换结果3.4 量化前后模型大小对比 四、量化模型推理4.1 参数修改4.2 代码4.3 推理结果4.4推理时间 五、总结 一、源码包准备 1.1 源…

什么是Sectigo?如何优惠申请?

Sectigo&#xff0c;全球领先的SSL/TLS证书提供商&#xff0c;以其卓越的安全性能和广泛的认可度赢得了业界的一致好评。我们的证书不仅能加密您的网站通信&#xff0c;确保敏感信息传输过程中的绝对安全&#xff0c;还能显著提升您的网站信誉&#xff0c;让访客一眼就能识别出…

价格战打响!阿里云服务器和腾讯云服务器价格对比

2024年阿里云服务器和腾讯云服务器价格战已经打响&#xff0c;阿里云服务器优惠61元一年起&#xff0c;腾讯云服务器62元一年&#xff0c;2核2G3M、2核4G、4核8G、8核16G、16核32G、16核64G等配置价格对比&#xff0c;阿腾云atengyun.com整理阿里云和腾讯云服务器详细配置价格表…

什么是VR紧急情况模拟|消防应急虚拟展馆|VR游戏体验馆加盟

VR紧急情况模拟是利用虚拟现实&#xff08;Virtual Reality&#xff0c;简称VR&#xff09;技术来模拟各种紧急情况和应急场景的训练和演练。通过VR技术&#xff0c;用户可以身临其境地体验各种紧急情况&#xff0c;如火灾、地震、交通事故等&#xff0c;以及应对这些紧急情况的…

第三百七十四回

文章目录 1. 概念介绍2. 实现方法2.1 基本用法2.2 特殊用法 3. 示例代码4. 内容总结 我们在上一章回中介绍了"分享三个使用TextField的细节"相关的内容&#xff0c;本章回中将介绍如何让Text组件中的文字自动换行.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1.…

leetcode刷题(剑指offer) 46.全排列

46.全排列 给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]示例 2&#xff1a; 输入&#…

STM32F103学习笔记(七) PWR电源管理(原理篇)

目录 1. PWR电源管理简介 2. STM32F103的PWR模块概述 2.1 PWR模块的基本工作原理 2.2 电源管理的功能和特点 3. PWR模块的常见应用场景 4. 常见问题与解决方案 1. PWR电源管理简介 PWR&#xff08;Power&#xff09;模块是STM32F103系列微控制器中的一个重要组成部分&…

C语言 变量

变量其实只不过是程序可操作的存储区的名称。C 中每个变量都有特定的类型&#xff0c;类型决定了变量存储的大小和布局&#xff0c;该范围内的值都可以存储在内存中&#xff0c;运算符可应用于变量上。 变量的名称可以由字母、数字和下划线字符组成。它必须以字母或下划线开头…

导出数据库表结构到文档中

导出效果&#xff1a; 完整代码&#xff1a; Controller层&#xff1a; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotatio…

Android Stdio Execution failed for task ‘:app:compileDebugKotlin‘ 报错解决

具体报错信息如下&#xff1a; compileDebugJavaWithJavac task (current target is 1.8) and compileDebugKotlin task (current target is 17)jvm target compatibility should be set to the same Java version.很显然&#xff0c;这是一个版本冲突问题&#xff0c;compile…

云上攻防-云服务篇弹性计算服务器云数据库实例元数据控制角色AK控制台接管

知识点: 1、云服务-弹性计算服务器-元数据&SSRF&AK 2、云服务-云数据库-外部连接&权限提升 章节点&#xff1a; 云场景攻防&#xff1a;公有云&#xff0c;私有云&#xff0c;混合云&#xff0c;虚拟化集群&#xff0c;云桌面等 云厂商攻防&#xff1a;阿里云&am…

Tomcat服务部署

1、安装jdk、设置环境变量并测试 第一步&#xff1a;安装jdk 在部署 Tomcat 之前必须安装好 jdk&#xff0c;因为 jdk 是 Tomcat 运行的必要环境。 1. #关闭防火墙 systemctl stop firewalld systemctl disable firewalld setenforce 02. #将安装 Tomcat 所需软件包传到/opt…

90%电商APP已沦落至无人下载,用户主观意愿——是真正实用性价值!

90%电商APP已沦落至无人下载&#xff0c;用户主观意愿——是真正实用性价值&#xff01; 文丨微三云营销总监胡佳东&#xff0c;点击上方“关注”&#xff0c;为你分享市场商业模式电商干货。 - 引言&#xff1a;在互联网发展的大时代下&#xff0c;似乎每个月都有新的APP出现…

Linux Shell脚本练习(一)

一、 Linux下执行Shell脚本的方式&#xff1a; 1、用shell程序执行脚本&#xff1a; a、根据你的shell脚本的类型&#xff0c;选择shell程序&#xff0c;常用的有sh&#xff0c;bash&#xff0c;tcsh等 b、程序的第一行#!/bin/bash里面指明了shell类型的&#xff0c;比如#!/…