从本篇开始,将会记录自己关于QT开发相关的学习记录以及demo
今天分享的是如何使用QT开发一个简单的串口助手
开发环境
Ubantu18.0.4 + QT5.12
1.配置文件中增加serialport模块
MyApplication.pro
QT += core gui serialport
2.绘制布局
如下图所示
布局代码:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>mainwindow</class><widget class="QWidget" name="mainwindow"><property name="geometry"><rect><x>0</x><y>0</y><width>789</width><height>535</height></rect></property><property name="windowTitle"><string>SWY Serial Port Assiant</string></property><layout class="QGridLayout" name="gridLayout_2"><item row="0" column="0" rowspan="2"><layout class="QGridLayout" name="gridLayout"><item row="0" column="0"><widget class="QLabel" name="label"><property name="text"><string>Receive Area</string></property></widget></item><item row="1" column="0" colspan="2"><widget class="QTextEdit" name="editReceive"><property name="readOnly"><bool>true</bool></property></widget></item><item row="2" column="0"><widget class="QLabel" name="label_2"><property name="text"><string>Configuration</string></property></widget></item><item row="3" column="0" rowspan="2"><layout class="QVBoxLayout" name="verticalLayout"><item><layout class="QHBoxLayout" name="horizontalLayout_2"><item><widget class="QLabel" name="label_3"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="text"><string>Serial Port</string></property></widget></item><item><widget class="QComboBox" name="cbSerialPort"><property name="minimumSize"><size><width>80</width><height>0</height></size></property></widget></item></layout></item><item><layout class="QHBoxLayout" name="horizontalLayout_3"><item><widget class="QLabel" name="label_4"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="text"><string>Baud Rate</string></property></widget></item><item><widget class="QComboBox" name="cbBaudRate"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="currentIndex"><number>2</number></property><item><property name="text"><string>4800</string></property></item><item><property name="text"><string>9600</string></property></item><item><property name="text"><string>115200</string></property></item></widget></item></layout></item><item><layout class="QHBoxLayout" name="horizontalLayout_4"><item><widget class="QLabel" name="label_5"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="text"><string>Data Bits</string></property></widget></item><item><widget class="QComboBox" name="cbDataBits"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="currentIndex"><number>3</number></property><item><property name="text"><string>5</string></property></item><item><property name="text"><string>6</string></property></item><item><property name="text"><string>7</string></property></item><item><property name="text"><string>8</string></property></item></widget></item></layout></item><item><layout class="QHBoxLayout" name="horizontalLayout_5"><item><widget class="QLabel" name="label_6"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="text"><string>Stop Bits</string></property></widget></item><item><widget class="QComboBox" name="cbStopBits"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><item><property name="text"><string>1</string></property></item><item><property name="text"><string>1.5</string></property></item><item><property name="text"><string>2</string></property></item></widget></item></layout></item><item><layout class="QHBoxLayout" name="horizontalLayout_6"><item><widget class="QLabel" name="label_7"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><property name="text"><string>Parity</string></property></widget></item><item><widget class="QComboBox" name="cbParity"><property name="minimumSize"><size><width>80</width><height>0</height></size></property><item><property name="text"><string>none</string></property></item></widget></item></layout></item></layout></item><item row="3" column="1"><widget class="QTextEdit" name="editSend"/></item><item row="4" column="1"><layout class="QHBoxLayout" name="horizontalLayout"><item><widget class="QPushButton" name="btnOpen"><property name="text"><string>Open</string></property></widget></item><item><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="btnClose"><property name="text"><string>Close</string></property></widget></item><item><spacer name="horizontalSpacer_2"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="btnSend"><property name="text"><string>Send</string></property></widget></item><item><spacer name="horizontalSpacer_3"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="btnClear"><property name="text"><string>Clear</string></property></widget></item></layout></item></layout></item></layout></widget><resources/><connections/>
</ui>
3.增加信号槽
private slots:void on_btnOpen_clicked();void on_btnClose_clicked();void on_btnSend_clicked();void on_btnClear_clicked();void slot_serialPort_ready_read();
分别对应着打开串口、关闭串口、发送消息、清空屏幕和接收消息五个信号
分析:
1.开启串口:需要做的就是串口初始化,具体包含连接的串口名、波特率、数据为、停止位和校验位的设置,最终是开启串口
2.关闭串口:调用串口对象的关闭方法
3.发送数据:将输入框中的内容,调用串口的write方法
4.接收数据:将串口发送的数据展示到接收区域内
5.清空屏幕:调用两个textEdit组件的clear方法
4.完整代码
4.1 头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QWidget>
#include <QSerialPort>
#include <QString>namespace Ui {
class mainwindow;
}class mainwindow : public QWidget
{Q_OBJECTpublic:explicit mainwindow(QWidget *parent = nullptr);~mainwindow();QSerialPort *serialPort;private slots:void on_btnOpen_clicked();void on_btnClose_clicked();void on_btnSend_clicked();void on_btnClear_clicked();void slot_serialPort_ready_read();private:Ui::mainwindow *ui;
};#endif // MAINWINDOW_H
4.2 cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPortInfo>
#include <QMessageBox>mainwindow::mainwindow(QWidget *parent) :QWidget(parent),ui(new Ui::mainwindow)
{ui->setupUi(this);serialPort = new QSerialPort();QStringList serialPortNameList;foreach (const QSerialPortInfo info, QSerialPortInfo::availablePorts()) {serialPortNameList<<info.portName();}ui->cbSerialPort->addItems(serialPortNameList);connect(serialPort,SIGNAL(readyRead()),this,SLOT(slot_serialPort_ready_read()));
}mainwindow::~mainwindow()
{delete ui;
}void mainwindow::on_btnOpen_clicked()
{QSerialPort::BaudRate baudRate;QSerialPort::DataBits dataBits;QSerialPort::StopBits stopBits;QSerialPort::Parity parity;QString strBaudRate = ui->cbBaudRate->currentText();if("4800" == strBaudRate){baudRate = QSerialPort::Baud4800;}else if("4800" == strBaudRate){baudRate = QSerialPort::Baud9600;}else if("115200" == strBaudRate){baudRate = QSerialPort::Baud115200;}QString strDataBits = ui->cbDataBits->currentText();if("5" == strDataBits){dataBits = QSerialPort::Data5;}else if("6" == strDataBits){dataBits = QSerialPort::Data6;}else if("7" == strDataBits){dataBits = QSerialPort::Data7;}else if("8" == strDataBits){dataBits = QSerialPort::Data8;}QString strStopBits = ui->cbStopBits->currentText();if("1" == strStopBits){stopBits = QSerialPort::OneStop;}else if("1.5" == strStopBits){stopBits = QSerialPort::OneAndHalfStop;}else if("2" == strStopBits){stopBits = QSerialPort::TwoStop;}QString strParity = ui->cbParity->currentText();if("none"==strParity){parity = QSerialPort::NoParity;}serialPort->setPortName(ui->cbSerialPort->currentText());serialPort->setBaudRate(baudRate);serialPort->setDataBits(dataBits);serialPort->setStopBits(stopBits);serialPort->setParity(parity);if(serialPort->open(QIODevice::ReadWrite)){ui->editReceive->append("serial port connected");}else{QMessageBox::critical(this,"Warnning","SerialPort Open Failed.Reason is :"+serialPort->errorString());}
}void mainwindow::on_btnClose_clicked()
{if(serialPort->isOpen()){serialPort->close();ui->editReceive->append("serial port disconnected");}
}void mainwindow::slot_serialPort_ready_read()
{QString buffer = QString(serialPort->readAll());ui->editReceive->append(buffer);
}void mainwindow::on_btnSend_clicked()
{if(serialPort->isOpen()){QString msg = ui->editSend->toPlainText();serialPort->write(msg.toLocal8Bit().data());ui->editReceive->append("SEND: "+msg);}
}void mainwindow::on_btnClear_clicked()
{ui->editSend->clear();ui->editReceive->clear();
}