目录
- 1. 实现目标
- 1.1 界面
- 1.2 发送
- 1.3 接收
- 1.4 清除接收、发送
- 2. 新建工程
- 3. 添加头文件
- 4. 变量定义
- 5. 完整代码
- 6. 工程下载
1. 实现目标
1.1 界面
1.2 发送
1.3 接收
1.4 清除接收、发送
2. 新建工程
3. 添加头文件
QT += serialport
// #include <QDebug‘’>
#include <QtSerialPort/QSerialPort> // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo> // 提供系统中存在的串口信息
// #include <QDebug‘’>
#include <QtSerialPort/QSerialPort> // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo> // 提供系统中存在的串口信息
// #include <QTimer‘’>
#include “qmessagebox.h”
4. 变量定义
/串口实例化/
QSerialPort serial;
/串口接收数组/
QByteArray serial_array;
/接收数据总长度/
static qint64 recv_data_len;
5. 完整代码
工程代码
QT += core gui
QT += serialportgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件部分
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QDebug>
#include <QtSerialPort/QSerialPort> // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo> // 提供系统中存在的串口信息QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);void serial_recv_del();~Widget();private slots:void on_pushButton_2_clicked();void on_pushButton_clicked();void on_pushButton_4_clicked();void on_pushButton_5_clicked();void on_pushButton_3_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
cpp
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include <QtSerialPort/QSerialPort> // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo> // 提供系统中存在的串口信息
#include <QTimer>
#include "qmessagebox.h"/*串口实例化*/
QSerialPort serial;
/*串口接收数组*/
QByteArray serial_array;
/*接收数据总长度*/
static qint64 recv_data_len;Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);/*开启按钮使能,禁用关闭按钮*/ui->pushButton_2->setDisabled(true);/*遍历COM口*/foreach (const QSerialPortInfo&info, QSerialPortInfo::availablePorts()){ui->comboBox->addItem(info.portName());}/*添加波特率*/ui->comboBox_2->addItem("9600", "9600");ui->comboBox_2->addItem("38400", "38400");ui->comboBox_2->addItem("115200", "115200");}Widget::~Widget()
{delete ui;
}/*关闭串口按钮*/
void Widget::on_pushButton_2_clicked()
{serial.close();if(serial.isOpen()){QMessageBox::about(NULL, " 警告", "<font color='red'>串口关闭异常</font>");}else{ui->pushButton_2->setDisabled(true);ui->pushButton->setDisabled(false);ui->pushButton_2->setStyleSheet("QPushButton { background-color: white; }");}
}/*串口接收处理函数*/
void Widget::serial_recv_del()
{serial_array = serial.readAll();recv_data_len += serial_array.count();ui->lineEdit->setText(QString::number(recv_data_len));ui->textBrowser->insertPlainText(serial_array);serial_array.clear();
}void Widget::on_pushButton_clicked()
{/*设置端口名称*/serial.setPortName(ui->comboBox->currentText());/*设置波特率*/serial.setBaudRate(ui->comboBox_2->currentText().toInt());/*打开串口*/serial.open(QIODevice::ReadWrite);/*串口打开成功*/if(serial.isOpen()){/*改变开启串口失能*/ui->pushButton_2->setDisabled(false);ui->pushButton->setDisabled(true);ui->pushButton_2->setStyleSheet("QPushButton { background-color: green; }");connect(&serial, &QSerialPort::readyRead, this, &Widget::serial_recv_del);}else{QMessageBox::about(NULL, " 警告", "<font color='red'>串口开启异常</font>");}
}/*清除接收*/
void Widget::on_pushButton_4_clicked()
{ui->textBrowser->clear();/*清除接收数据长度*/ui->lineEdit->setText("0");recv_data_len = 0;serial_array.clear();
}/*清除发送*/
void Widget::on_pushButton_5_clicked()
{ui->plainTextEdit->clear();
}/*发送数据*/
void Widget::on_pushButton_3_clicked()
{/*textBrowser中的数据放到qbyte中*/QByteArray array_send = ui->plainTextEdit->toPlainText().toUtf8();/*发送数据*/serial.write(array_send);/*若选中checkbox发送回车换行*/if(ui->checkBox->isChecked()){serial.write("\r\n");}
}
6. 工程下载
点击下载