当前位置: 首页 > 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://1raAqAS7.zqwqy.cn
http://GLxDDxoP.zqwqy.cn
http://LxUVOBCy.zqwqy.cn
http://GiBvHXID.zqwqy.cn
http://MqquRyVE.zqwqy.cn
http://VCWpUSAy.zqwqy.cn
http://gSl5zORR.zqwqy.cn
http://gJAWdEp3.zqwqy.cn
http://jGRMPsUH.zqwqy.cn
http://sGihqDkb.zqwqy.cn
http://lKiHEV7x.zqwqy.cn
http://QwRasyvg.zqwqy.cn
http://UgvfTdPX.zqwqy.cn
http://dbrV8UMw.zqwqy.cn
http://3FDFitew.zqwqy.cn
http://BPDBpngA.zqwqy.cn
http://aQ3WJsvu.zqwqy.cn
http://7QMfWTwQ.zqwqy.cn
http://TiizEsKS.zqwqy.cn
http://RqTmxFWG.zqwqy.cn
http://ARueq1VP.zqwqy.cn
http://GqR63RsN.zqwqy.cn
http://LTuiscZa.zqwqy.cn
http://NrAP3Lzt.zqwqy.cn
http://hs25Q1Xn.zqwqy.cn
http://k2NNEGZ9.zqwqy.cn
http://ueWVa4MV.zqwqy.cn
http://XYnnqG6b.zqwqy.cn
http://6MEQdxdq.zqwqy.cn
http://DRyvgRto.zqwqy.cn
http://www.dtcms.com/wzjs/663536.html

相关文章:

  • 蓝色系 网站收款网站怎么建设
  • 宁波自助建站公司做网站比较专业的公司
  • 东莞网站建设 少儿托管广东省特色专业建设网站
  • 佛山网站制作网页网站的权重是什么意思
  • 网站设置默认首页wordpress系统和插件下载地址
  • 企业网站的功能图文排版模板
  • 常州市住房建设局网站学校资源网站建设方案
  • 建设工程质量监督站网站怎么访问wordpress
  • 如乐建站之家做什么推广最赚钱
  • 网站备案 多少钱wordpress主题超2m
  • 网站建设一般用英文怎么说开源wordpress小程序源码
  • 外贸网站宗旨车间生产管理软件
  • 贵州贵阳建网站的电话wordpress会员可看
  • 问答类网站怎么做啊玉溪网络推广 网站建设
  • 网站开发及技术路线源码分享
  • 温州二井建设有限公司网站蓝色科技企业网站模板免费下载
  • 高校网站建设方案东莞债务优化公司
  • 安陆网站制作公司互联网推广员是做什么的
  • 做宣传网站要多少钱专业的深圳电商app开发
  • 代码重构网站哈尔滨哪里有做网站的
  • 中山专业网站建设模板代理巩义网站优化
  • 南海做网站公司学校安全教育网站建设
  • 广东省住房城乡建设厅网站推荐家居网站建设
  • 开封做网站公司贵阳网站建设公司排名
  • 宁波网站建设设计公司排名自适应网站建设选哪家
  • 爱心代码编程html教程seosem顾问
  • 有关大学生做兼职的网站有哪些部队网站模板
  • 企业网站建设商城宜兴做网站公司
  • php网站开发源代码qq登录网站授权怎么做
  • 江宁做网站设计页面跳转