当前位置: 首页 > news >正文

Qt获取hid设备信息

Qt 中通过 HID(Human Interface Device)接口获取指定的 USB 设备,并读取其数据。资源文件中包含了 hidapi.h、hidapi.dll 和 hidapi.lib。通过这些文件,您可以在 Qt 项目中实现对 USB 设备的 HID 接口调用。

#include <QObject>
// #include <hidapi.h>
#include <QMap>
#include <QTimer>class GameControllerHID : public QObject
{Q_OBJECT
public:explicit GameControllerHID(QObject *parent = nullptr);~GameControllerHID();// 尝试连接游戏控制器Q_INVOKABLE bool connectController(unsigned short vendorId = 0, unsigned short productId = 0);Q_INVOKABLE void disconnectController();Q_INVOKABLE bool isConnected() const;// 按钮状态查询Q_INVOKABLE bool buttonState(int buttonId) const;Q_INVOKABLE QMap<int, bool> allButtonStates() const;// 摇杆状态查询 (返回 -1.0 到 1.0 的范围)Q_INVOKABLE double axisState(int axisId) const;Q_INVOKABLE QMap<int, double> allAxisStates() const;// 设置轮询间隔 (毫秒)Q_INVOKABLE void setPollInterval(int interval);signals:void controllerConnected();void controllerDisconnected();void buttonStateChanged(int buttonId, bool pressed);void axisStateChanged(int axisId, double value);void rawDataReceived(const QByteArray &data);private slots:void pollController();private:bool parseInputReport(const unsigned char *data, size_t length);void setupDefaultMapping();hid_device *m_device;QTimer *m_pollTimer;bool m_isConnected;// 设备信息unsigned short m_vendorId;unsigned short m_productId;QString m_manufacturer;QString m_product;// 输入状态QMap<int, bool> m_buttonStates;QMap<int, double> m_axisStates;// 输入报告缓冲区unsigned char *m_inputReportBuffer;size_t m_inputReportLength;// 设备映射配置struct AxisMapping {int byteOffset;int bitOffset;bool isSigned;double scale;double offset;};struct ButtonMapping {int byteOffset;int bitMask;};std::thread m_readThd;std::atomic_bool m_isStopThd = false;QMap<int, AxisMapping> m_axisMappings;QMap<int, ButtonMapping> m_buttonMappings;// bool isBut13StatChged = false;
};
bool GameControllerHID::connectController(unsigned short vendorId, unsigned short productId)
{if (m_isConnected) {disconnectController();}// 如果未指定VID/PID,尝试查找第一个游戏控制器if (vendorId == 0 || productId == 0) {struct hid_device_info *devs = hid_enumerate(0, 0);struct hid_device_info *cur_dev = devs;while (cur_dev) {// 简单判断是否是游戏控制器 (可根据需要调整)if (cur_dev->usage_page == 1 && cur_dev->usage == 5) {vendorId = cur_dev->vendor_id;productId = cur_dev->product_id;break;}cur_dev = cur_dev->next;}hid_free_enumeration(devs);if (vendorId == 0 || productId == 0) {qWarning() << "No game controller found";return false;}}// 打开设备m_device = hid_open(vendorId, productId, nullptr);if (!m_device) {qWarning() << "Failed to open HID device" << QString::number(vendorId, 16) << QString::number(productId, 16);return false;}// 获取设备信息wchar_t wstr[256];if (hid_get_manufacturer_string(m_device, wstr, 256) >= 0) {m_manufacturer = QString::fromWCharArray(wstr);}if (hid_get_product_string(m_device, wstr, 256) >= 0) {m_product = QString::fromWCharArray(wstr);}m_vendorId = vendorId;m_productId = productId;m_isConnected = true;// 设置默认映射 (需要根据实际控制器调整)setupDefaultMapping();// 开始轮询// m_pollTimer->start();emit controllerConnected();return true;
}

初始化 HID API: 在调用 HID 接口之前,首先需要初始化 HID API。

int res;
res = hid_init();

打开设备: 使用设备的 VID(Vendor ID)和 PID(Product ID)打开指定的 USB 设备。

wchar_t wstr[MAX_STR];
int i;
// Open the device using the VID, PID, and optionally the Serial number.
handle = hid_open(0x0483, 0x5750, NULL);
if(handle == NULL)
{qDebug() << "NULL-----------------------NULL";return;
}
else
{qDebug() << "not ------------NULL-----------------------NULL";
}

读取设备信息: 读取设备的制造商字符串、产品字符串和序列号字符串。

// Read the Manufacturer String
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
wprintf(L"Manufacturer String: %s\n", wstr);// Read the Product String
res = hid_get_product_string(handle, wstr, MAX_STR);
wprintf(L"Product String: %s\n", wstr);// Read the Serial Number String
res = hid_get_serial_number_string(handle, wstr, MAX_STR);
wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);// Read Indexed String 1
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
wprintf(L"Indexed String 1: %s\n", wstr);

读取数据: 设置非阻塞模式并循环读取设备数据。

qDebug("hid read start");
int res = hid_set_nonblocking(handle, 0);
while (1)
{res = hid_read(handle, buf, sizeof(buf));QString asd;for(int i = 0; i < sizeof(buf); i++){char str[20];sprintf(str, "%02x", buf[i]);asd += str;}if(!cardInfo.contains(asd.toUpper())){cardInfo.append(asd.toUpper());for(int i = 0; i < cardInfo.size(); i++){dealWithData(cardInfo[i]);}}
}

注意事项

  • 适用于 Windows 10 系统,使用 MSVC编译的 Release 版本。
  • 在使用过程中,请确保设备的 VID 和 PID 正确无误。
  • 读取数据时,建议使用非阻塞模式以避免程序卡死。
http://www.dtcms.com/a/282601.html

相关文章:

  • 抖音小程序开发如何避坑?如何接口调用与性能优化?
  • Nacos 开源 MCP Router,加速 MCP 私有化部署
  • 运维打铁: Ruby 脚本在运维自动化中的应用探索
  • leetcode 3201. 找出有效子序列的最大长度 I 中等
  • K8s 自定义调度器 Part1:通过 Scheduler Extender 实现自定义调度逻辑
  • AI Agent开发学习系列 - langchain之LCEL(1):LangChain LCEL链式编排与RAG增强实践
  • FunASR Paraformer-zh:高效中文端到端语音识别方案全解
  • Django ORM 查询工具对象详解
  • Linux 命令:uname
  • Scalefusion 与 EasyControl 对比:轻量级方案与全功能 IoT MDM 的深度碰撞
  • 前端面试题(HTML、CSS、JavaScript)
  • 在此处打开 powershell 窗口 shift + 右键 修改为 右键弹出
  • [硬件电路-28]:从简单到复杂:宇宙、芯片与虚拟世界的共通逻辑2
  • 1软件工程概念及其基本要素-思考题
  • Netty介绍和基本代码演示
  • 基于渐进式迁移学习网络(PTLN)​的小样本故障诊断模型
  • STM32CubeMX红外收发模块(串口)
  • H3CNE综合实验之五角星
  • 了解linux用户和权限
  • Linux开发利器:探秘开源,构建高效——基础开发工具指南(上)【包管理器/Vim】
  • springboot项目部署到K8S
  • Golang 并发快速上手
  • SingLoRA:单矩阵架构减半参数量,让大模型微调更稳定高效
  • ​HAProxy负载均衡集群概述
  • 逆向入门(39、40)程序逆向篇-DaNiEl-RJ.1、genocide1
  • 基于SSE传输的MCP服务器实现
  • LVS集群实践
  • 使用CodeQL挖掘Spring中的大量赋值漏洞
  • 行为模式-策略模式
  • 华大北斗TAU1201-1216A00高精度双频GNSS定位模块 自动驾驶专用