前言:此文章是这篇文章的拓展 QT+串口调试助手+基本版-CSDN博客,如果需要独立完成串口调试助手直接看基本版文章即可,如果需要完成串口调试助手的其他功能,参考拓展版。
一、更新QT串口调试助手UI界面
1、ui串口设置界面
2、ui串口接收设置界面
3、ui串口发送设置界面
4、更改UI界面控件名称
5、ui界面整体图
二、运行查看UI界面效果
三、串口调试助手功能开发
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QtSerialPort>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_openSerialBut_clicked(); //打开串口槽函数void serialPort_readyRead(); //串口接收槽函数void on_sendBut_clicked();void on_clearSendBut_clicked();void on_clearReceiveBut_clicked();void on_updateSerialBut_clicked();void on_sendCheckBox_stateChanged(int arg1);private:Ui::Widget *ui;QSerialPort *serial; //串口端口QTimer *timSend; // 定时发送-定时器
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QtSerialPort>
#include <QSerialPortInfo>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);setWindowTitle("Serial_Port");serial = new QSerialPort(this);ui->dataBitCB->setCurrentIndex(3);//将数据位默认选项就是8ui->baundDrateCB->setCurrentIndex(1);//将波特率默认选项是115200//通过QSerialPortInfo查找可用串口,但存在一个局限,如果在软件打开后在插入串口,此时串口不可以识别到ui->serailCB->clear();foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){ui->serailCB->addItem(info.portName());}//信号与槽函数,当串口接收到数据后转到对应的接收函数connect(serial,SIGNAL(readyRead()),this,SLOT(serialPort_readyRead()));// 定时发送-定时器timSend = new QTimer;timSend->setInterval(1000);// 设置默认定时时长1000msconnect(timSend, &QTimer::timeout, this, [=](){on_sendBut_clicked();});
}Widget::~Widget()
{delete ui;
}void Widget::on_openSerialBut_clicked()
{if(ui->openSerialBut->text()==QString("打开串口")){//设置串口名serial->setPortName(ui->serailCB->currentText());//设置波特率serial->setBaudRate(ui->baundDrateCB->currentText().toInt());//设置数据位switch(ui->dataBitCB->currentText().toInt()){case 8:serial->setDataBits(QSerialPort::Data8);break;}//设置停止位switch (ui->stopBitCB->currentIndex()){case 0:serial->setStopBits(QSerialPort::OneStop);break;case 1:serial->setStopBits(QSerialPort::OneAndHalfStop);break;case 2:serial->setStopBits(QSerialPort::TwoStop);break;}//设置奇偶校验位switch(ui->checkBitCB->currentIndex()){case 0:serial->setParity(QSerialPort::NoParity);break;//无校验case 1:serial->setParity(QSerialPort::OddParity);break;//奇校验case 2:serial->setParity(QSerialPort::EvenParity);break;//偶校验}//设置流控制serial->setFlowControl(QSerialPort::NoFlowControl);//打开串口if(!serial->open(QIODevice::ReadWrite)){QMessageBox::about(NULL,"提示","无法打开串口");return;}ui->openSerialBut->setText(tr("关闭串口"));}else{serial->close();//关闭串口ui->openSerialBut->setText(tr("打开串口"));}
}void Widget::serialPort_readyRead() //串口接收
{QByteArray lasttext;lasttext=ui->receivePlainTextEdit->toPlainText().toUtf8();//首先将接收文本框的内容全部复制到lasttext里面QByteArray receiveText = serial->readAll();//读串口接收数据if(ui->timeCheckBox->checkState() == Qt::Checked)//时间戳被选中{QDateTime nowtime = QDateTime::currentDateTime();QString str = "[" + nowtime.toString("yyyy-MM-dd hh:mm:ss") + "] "+ "RX: ";//记录当前时间str += QString(receiveText).append("\r\n");//数据格式:当前时间+接收内容+换行lasttext=lasttext.append(str.toStdString());//显示数据:之前数据+当前数据}else{receiveText = "\r\nRX:"+receiveText;lasttext=lasttext.append(receiveText);//显示数据:之前数据+当前数据}ui->receivePlainTextEdit->setPlainText(lasttext);
}void Widget::on_sendBut_clicked()
{QByteArray Sendtext=ui->sendTextEdit->toPlainText().toUtf8();serial->write(Sendtext);QString lasttext;if(ui->timeCheckBox->checkState() == Qt::Checked)//时间戳被选中{QDateTime nowtime = QDateTime::currentDateTime();lasttext=ui->receivePlainTextEdit->toPlainText();//首先将接收文本框的内容全部复制到lasttext里面lasttext = lasttext+"[" + nowtime.toString("yyyy-MM-dd hh:mm:ss") + "] "+ "\r\nTX: ";//记录当前时间lasttext += ui->sendTextEdit->toPlainText();//数据格式:当前时间+接收内容+换行}else{lasttext=ui->receivePlainTextEdit->toPlainText();//首先将接收文本框的内容全部复制到lasttext里面lasttext=lasttext+"\r\nTX:"+ui->sendTextEdit->toPlainText();}ui->receivePlainTextEdit->setPlainText(lasttext);
}void Widget::on_clearSendBut_clicked()
{ui->sendTextEdit->clear();
}void Widget::on_clearReceiveBut_clicked()
{ui->receivePlainTextEdit->clear();
}void Widget::on_updateSerialBut_clicked()
{if(ui->openSerialBut->text() == "打开串口"){ui->serailCB->clear();//通过QSerialPortInfo查找可用串口foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){ui->serailCB->addItem(info.portName());}}
}void Widget::on_sendCheckBox_stateChanged(int arg1)
{// 获取复选框状态,未选为0,选中为2if(arg1 == 0){timSend->stop();}else{timSend->start(ui->sendTimeTextEdit->toPlainText().toInt());// 设置定时时长,重新计数}
}
四、完整工程
链接:https://pan.baidu.com/s/119Aod8D9wsfUbtSgIijAjg?pwd=8888
提取码:8888