当前位置:网站首页>QT编写串口助手
QT编写串口助手
2022-07-28 16:32:00 【浮诩】
QT编写串口助手
1.pro文件添加中serialport

2.widget.h文件编写
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSerialPort> //提供访问串口的功能
#include <QSerialPortInfo> //提供系统中存在的串口的信息
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void set_baudList(); //设置波特率选择项
void set_parityList();//设置校验位选项
void set_dataBitsList();//设置数据位选项
void set_stopBitsList();//设置停止位选项
void serial_Init();//串口设置初始化
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
void serialPort_readyRead();
void on_pushButton_3_clicked();
void on_pushButton_5_clicked();
private:
Ui::Widget *ui;
QSerialPort serial;
};
#endif // WIDGET_H
3.main.cpp文件编写
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
4.widget.cpp文件编写
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QString>
#include <iostream>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->comboBox->clear();
this->setWindowTitle("串口助手");
//设置窗口固定大小
this->setMaximumSize(800,550);
this->setMinimumSize(800,550);
//通过QSerialPortInfo查找可用串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(info.portName());
}
//连接信号和槽
QObject::connect(&serial, &QSerialPort::readyRead, this, &Widget::serialPort_readyRead);
//串口设置初始化
serial_Init();
}
Widget::~Widget()
{
delete ui;
}
//设置波特率选项函数**********************************************
void Widget::set_baudList()
{
QStringList baudList; //波特率对象
baudList<<"1200"<<"2400"<<"4800"<<"9600"
<<"14400"<<"19200"<<"38400"<<"56000"
<<"57600"<<"115200";
ui->comboBox_2->addItems(baudList);
ui->comboBox_2->setCurrentIndex(3);
}
//设置校验位函数**********************************************
void Widget::set_parityList()
{
QStringList parityList; //校验位对象
parityList<<"无"<<"奇校验"<<"偶校验";
ui->comboBox_5->addItems(parityList);
ui->comboBox_5->setCurrentIndex(0);
}
//设置数据位函数**********************************************
void Widget::set_dataBitsList()
{
QStringList dataBitsList; //数据位对象
dataBitsList<<"5"<<"6"<<"7"<<"8";
ui->comboBox_4->addItems(dataBitsList);
ui->comboBox_4->setCurrentIndex(3);
}
//设置停止位函数**********************************************
void Widget::set_stopBitsList()
{
QStringList stopBitsList; //停止位对象
stopBitsList<<"1"<<"2";
ui->comboBox_3->addItems(stopBitsList);
ui->comboBox_3->setCurrentIndex(0);
}
//串口设置初始化函数*********************************************
void Widget::serial_Init()
{
//设置波特率选项
set_baudList();
//设置校验位选项
set_parityList();
//设置数据位选项
set_dataBitsList();
//设置停止位选项
set_stopBitsList();
}
//检测串口槽函数*****************************************************
void Widget::on_pushButton_2_clicked()
{
ui->comboBox->clear();
//通过QSerialPortInfo查找可用串口
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(info.portName());
}
}
//打开串口槽函数****************************************************
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text()==QString("打开串口"))
{
//设置串口名
serial.setPortName(ui->comboBox->currentText());
//设置波特率
serial.setBaudRate(ui->comboBox_2->currentText().toInt());
//设置数据位
switch(ui->comboBox_4->currentIndex())
{
case 5: serial.setDataBits(QSerialPort::Data5); break;
case 6: serial.setDataBits(QSerialPort::Data6); break;
case 7: serial.setDataBits(QSerialPort::Data7); break;
case 8: serial.setDataBits(QSerialPort::Data8); break;
default: break;
}
//设置奇偶校验
switch(ui->comboBox_5->currentIndex())
{
case 0: serial.setParity(QSerialPort::NoParity); break;
case 1: serial.setParity(QSerialPort::OddParity); break;
case 2: serial.setParity(QSerialPort::EvenParity); break;
default: break;
}
// 设置流控制
serial.setFlowControl(QSerialPort::NoFlowControl);
//设置停止位
switch(ui->comboBox_3->currentIndex())
{
case 1: serial.setStopBits(QSerialPort::OneStop); break;
case 2: serial.setStopBits(QSerialPort::TwoStop); break;
default: break;
}
//打开串口
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL, "提示", "无法打开串口!");
return;
}
//下拉菜单控件失效
ui->comboBox->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->comboBox_3->setEnabled(false);
ui->comboBox_4->setEnabled(false);
ui->comboBox_5->setEnabled(false);
//发送按键使能
ui->pushButton->setEnabled(true);
}
else
{
serial.close();
//下拉按键使能
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
//搜索串口按键使能
ui->pushButton_2->setEnabled(true);
//发送按键使能
ui->pushButton->setEnabled(false);
}
}
//接收窗口显示槽函数*************************************************
void Widget::serialPort_readyRead()
{
//从接收缓冲区中读取数据
QByteArray buffer = serial.readAll();
//从界面中读取以前收到的数据
QString recv = ui->textBrowser->toPlainText();
recv += QString(buffer);
//清空以前的显示
ui->textBrowser->clear();
//重新显示
ui->textBrowser->append(recv);
}
//清空显示窗口的槽函数*********************************************
void Widget::on_pushButton_3_clicked()
{
ui->textBrowser->clear();
}
//关闭串口槽函数*********************************************
void Widget::on_pushButton_5_clicked()
{
serial.close();
//下拉按键使能
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
//搜索串口按键使能
ui->pushButton_2->setEnabled(true);
//发送按键使能
ui->pushButton->setEnabled(true);
}
5.效果展示

边栏推荐
猜你喜欢

深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向

Talk about what you know about publishing online (2)

医学公共数据库

Vscode uses eslint prettier to format code automatically

Visual Object Class介绍PASCAL VOC数据集

Talk about what you know about publishing online (I)

谈谈你知道的发布上线(一)

【C语言笔记分享】——动态内存管理malloc、free、calloc、realloc、柔性数组

PCA 报错Error in eigen(crossprod(t(X), t(X)), symmetric = TRUE) : ‘x‘里有无穷值或遗漏值

【无标题】
随机推荐
多大适合学习软件测试?
一篇带你走近Kubernetes概貌与原理
将input type='file' 类型的图片文件转成base64
Factor in R
Uparse rich text style of uni app
漫谈测试平台—平台建设思路(上)
Hgu95av2. Online installation failed
JVM性能调优
Awk of shell script
蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新
The difference between using switch in a loop and using break and continue after executing a condition
AMQP protocol details
MySQL高级-MVCC(超详细整理)
完全未接触过软件测试的人,培训两个月就可以上岗,真的现实吗?
漫谈测试平台—建设模式探讨
Easy to use vscode plug-in memo
2021 年全国大学生数据统计与分析竞赛
Adding new objects to the object array in JS results in the modification of existing objects in the array
Master JVM interview topics and answers offer get soft (with learning roadmap)
The easy-to-use special app testing tool itest4.7.0 has been released