今天用Qt写了一个串口收发工具,记录下整个流程。
1、项目工程布局
2、ui文件布局
3、widget.h文件
-
#ifndef WIDGET_H
-
#define WIDGET_H
-
-
#include <QWidget>
-
#include <QtSerialPort/QSerialPort>
-
#include <QtSerialPort/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();
-
-
private:
-
Ui::Widget *ui;
-
QSerialPort *pSerialPort;
-
QByteArray recvData;
-
-
-
};
-
#endif // WIDGET_H
4、widget.cpp文件
-
#include "widget.h"
-
#include "ui_widget.h"
-
#include <QDebug>
-
#include <QMessageBox>
-
#include <QFileDialog>
-
#include <QFile>
-
#include <QTextStream>
-
-
-
Widget::Widget(QWidget *parent)
-
: QWidget(parent)
-
, ui(
new Ui::Widget)
-
{
-
ui->setupUi(
this);
-
-
-
ui->widget->setWindowTitle(
"串口助手");
-
pSerialPort =
NULL;
-
-
-
//枚举识别到的com口
-
//写法一:
-
/* foreach(QSerialPortInfo info,QSerialPortInfo::availablePorts())//读串口
-
{
-
-
QSerialPort port;
-
-
port.setPort(info);
-
-
if(port.open(QIODevice::ReadWrite))//如果串口可以读写的形式打开
-
{
-
ui->comboBox_portName->addItem(info.portName()); //显示出来
-
-
port.close();//自动关闭等待人为打开
-
}
-
}*/
-
//写法二
-
QList<QSerialPortInfo> seriList = QSerialPortInfo::availablePorts();
-
-
for(
int i =
0;i <seriList.count();i++ )
-
{
-
QSerialPort port;
-
-
port.setPort(seriList.at(i));
-
-
if(port.open(QIODevice::ReadWrite))
-
{
-
ui->comboBox_portName->addItem(seriList.at(i).portName());
-
-
port.close();
-
}
-
}
-
-
-
//信号槽连接打开按钮和相关处理操作
-
connect(ui->openBtn,&QPushButton::clicked,[=](){
-
-
if(pSerialPort !=
NULL)
-
{
-
QMessageBox::warning(
this,
"警告",
"已有活跃串口,请先关闭再打开");
-
-
return;
-
}
-
-
pSerialPort =
new QSerialPort(
this);
-
-
pSerialPort->setPortName(ui->comboBox_portName->currentText());
-
pSerialPort->open(QIODevice::ReadWrite);
-
pSerialPort->setBaudRate(ui->comboBox_bandRate->currentText().toInt());
-
pSerialPort->setDataBits(QSerialPort::Data8);
-
pSerialPort->setParity(QSerialPort::NoParity);
-
pSerialPort->setFlowControl(QSerialPort::NoFlowControl);
-
pSerialPort->setStopBits(QSerialPort::OneStop);
-
-
connect(pSerialPort,&QSerialPort::readyRead,
this,[=](){
-
-
recvData = pSerialPort->readAll();
-
-
if(!recvData.isEmpty())
-
{
-
ui->textEdit_read->append(recvData);
-
}
-
-
recvData.clear();
-
-
});
-
-
});
-
-
//发送数据信号槽连接
-
connect(ui->sendBtn,&QPushButton::clicked,
this,[=](){
-
-
if(
NULL == pSerialPort )
-
return;
-
pSerialPort->write(ui->lineEdit_write->text().toUtf8());
-
-
ui->lineEdit_write->clear();
-
-
});
-
-
//清空按钮信号槽连接
-
connect(ui->clearBtn,&QPushButton::clicked,
this,[=](){
-
-
ui->textEdit_read->clear();
-
});
-
-
-
//关闭串口按钮信号槽连接
-
connect(ui->closeBtn,&QPushButton::clicked,
this,[=](){
-
-
pSerialPort->close();
-
-
delete pSerialPort;
-
-
pSerialPort =
NULL;
-
});
-
-
-
//保存按钮信号槽连接
-
connect(ui->saveBtn,&QPushButton::clicked,
this,[=](){
-
if(ui->textEdit_read->document()->isEmpty())
-
{
-
QMessageBox::warning(
this,
"警告",
"记录不能为空");
-
-
return;
-
}
-
-
QString path = QFileDialog::getSaveFileName(
this,
"保存log记录",
"log记录",
"(*.txt)");
-
-
qDebug()<<path;
-
-
-
if (path.isEmpty())
-
{
-
QMessageBox::warning(
this,
"路径",
"路径不能为空");
-
-
return ;
-
}
-
-
QFile file(path);
-
-
file.open(QIODevice::WriteOnly|QIODevice::Text);
-
-
QTextStream stream(&file);
-
-
-
stream<<ui->textEdit_read->toPlainText();
-
-
file.close();
-
-
});
-
-
-
ui->lineEdit_write->setFocus();
-
-
}
-
-
Widget::~Widget()
-
{
-
delete ui;
-
}
-
5、main.cpp 文件
-
#include "widget.h"
-
-
#include <QApplication>
-
-
int main(int argc, char *argv[])
-
{
-
QApplication a(argc, argv);
-
Widget w;
-
w.show();
-
return a.exec();
-
}
6、Serial.pro中要加入 serialport 模块
7.运行效果(电脑上外接串口,然后串口Tx和Rx短接,用作自发自收)
转载:https://blog.csdn.net/weixin_40204595/article/details/110433771
查看评论