目录
- 1.概述
- 2.本次内容最终实现
- 3.代码部分
- 4.完整工程文件下载
1.概述
基于《串口开发基础》
在该基础上增加超时时间接收功能,加入定时器循环;
例如,接收数据开始后,在100ms内未接收到任何数据,视作本次数据接收结束,立刻进入数据处理阶段。
同时,在定时器循环基础上增加数据定时发送功能。
2.本次内容最终实现
发送间隔小于超时时间,视为一条数据:
单次发送不受影响:
定时发送:定时器用的不是很精确的,误差有点大,功能倒是没问题
3.代码部分
Pro工程代码
QT += core gui
QT += serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += 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.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
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
头文件 widget.h代码
#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_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
void serial_recv_del();
void this_timer_out();
~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();
void on_checkBox_2_stateChanged(int arg1);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
cpp - widget.cpp代码
#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include <QtSerialPort/QSerialPort> // 提供访问串口的功能
#include <QtSerialPort/QSerialPortInfo> // 提供系统中存在的串口信息
#include <QTimer>
#include "qmessagebox.h"
/*串口实例化*/
QSerialPort serial;
/*定时器实例化*/
QTimer timer;
/*串口接收数组*/
QByteArray serial_array;
/*接收数据总长度*/
static qint64 recv_data_len;
/*老的接收的串口数据长度*/
int old_serial_cnt;
/*接收数据长度相等后计数*/
static qint8 serial_recv_cnt = 0;
/*连续发送开关*/
static bool continusSend_sw = false;
/*连续发送定时器计数*/
static int continusSend_cnt = 0;
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");
//连接这个定时器的信号和槽,利用定时器的timeout(),this_timer_out()槽函数
connect(&timer, &QTimer::timeout, this, &Widget::this_timer_out);
/*添加定时发送时间*/
ui->comboBox_3->addItem("100", "100");
ui->comboBox_3->addItem("200", "200");
ui->comboBox_3->addItem("300", "300");
ui->comboBox_3->addItem("400", "400");
ui->comboBox_3->addItem("500", "500");
ui->comboBox_3->addItem("600", "600");
ui->comboBox_3->addItem("700", "700");
ui->comboBox_3->addItem("800", "800");
ui->comboBox_3->addItem("900", "900");
ui->comboBox_3->addItem("1000", "1000");
/*开启定时器ms*/
timer.start(20);
}
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();
}
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()
{
if(ui->checkBox_2->isChecked()) //连续发送
{
continusSend_sw = true;
}
else //不连续发送
{
continusSend_sw = false;
/*textBrowser中的数据放到qbyte中*/
QByteArray array_send = ui->plainTextEdit->toPlainText().toUtf8();
/*发送数据*/
serial.write(array_send);
/*若选中checkbox发送回车换行*/
if(ui->checkBox->isChecked())
{
serial.write("\r\n");
}
}
}
/*定时器中断*/
void Widget::this_timer_out()
{
/*接收数据*/
if(serial_array.count() > 0) //接收到数据
{
if(old_serial_cnt == serial_array.count()) //判断是否接受到新数据
{
serial_recv_cnt++;
if(serial_recv_cnt > 6) //数据接收完成
{
old_serial_cnt = 0;
/*接收完成后清空*/
serial_recv_cnt = 0;
/*更新文本框*/
recv_data_len += serial_array.count();
ui->lineEdit->setText(QString::number(recv_data_len));
ui->textBrowser->insertPlainText(serial_array);
serial_array.clear();
}
}
else
{
old_serial_cnt = serial_array.count();
serial_recv_cnt = 0;
}
}
/*发送数据*/
if(continusSend_sw)
{
continusSend_cnt += 20;
if(continusSend_cnt >= ui->comboBox_3->currentText().toInt())
{
/*textBrowser中的数据放到qbyte中*/
QByteArray array_send = ui->plainTextEdit->toPlainText().toUtf8();
/*发送数据*/
serial.write(array_send);
/*若选中checkbox发送回车换行*/
if(ui->checkBox->isChecked())
{
serial.write("\r\n");
}
/*clear*/
continusSend_cnt = 0;
}
}
}
void Widget::on_checkBox_2_stateChanged(int arg1)
{
if(arg1) //选中
{
}
else //未选中
{
continusSend_sw = false;
}
}
4.完整工程文件下载
完整工程下载地址: 点击下载