//mainwindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSerialPort>

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 on_pushButton_open_clicked();

void on_pushButton_clean_recv_clicked();

void on_pushButton_clean_send_clicked();

void on_pushButton_send_clicked();

private:
Ui::MainWindow *ui;

QSerialPort *serialPort;
bool portOpened;

void scanSerialPort();
void baudInit();
void databitInit();
void stopbitInit();
void parityInit();

private slots:
void serialPortReadyRead();
};
#endif // MAINWINDOW_H
//mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QSerialPort"
#include "QSerialPortInfo"
#include "QMessageBox"
#include "QDateTime"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置左右比例为2:8
ui->splitter_left_right->setStretchFactor(0,2);
ui->splitter_left_right->setStretchFactor(1,8);

//设置上下比例为7:3
ui->splitter_top_bot->setStretchFactor(0, 7);
ui->splitter_top_bot->setStretchFactor(1, 3);

scanSerialPort();
baudInit();
databitInit();
stopbitInit();
parityInit();

serialPort = new QSerialPort(this);
ui->checkBox_autoline->setChecked(true);
ui->checkBox_timestamp->setChecked(true);
portOpened = false;
ui->pushButton_send->setEnabled(false);
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialPortReadyRead()));
}

MainWindow::~MainWindow()
{
delete ui;
}

//获取可用的串口
void MainWindow::scanSerialPort()
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
ui->comboBox_port->addItem(info.portName());
}
}

//设置波特率
void MainWindow::baudInit()
{
QList <QString> list;
list<<"1200"<<"2400"<<"4800"<<"9600"<<"19200"<<"38400"<<"57600"<<"115200";

for (int i = 0; i < 8; i++) {
ui->comboBox_baud->addItem(list[i]);
}
ui->comboBox_baud->setCurrentIndex(3);
}

//设置数据位
void MainWindow::databitInit()
{
QList <QString> list;
list<<"5"<<"6"<<"7"<<"8";

for (int i = 0; i < 4; i++) {
ui->comboBox_databit->addItem(list[i]);
}
ui->comboBox_databit->setCurrentIndex(3);
}

//设置停止位
void MainWindow::stopbitInit()
{
QList <QString> list;
list<<"1"<<"2";

for (int i = 0; i < 2; i++) {
ui->comboBox_stopbit->addItem(list[i]);
}
ui->comboBox_stopbit->setCurrentIndex(0);
}

//设置校验位
void MainWindow::parityInit()
{
QList <QString> list;
list<<"None"<<"Even"<<"Odd"<<"Space"<<"Mark";

for (int i = 0; i < 5; i++) {
ui->comboBox_parity->addItem(list[i]);
}
ui->comboBox_parity->setCurrentIndex(0);
}

void MainWindow::on_pushButton_open_clicked()
{
//在串口未打开时,设置数据位、波特率、停止位、校验位
if (portOpened == false) {
serialPort->setPortName(ui->comboBox_port->currentText());
serialPort->setBaudRate(ui->comboBox_baud->currentText().toInt());

switch (ui->comboBox_databit->currentText().toInt()) {
case 5:
serialPort->setDataBits(QSerialPort::Data5);
break;
case 6:
serialPort->setDataBits(QSerialPort::Data6);
break;
case 7:
serialPort->setDataBits(QSerialPort::Data7);
break;
case 8:
serialPort->setDataBits(QSerialPort::Data8);
break;
default:
break;
}

switch (ui->comboBox_stopbit->currentText().toInt()) {
case 1:
serialPort->setStopBits(QSerialPort::OneStop);
break;
case 2:
serialPort->setStopBits(QSerialPort::TwoStop);
break;
default:
break;
}

switch (ui->comboBox_parity->currentIndex()) {
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::OddParity);
break;
case 3:
serialPort->setParity(QSerialPort::SpaceParity);
break;
case 4:
serialPort->setParity(QSerialPort::MarkParity);
break;
default:
break;
}
if (!serialPort->open(QIODevice::ReadWrite)) {
QMessageBox::about(NULL, "错误", "串口打开失败");
} else {
portOpened = true;
ui->comboBox_baud->setEnabled(false);
ui->comboBox_databit->setEnabled(false);
ui->comboBox_parity->setEnabled(false);
ui->comboBox_port->setEnabled(false);
ui->comboBox_stopbit->setEnabled(false);
ui->pushButton_open->setText("关闭");
ui->pushButton_send->setEnabled(true);
}
} else {
// close serial port
serialPort->close();
portOpened = false;
ui->comboBox_baud->setEnabled(true);
ui->comboBox_databit->setEnabled(true);
ui->comboBox_parity->setEnabled(true);
ui->comboBox_port->setEnabled(true);
ui->comboBox_stopbit->setEnabled(true);
ui->pushButton_open->setText("打开");
ui->pushButton_send->setEnabled(false);
}
}

//清空接收区数据
void MainWindow::on_pushButton_clean_recv_clicked()
{
ui->textBrowser->clear();
}

//清空发送区数据
void MainWindow::on_pushButton_clean_send_clicked()
{
ui->textEdit->clear();
}

//发送
void MainWindow::on_pushButton_send_clicked()
{
QByteArray data = ui->textEdit->toPlainText().toUtf8();
serialPort->write(data);
serialPort->waitForBytesWritten(data.size());
}

void MainWindow::serialPortReadyRead()
{
QByteArray buf = serialPort->readAll();
QString str;

if (ui->checkBox_timestamp->isChecked()) {
QDateTime cur = QDateTime::currentDateTime();
str = cur.toString("[yy-MM-dd hh:mm:ss] ") + QString(buf);
} else {
str = QString(buf);
}

if (ui->checkBox_autoline->isChecked())
ui->textBrowser->append(str);
else
ui->textBrowser->insertPlainText(str);
}


自写QT串口调试工具_串口