前言
Qt中都有串口通信相关函数方法,接收串口数据亲测有效!发送未测试!当然网络串口工具也比较多,该版本这里只是简单了解下。
QT += serialport
示例图
示例源码
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QComboBox>
#include <QSerialPort>
#include <QPushButton>
#include <QTextBrowser>
#include <QTimer>
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 OnOpenClosePort();
void OnSend();
void OnRead();
void OnClear();
private:
void append(int type, const QString &data, bool clear = false);
private:
Ui::MainWindow *ui;
QPushButton* btn_openClose;
QSerialPort* m_serialPort;
QComboBox* m_comboBox;
QTextBrowser* m_browser;
QTimer* m_timerRead;
QComboBox* m_comboBox_Port;
QComboBox* m_comboBox_BaudRate;
QComboBox* m_comboBox_DataBits;
QComboBox* m_comboBox_StopBit;
QComboBox* m_comboBox_CheckBit;
};
#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QTextBrowser>
#include <QLineEdit>
#include <QPushButton>
#include <QLayout>
#include <QComboBox>
#include <QLabel>
#include <QGroupBox>
#include <QMessageBox>
#include <QThread>
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget* widget = new QWidget;
QHBoxLayout* hbox = new QHBoxLayout;
QHBoxLayout* hbox_left = new QHBoxLayout;
QVBoxLayout* vbox_left = new QVBoxLayout;
QVBoxLayout* vbox_right = new QVBoxLayout;
m_browser = new QTextBrowser;
m_comboBox = new QComboBox;
QPushButton* btn = new QPushButton("发送");
connect(btn,&QPushButton::clicked, this, &MainWindow::OnSend);
btn->setFixedWidth(50);
m_comboBox->setEditable(true);//设置可编辑
hbox_left->addWidget(m_comboBox);
hbox_left->addWidget(btn);
vbox_left->addWidget(m_browser);
vbox_left->addLayout(hbox_left);
m_comboBox_Port = new QComboBox;
m_comboBox_BaudRate = new QComboBox;
m_comboBox_DataBits = new QComboBox;
m_comboBox_StopBit = new QComboBox;
m_comboBox_CheckBit = new QComboBox;
m_comboBox_BaudRate->setEditable(true);//设置可编辑
m_comboBox_Port->setFixedWidth(100);
m_comboBox_BaudRate->setFixedWidth(100);
m_comboBox_DataBits->setFixedWidth(100);
m_comboBox_StopBit->setFixedWidth(100);
m_comboBox_CheckBit->setFixedWidth(100);
QLabel* label_Port = new QLabel("端口号");
QLabel* label_BaudRate = new QLabel("波特率");
QLabel* label_DataBits = new QLabel("数据位");
QLabel* label_StopBit = new QLabel("停止位");
QLabel* label_CheckBit = new QLabel("校验位");
btn_openClose = new QPushButton("打开串口");
connect(btn_openClose,&QPushButton::clicked, this, &MainWindow::OnOpenClosePort);
QPushButton* btn_clear = new QPushButton("清空");
connect(btn_clear, &QPushButton::clicked, this, &MainWindow::OnClear);
QFrame* frame = new QFrame;
frame->setFrameShape(QFrame::Box);
QVBoxLayout* vbox_port = new QVBoxLayout;
QHBoxLayout* hbox_Port = new QHBoxLayout;
QHBoxLayout* hbox_BaudRate = new QHBoxLayout;
QHBoxLayout* hbox_DataBits = new QHBoxLayout;
QHBoxLayout* hbox_StopBit = new QHBoxLayout;
QHBoxLayout* hbox_CheckBit = new QHBoxLayout;
hbox_Port->addWidget(label_Port);
hbox_Port->addWidget(m_comboBox_Port);
hbox_BaudRate->addWidget(label_BaudRate);
hbox_BaudRate->addWidget(m_comboBox_BaudRate);
hbox_DataBits->addWidget(label_DataBits);
hbox_DataBits->addWidget(m_comboBox_DataBits);
hbox_StopBit->addWidget(label_StopBit);
hbox_StopBit->addWidget(m_comboBox_StopBit);
hbox_CheckBit->addWidget(label_CheckBit);
hbox_CheckBit->addWidget(m_comboBox_CheckBit);
vbox_port->addLayout(hbox_Port);
vbox_port->addLayout(hbox_BaudRate);
vbox_port->addLayout(hbox_DataBits);
vbox_port->addLayout(hbox_StopBit);
vbox_port->addLayout(hbox_CheckBit);
vbox_port->addWidget(btn_openClose);
frame->setLayout(vbox_port);
vbox_right->addWidget(frame);
vbox_right->addStretch();
vbox_right->addWidget(btn_clear);
hbox->addLayout(vbox_left);
hbox->addLayout(vbox_right);
widget->setLayout(hbox);
setCentralWidget(widget);
//添加波特率
m_comboBox_BaudRate->addItem("9600");
m_comboBox_BaudRate->addItem("19200");
m_comboBox_BaudRate->addItem("115200");
//添加数据位
m_comboBox_DataBits->addItem("6");
m_comboBox_DataBits->addItem("7");
m_comboBox_DataBits->addItem("8");
m_comboBox_DataBits->setCurrentText("8");
//添加校验位
m_comboBox_CheckBit->addItem("无");
m_comboBox_CheckBit->addItem("奇");
m_comboBox_CheckBit->addItem("偶");
m_comboBox_CheckBit->addItem("标志");
m_comboBox_CheckBit->addItem("空格");
//添加停止位
m_comboBox_StopBit->addItem("1");
m_comboBox_StopBit->addItem("2");
//检测串口
QList<QSerialPortInfo> portInfos = QSerialPortInfo::availablePorts();
foreach (const QSerialPortInfo &serialInfo, portInfos) {
m_comboBox_Port->addItem(serialInfo.portName());
}
//读取数据
m_timerRead = new QTimer(this);
connect(m_timerRead,&QTimer::timeout, this, &MainWindow::OnRead);
m_timerRead->setInterval(100);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::OnOpenClosePort()
{
if(btn_openClose->text() == "关闭串口"){
btn_openClose->setText("打开串口");
m_comboBox_Port->setEnabled(true);
m_comboBox_BaudRate->setEnabled(true);
m_comboBox_DataBits->setEnabled(true);
m_comboBox_CheckBit->setEnabled(true);
m_comboBox_StopBit->setEnabled(true);
m_serialPort->close();
m_timerRead->stop();
m_serialPort->deleteLater();
return;
}
m_serialPort = new QSerialPort;
//初始化串口
m_serialPort->setPortName(m_comboBox_Port->currentText());
//设置波特率
m_serialPort->setBaudRate((QSerialPort::BaudRate)m_comboBox_BaudRate->currentText().toInt());
//设置数据位
m_serialPort->setDataBits((QSerialPort::DataBits)m_comboBox_DataBits->currentText().toInt());
//设置校验位
m_serialPort->setParity((QSerialPort::Parity)m_comboBox_CheckBit->currentIndex());
//设置停止位
m_serialPort->setStopBits(( QSerialPort::StopBits)m_comboBox_StopBit->currentIndex());
btn_openClose->setText("关闭串口");
m_comboBox_Port->setEnabled(false);
m_comboBox_BaudRate->setEnabled(false);
m_comboBox_DataBits->setEnabled(false);
m_comboBox_CheckBit->setEnabled(false);
m_comboBox_StopBit->setEnabled(false);
if(m_serialPort->open(QIODevice::ReadWrite) == true){
m_timerRead->start();
QMessageBox::information(nullptr,"提示","打开成功!");
}
else
QMessageBox::information(nullptr,"提示","打开失败!");
}
void MainWindow::OnSend()
{
if(!m_serialPort->isOpen()){
QMessageBox::information(nullptr,"提示","未打开串口!");
return;
}
m_comboBox->addItem(m_comboBox->currentText());
if(m_serialPort->write(m_comboBox->currentText().toLocal8Bit().data())){
m_browser->append("发送成功:"+m_comboBox->currentText() );
}else{
m_browser->append("发送失败:"+m_comboBox->currentText() );
}
}
void MainWindow::OnRead()
{
QTime endTime = QTime::currentTime().addMSecs(10);
while (QTime::currentTime() < endTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
QByteArray data = m_serialPort->readAll();
int dataLen = data.length();
if (dataLen <= 0) {
return;
}
QString buffer;
buffer = QString::fromLocal8Bit(data);
append(1, buffer);
}
void MainWindow::append(int type, const QString &data, bool clear)
{
static int currentCount = 0;
static int maxCount = 100;
if (clear) {
m_browser->clear();
currentCount = 0;
return;
}
if (currentCount >= maxCount) {
m_browser->clear();
currentCount = 0;
}
//过滤回车换行符
QString strData = data;
strData = strData.replace("\r", "");
strData = strData.replace("\n", "");
//不同类型不同颜色显示
QString strType;
if (type == 0) {
strType = "串口发送 >>";
m_browser->setTextColor(QColor("dodgerblue"));
} else if (type == 1) {
strType = "串口接收 <<";
m_browser->setTextColor(QColor("red"));
} else if (type == 2) {
strType = "处理延时 >>";
m_browser->setTextColor(QColor("gray"));
} else if (type == 3) {
strType = "正在校验 >>";
m_browser->setTextColor(QColor("green"));
} else if (type == 4) {
strType = "网络发送 >>";
m_browser->setTextColor(QColor(24, 189, 155));
} else if (type == 5) {
strType = "网络接收 <<";
m_browser->setTextColor(QColor(255, 107, 107));
} else if (type == 6) {
strType = "提示信息 >>";
m_browser->setTextColor(QColor(100, 184, 255));
}
strData = QString("时间[%1] %2 %3").arg(QTime::currentTime().toString("HH:mm:ss zzz")).arg(strType).arg(strData);
m_browser->append(strData);
currentCount++;
}
void MainWindow::OnClear()
{
m_browser->clear();
}