Qt5.5版本,以下是项目源码和ui设计,结尾附上.exe可执行文件和项目代码。
【.pro文件】
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
【widget.h头文件】
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void InitUI();
void UpDataSerialPortNames();
void SetTip(QString tip);
private slots:
void on_Btn_Open_clicked();
void on_Btn_UpData_clicked();
void on_Btn_Close_clicked();
void ProcesReception();
void on_Btn_ClearReceive_clicked();
void on_Btn_Send_clicked();
void HandleError(QSerialPort::SerialPortError error);//串口断开时
private:
Ui::Widget *ui;
QSerialPort m_serial;
QSerialPortInfo m_serialInfo;
};
#endif // WIDGET_H
【main.cpp】
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
【widget.cpp】
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
InitUI();
}
Widget::~Widget()
{
delete ui;
}
void Widget::InitUI()
{
// 连接错误信号到槽函数
connect(&m_serial,&QSerialPort::errorOccurred,this,&Widget::HandleError);
//更新串口
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Widget::UpDataSerialPortNames);
timer->start(1000); // 每秒钟检测一次串口
//设置软件Ui
setWindowTitle("串口调试助手");
//设置波特率
ui->BaudRate->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
ui->BaudRate->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
ui->BaudRate->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
ui->BaudRate->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
//设置数据位
ui->DataBits->addItem(QStringLiteral("5"), QSerialPort::Data5);
ui->DataBits->addItem(QStringLiteral("6"), QSerialPort::Data6);
ui->DataBits->addItem(QStringLiteral("7"), QSerialPort::Data7);
ui->DataBits->addItem(QStringLiteral("8"), QSerialPort::Data8);
ui->DataBits->setCurrentIndex(3);
//设置奇偶校验位
ui->ParityBit->addItem(QStringLiteral("无"), QSerialPort::NoParity);
ui->ParityBit->addItem(QStringLiteral("偶"), QSerialPort::EvenParity);
ui->ParityBit->addItem(QStringLiteral("奇"), QSerialPort::OddParity);
ui->ParityBit->addItem(QStringLiteral("高"), QSerialPort::MarkParity);
ui->ParityBit->addItem(QStringLiteral("低"), QSerialPort::SpaceParity);
ui->ParityBit->setCurrentIndex(0);
//设置停止位
ui->StopBit->addItem(QStringLiteral("1"), QSerialPort::OneStop);
ui->StopBit->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
ui->StopBit->setCurrentIndex(0);
//添加流控"XON/XOFF"
ui->FlowBit->addItem(QStringLiteral("无"), QSerialPort::NoFlowControl);
ui->FlowBit->addItem(QStringLiteral("RTS/CTS"), QSerialPort::HardwareControl);
ui->FlowBit->addItem(QStringLiteral("XON/XOFF"), QSerialPort::SoftwareControl);
ui->FlowBit->setCurrentIndex(0);
//清空提示信息
SetTip("未连接到串口");
//更新串口名信息
UpDataSerialPortNames();
ui->Btn_Open->setVisible(true);
ui->Btn_Close->setVisible(false);
}
void Widget::UpDataSerialPortNames()
{
ui->SerialPortName->clear();
foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()){
ui->SerialPortName->addItem(info.portName());
}
}
//打开串口
void Widget::on_Btn_Open_clicked()
{
QString SerialName = ui->SerialPortName->currentText();
int BaudRate = ui->BaudRate->currentText().toInt();
int Databits = ui->DataBits->currentText().toInt();
int Parity = ui->ParityBit->currentText().toInt();
int StopBit = ui->StopBit->currentText().toInt();
int FlowBit = ui->FlowBit->currentText().toInt();
//设置串口
m_serial.setPortName(SerialName);
//设置波特率
m_serial.setBaudRate(BaudRate);
//设置数据位
m_serial.setDataBits(QSerialPort::DataBits(Databits));
//设置奇偶校验位
m_serial.setParity(QSerialPort::Parity(Parity));
//设置停止位
m_serial.setStopBits(QSerialPort::StopBits(StopBit));
//设置流控
m_serial.setFlowControl(QSerialPort::FlowControl(FlowBit));
//打开串口
m_serial.open(QIODevice::ReadWrite);
if(m_serial.isOpen()){
SetTip("打开成功");
connect(&m_serial,&QSerialPort::readyRead,this,&Widget::ProcesReception);
ui->SerialPortName->setEnabled(false);
ui->BaudRate->setEnabled(false);
ui->DataBits->setEnabled(false);
ui->ParityBit->setEnabled(false);
ui->StopBit->setEnabled(false);
ui->FlowBit->setEnabled(false);
ui->Btn_Open->setVisible(false);
ui->Btn_Close->setVisible(true);
}else{
SetTip("打开失败!!!");
}
}
//刷新串口
void Widget::on_Btn_UpData_clicked()
{
UpDataSerialPortNames();
SetTip("更新成功");
}
//关闭串口
void Widget::on_Btn_Close_clicked()
{
if(m_serial.isOpen()){
m_serial.close();
SetTip("串口已经关闭");
ui->SerialPortName->setEnabled(true);
ui->BaudRate->setEnabled(true);
ui->DataBits->setEnabled(true);
ui->ParityBit->setEnabled(true);
ui->StopBit->setEnabled(true);
ui->FlowBit->setEnabled(true);
ui->Btn_Open->setVisible(true);
ui->Btn_Close->setVisible(false);
}else{
SetTip("未打开,关闭失败");
}
}
void Widget::ProcesReception()
{
QByteArray array=m_serial.readAll();
ui->ReceivePlainTextEdit->insertPlainText(array);
}
//清空接收
void Widget::on_Btn_ClearReceive_clicked()
{
ui->ReceivePlainTextEdit->clear();
}
//点击发送
void Widget::on_Btn_Send_clicked()
{
QByteArray array=ui->SendPlainTextEdit->toPlainText().toUtf8();
if(m_serial.isOpen()){
m_serial.write(array);
}else{
SetTip("发送失败:串口未打开");
}
}
void Widget::SetTip(QString tip)
{
ui->TipText->setText(tip);
}
void Widget::HandleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
on_Btn_Close_clicked();
}
}
【widget.ui】
项目运行效果:
【项目源码+可项目执行文件】:
链接:https://pan.baidu.com/s/1qk3Z6B1F2Ed82n1FduWh4w?pwd=dhir
提取码:dhir