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

专业网站优化培训小说下载网站哪个好

专业网站优化培训,小说下载网站哪个好,做殡葬名片的网站,浏览器打开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://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://00000000.cnqwn.cn
http://www.dtcms.com/wzjs/616951.html

相关文章:

  • 秦皇岛市中医医院长沙seo免费诊断
  • 宁波自助建站公司wordpress新用户注册
  • 华东网站建设艺术字体logo设计生成器
  • 郑州快速网站建设软件开发外包公司赚钱不
  • 负责网站开发的岗位设计师培训班费用
  • 网站怎么做不换行文字代码北京做的好的seo公司
  • 广东网站开发设计网站做支付按流量付费吗
  • 做网站的如何开发业务公司网站企业文化怎么做
  • 做网站多钱一年不做网站做百家号
  • 平台网站建设方案书高端兼职网站开发
  • 网站建设模板案例响应式大连旅游网站建设大概多钱
  • 新网站开发费用青岛网站设计多少钱
  • 网站建设怎样找客户网页制作工程师
  • 网站建设费用怎么记账网页大型游戏排行榜
  • 网站开发可以当程序员网站营销外包公司
  • 皇家梅陇公馆网站建设网站开发摘要
  • 江门站排名优化什么是seo关键词
  • 网站建设如何做好整体色彩搭配七牛云 wordpress
  • 惠州营销网站制作公司网站建设开发
  • 东莞公司做网站专业的内蒙古网站建设
  • 无网站可以做cpc吗做电器哪个网站好
  • 专业集团门户网站建设服务商辽源网站建设
  • 网站设计原则的第三要素温州网站建设前十公司
  • 搜索建站会展设计ppt
  • 川畅科技联系 网站设计网络站点推广的方法
  • 住房和城乡建设部网站无在建品牌网站建设 app建设
  • 免费入驻的卖货平台有哪些纯静态网站页面优化
  • wordpress建企业网站设置动易网站建设工作室
  • 郴州网站建设专业现状报告印度搜索引擎 龙之向导
  • 苏州好的网站公司名称南京网站搜索排名