1
一、项目需求
用QT开发一个串口小助手,要求能实现串口接收,发送等基本功能。
二、UI界面设计
三、源码实现
3.1 mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onClickedButtun();
void readData();
void clearReceiveWindow();
void on_sendButtun_clicked();
void clearSendWindow();
private:
Ui::MainWindow *ui;
QSerialPort *m_serial = nullptr;
};
#endif // MAINWINDOW_H
3.2 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//自动识别并显示当前存在的串口号
const auto infos = QSerialPortInfo::availablePorts();
for(const QSerialPortInfo &info:infos)
ui->serialPortNamecomboBox->addItem(info.portName());
//添加波特率下拉菜单
const QString serialBaudrate[9]={"2400","4800","9600","14400","19200","38400","56000","57600","115200"};
for(int i = 0 ; i < 9 ; i++)
{
ui->serialBaudratecomboBox->addItem(serialBaudrate[i]);
}
connect(ui->serialOnClickpushButton,&QPushButton::clicked,this,&MainWindow::onClickedButtun);
connect(ui->serialCleanReceivepushButton,&QPushButton::clicked,this,&MainWindow::clearReceiveWindow);
connect(ui->serialSendpushButton,&QPushButton::clicked,this,&MainWindow::on_sendButtun_clicked);
connect(ui->serialCleanSendpushButton,&QPushButton::clicked,this,&MainWindow::clearSendWindow);
}
MainWindow::~MainWindow()
{
delete ui;
}
/*
*@brief:按钮控制函数,控制串口的开和关
*@param:None
*@retval:None
*/
void MainWindow::onClickedButtun()
{
if(ui->serialOnClickpushButton->text() == tr("打开串口"))
{
m_serial = new QSerialPort;
//设置串口名
m_serial->setPortName(ui->serialPortNamecomboBox->currentText());
//打开串口
m_serial->open(QIODevice::ReadWrite);
//设置波特率
m_serial->setBaudRate(ui->serialBaudratecomboBox->currentIndex());
//连接信号槽
connect(m_serial,&QSerialPort::readyRead,this,&MainWindow::readData);
ui->serialPortNamecomboBox->setEnabled(false);
ui->serialBaudratecomboBox->setEnabled(false);
ui->serialOpenradioButton->toggle();
ui->serialOnClickpushButton->setText(tr("关闭串口"));
}
else
{
//关闭串口
m_serial->clear();
m_serial->close();
m_serial->deleteLater();
//恢复设置使能
ui->serialPortNamecomboBox->setEnabled(true);
ui->serialBaudratecomboBox->setEnabled(true);
ui->serialOpenradioButton->toggle();
ui->serialOnClickpushButton->setText(tr("打开串口"));
}
}
/*
*@brief:读取数据函数
*@param:None
*@retval:None
*/
void MainWindow::readData()
{
QByteArray data = m_serial->readAll();
if(!data.isLower())
{
QString str = ui->serialReceivetextEdit->toPlainText();
// str += tr(data); //此写法不支持中文
str += QString::fromLocal8Bit(data); //此写法支持中文
ui->serialReceivetextEdit->clear();
ui->serialReceivetextEdit->append(str);
}
}
/*
*@brief:清除接收窗口
*@param:None
*@retval:None
*/
void MainWindow::clearReceiveWindow()
{
ui->serialReceivetextEdit->clear();
}
/*
*@brief:发送函数
*@param:None
*@retval:None
*/
void MainWindow::on_sendButtun_clicked()
{
// m_serial->write(ui->serialSendtextEdit->toPlainText().toLatin1()); //此写法不支持中文
m_serial->write(ui->serialSendtextEdit->toPlainText().toLocal8Bit()); //此写法支持中文
}
/*
*@brief:清除发送窗口
*@param:None
*@retval:None
*/
void MainWindow::clearSendWindow()
{
ui->serialSendtextEdit->clear();
}
四、测试运行
略
全部0条评论
快来发表一下你的评论吧 !