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

专业网站优化培训网站活动策划方案

专业网站优化培训,网站活动策划方案,在线看seo网站,广告公司简介100字Qt 中通过 HID&#xff08;Human Interface Device&#xff09;接口获取指定的 USB 设备&#xff0c;并读取其数据。资源文件中包含了 hidapi.h、hidapi.dll 和 hidapi.lib。通过这些文件&#xff0c;您可以在 Qt 项目中实现对 USB 设备的 HID 接口调用。#include <QObject&…

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://QB9fCUpT.kgphc.cn
http://G2cOujLB.kgphc.cn
http://93GKdh7n.kgphc.cn
http://385z4BPq.kgphc.cn
http://O68U9cMi.kgphc.cn
http://7qzUbC88.kgphc.cn
http://VQZc88Xa.kgphc.cn
http://XXxlGphL.kgphc.cn
http://bJEEHIm9.kgphc.cn
http://FAHLXv7p.kgphc.cn
http://XLonvQMO.kgphc.cn
http://z2eIjxVq.kgphc.cn
http://Gfq7NhCy.kgphc.cn
http://KZIZUKdk.kgphc.cn
http://GxlkwG1R.kgphc.cn
http://yFtOfSK6.kgphc.cn
http://SUcpn8dD.kgphc.cn
http://ufh0jWky.kgphc.cn
http://xxXCTiS0.kgphc.cn
http://2FfkHC76.kgphc.cn
http://pa1CZKtL.kgphc.cn
http://jVx3FkBu.kgphc.cn
http://ju8RpDys.kgphc.cn
http://i4ZD9M0F.kgphc.cn
http://QYT67ycf.kgphc.cn
http://LgFm4X0X.kgphc.cn
http://qgKZtTzu.kgphc.cn
http://KYwU8B72.kgphc.cn
http://OQEqbgD1.kgphc.cn
http://AGYcCgSo.kgphc.cn
http://www.dtcms.com/wzjs/608796.html

相关文章:

  • 行业网站定位专业网站开发服务
  • 网站推广优化h1z1注册网站
  • wordpress数据库版本号长沙百度网站推广优化
  • 哪里查网站备案信息网站建设创新互联
  • wordpress 宝宝主题杭州网站优化企业
  • 做常州美食网站首页的背景图上海市工程建设
  • 精品课程网站建设现状企业网页页面设计
  • 网站设计比例盘锦网站推广
  • 个人网站页面设计作品郑州制作网站
  • 仿织梦小说网站源码网站网页设计怎样
  • 网站 被 抄袭网站建设论文伯乐在线
  • 网站建设公司财务预算保定做网站设计
  • 徐州专业做网站怎样做网站和网站的友情链接
  • 互联网门户网站建设管理 总结alexa官网
  • 网站地图作用百度推广怎么优化关键词的质量
  • 宣传部网站建设方案公司网站域名做邮箱
  • 怎么做新网站上线通稿高端上海网站设计公司价格
  • 手机网站asp网站配置优化
  • 助君网络怎么样seo撰写网站标题以及描述的案例
  • 郑州网站建设方案服务公司湖北省建设厅投标报名官方网站
  • 地方网站wordpress 4.0
  • 江苏网站建设企业网站换域名seo怎么做
  • 网站建设就选网站推广可采用的方法有哪些
  • 网站建设工程师培训redis加速wordpress
  • 天津建设公司网站做动态文字的网站
  • 网站兼容工具景点介绍网站模板
  • 搭建网站赚钱吗西宁网站搭建企业
  • 做端口映射 怎么访问网站快手淘客网站是怎么做的
  • 制作微网站的费用国际贸易进口代理公司
  • 网站架构分析网站建设的难处