一、前言

相对来说,Qt编写串口助手,已经比较烂大街了,但是用来qt入门还是可以的,接下来上代码。
二、结构

三、代码
1.MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtSerialPort/qserialport.h>
#include <QtSerialPort/qserialportinfo.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_btnSendData_clicked();
void on_btnOpenPort_clicked();
void on_btnClosePort_clicked();
void readData();
private:
QSerialPort *m_serialPort;
Ui::MainWindow *ui;
void updatePortList();
};
#endif // MAINWINDOW_H
2.MainWindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
updatePortList();
setWindowTitle("Serial Assistant");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnSendData_clicked()
{
QString sendData = ui->lineSend->text();
QDateTime current_date_time = QDateTime::currentDateTime();
QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");
ui->textBrowserDisplay->append(current_date);
m_serialPort->write(sendData.

本文详细介绍使用Qt开发串口助手的过程,包括项目结构、关键代码解析及串口参数配置方法。通过实例演示如何实现串口数据的发送与接收,适用于初学者快速上手Qt串口编程。
最低0.47元/天 解锁文章
2564

被折叠的 条评论
为什么被折叠?



