当前位置: 首页> 游戏> 攻略 > 西安企业seo_微信小程序双人游戏情侣_湖南seo优化价格_seo实战培训中心

西安企业seo_微信小程序双人游戏情侣_湖南seo优化价格_seo实战培训中心

时间:2025/7/12 1:27:29来源:https://blog.csdn.net/simple_core/article/details/144026824 浏览次数:0次
西安企业seo_微信小程序双人游戏情侣_湖南seo优化价格_seo实战培训中心

Qt读写Usb设备的数据

  • 问题:要读取usb设备进行通讯,qt好像没有对应的库支持。
  • 解决:
    • libusb
      • window下载 :
      • Linux下载:
    • QtUsb 开源的第三方库
      • 库里面的函数说明:
      • window版本:
      • Linux中也提供的直接下载
      • 测试代码:
      • 库下载:

问题:要读取usb设备进行通讯,qt好像没有对应的库支持。

解决:

libusb

查看到,基本上使用的都是libusb这库,这个库是支持跨平台的,C语言的库。
官网 http://libusb.info/可以下载到,如下图:
在这里插入图片描述
可以下载源代码,和.7z后缀的window版本的库。

还有的是github的地址:https://github.com/libusb/libusb,如图:
在这里插入图片描述
可以下载对应的源码进行编译。里面有configure文件,编译用的。下载对应的依赖就行。

window下载 :

直接下载对应的relase版本的.7z文件就可以了。

Linux下载:

sudo apt-get install libusb-1.0-0-dev

可以使用对应的API与usb进行数据通讯。

QtUsb 开源的第三方库

dithub地址:https://github.com/fpoussin/QtUsb

这里面库,可以看到有依赖,libusb,hidapi这两个库。

库里面的函数说明:

地址:https://fpoussin.github.io/doxygen/qtusb/0.7.x/qusbendpoint.html

window版本:

提供了msvc编译器版本的直接的下载:
在这里插入图片描述

Linux中也提供的直接下载

不过我在使用Linux中的时候出现了问题,直接下载老是失败。只能是自己编译源代码了。

在这里插入图片描述
需要下载依赖:
libusb-1.0-0-dev, libhidapi-dev and pkg-config

不过我是编译成静态库,方便移动。

测试代码:

#include <QObject>
#include <QCoreApplication>
#include <QtSerialPort/QSerialPort>
#include <QUsbDevice>
#include <QUsbEndpoint>#include <QByteArray>
#include <QDebug>const quint8 USB_ENDPOINT_IN = 0x81; /* Bulk output endpoint for responses */
const quint8 USB_ENDPOINT_OUT = 0x01; /* Bulk input endpoint for commands */
const quint16 USB_TIMEOUT_MSEC = 300;class UsbTest : public QObject
{Q_OBJECT
public:UsbTest():m_write_ep(nullptr),m_read_ep(nullptr){usb = new QUsb;connect(usb,&QUsb::deviceInserted,[this](QUsb::Id id){qDebug()<<id;if(id.pid == 0x5721){m_filter = id;if (this->openDevice()) {qInfo("Device open!");this->write();} else {qWarning("Could not open device!");}}});m_usb_dev = new QUsbDevice(this);for(auto& elem : usb->devices()){if(elem.pid == 0x5721){m_filter = elem;m_usb_dev->setId(m_filter);m_config.alternate = 0;m_config.config = 1;m_config.interface = 0;m_usb_dev->setConfig(m_config);if (this->openDevice()) {qInfo("Device open!");this->write();} else {qWarning("Could not open device!");}}}for(int i=0;i<513;i++)m_send.append(static_cast<char>(i%10));}bool openDevice(){qDebug("Opening");if (m_usb_dev->open() == QUsbDevice::statusOK) {// Device is openreturn this->openHandle();}return false;}bool openHandle(){if(m_read_ep){disconnect(m_read_ep, SIGNAL(readyRead()), this, SLOT(onReadyRead()));disconnect(m_write_ep, SIGNAL(bytesWritten(qint64)), this, SLOT(onWriteComplete(qint64)));delete m_read_ep;delete m_write_ep;}qDebug("Opening Handle");bool a = false, b = false;m_read_ep = new QUsbEndpoint(m_usb_dev, QUsbEndpoint::bulkEndpoint, USB_ENDPOINT_IN);m_write_ep = new QUsbEndpoint(m_usb_dev, QUsbEndpoint::bulkEndpoint, USB_ENDPOINT_OUT);connect(m_read_ep, SIGNAL(readyRead()), this, SLOT(onReadyRead()));connect(m_write_ep, SIGNAL(bytesWritten(qint64)), this, SLOT(onWriteComplete(qint64)));a = m_read_ep->open(QIODevice::ReadOnly);if (a) {m_read_ep->setPolling(true);}b = m_write_ep->open(QIODevice::WriteOnly);return a && b;}void write(){if(!m_write_ep) return;qDebug()<<"写的状态:"<<m_write_ep->status();//if(m_write_ep->status() != QUsbEndpoint::transferCompleted)//    return;qDebug() << "Writing" << m_send << m_send.size();if (m_write_ep->write(m_send.constData(), m_send.size()) < 0) {qWarning("write failed");}}
public slots:void onReadyRead(){qDebug()<<"读取数据:"<<m_read_ep->readAll();}void onWriteComplete(qint64 id){qDebug()<<"onWriteComplete:"<<id;}private:QUsb* usb;QUsbDevice *m_usb_dev;QUsb::Id m_filter;QUsb::Config m_config;QByteArray m_send, m_recv;QUsbEndpoint *m_read_ep, *m_write_ep;
};

库下载:

版本:ubantu22.04 ,libusb 1.0 ,静态库。

关键字:西安企业seo_微信小程序双人游戏情侣_湖南seo优化价格_seo实战培训中心

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: