Loading... 为体验在Windows操作系统下带GUI调试快感,于是有了此文。 > 目标:搭建Windows下Qt开发调试环境,搭建Ubuntu下Qt编译到嵌入式(ARMv7)环境。 # 一、所需环境及文件 **ubuntu下** * qt-everywhere-src-{此处是版本号}.tar.xz。Qt源码压缩包,可到https://download.qt.io/archive/qt/下载。 * qt-opensource-linux-x64-{此处是版本号}.run。QtCreator,可到https://download.qt.io/archive/qt/下载,高版本需下载在线安装器安装。 * 相关编译器。比如`arm-linux-gnueabi-g++`或者`arm-linux-gnueabihf-g++` **Windows下** * qt-opensource-windows-x86-{此处是版本号}.exe。可到https://download.qt.io/archive/qt/下载,高版本需下载在线安装器安装。 # 二、Qt源码交叉编译 具体编译、安装配置过程可参考诸如:[Linux下的Qt环境搭建(arm交叉编译)](https://blog.csdn.net/u012902367/article/details/87876079)等进行。 # 三、windows下Qt安装 自行百度。 # 四、QtSerialPort测试 1. 创建Qt控制台程序,在.pro中添加`QT += serialport` 2. 添加自定义类`CMySerialPort`,包括`cmyserialport.h`和`cmyserialport.cpp` 3. 具体代码: cmyserialport.h ```c++ #ifndef CMYSERIALPORT_H #define CMYSERIALPORT_H #include <QObject> #include <QtSerialPort/qserialport.h> class CMySerialPort:QObject { public: CMySerialPort(); QSerialPort *m_serialPort; QString QByteToHexString(QByteArray data_); void ppp(); }; #endif // CMYSERIALPORT_H ``` cmyserialport.cpp ```c++ #include "cmyserialport.h" #include <QtSerialPort/qserialportinfo.h> #include <QtSerialPort/qserialport.h> #include <qdebug.h> CMySerialPort::CMySerialPort() { qDebug()<<"12122121"; m_serialPort = new QSerialPort; m_serialPort->setPortName("COM3");///dev/ttySZ3 if(!m_serialPort->open(QIODevice::ReadWrite))//用ReadWrite 的模式尝试打开串口 { qDebug()<<"打开失败!"; return; } qDebug()<<"串口打开成功!"; m_serialPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);//设置波特率和读写方向 m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位 m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制 m_serialPort->setParity(QSerialPort::EvenParity); //无校验位 m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位 connect(m_serialPort,&QSerialPort::readyRead,this,[=](){ auto bytes = m_serialPort->readAll(); qDebug()<<"recv:"<<QByteToHexString(bytes); }); } QString CMySerialPort::QByteToHexString(QByteArray data_) { QString ret(data_.toHex().toUpper());//转为16进制大写 int len = ret.length() / 2; for (int i = 1; i < len; i++) { ret.insert(2 * i + i - 1, " ");//编写格式 } return ret; } void CMySerialPort::ppp() { qDebug()<<"abc"; } ``` main.cpp ```c++ #include <QCoreApplication> #include "cmyserialport.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); CMySerialPort port; port.ppp(); return a.exec(); } ``` 在Windows下编译、运行发现能正常收取数据。 修改portname后,在ubuntu下编译、运行发现能正常收取数据。 # 五、QtMqtt测试 具体流程参考[Qt开发MQTT(一) 之Qt官方Qt MQTT](https://blog.csdn.net/luoyayun361/article/details/104671603) > 注意: > > 1.编译mqtt时,需要将源码目录mqtt文件夹下头文件复制到qt安装目录对应编译器目录下include文件夹下,比如我Qt安装与C盘,就在C:\Qt\Qt5.12.10\5.12.10\mingw73_32\include目录下新建QtMqtt文件夹,并将头文件拷贝过来。 > > 2.Qt Mqtt源码中自带的simpleclient示例:修改#include <QtMqtt/QMqttClient>为#include <QtMqtt/qmqttclient.h>或者自行增加QMqttClient文件,否则编译不通过。 最后修改:2021 年 08 月 13 日 12 : 21 AM © 禁止转载