当前位置:网站首页>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.效果展示

边栏推荐
猜你喜欢

The easy-to-use special app testing tool itest4.7.0 has been released

Arya-专业web自动化测试平台

Management of third-party technical services in product development

easyui tree
![【C语言进阶】——剖析入微数据在内存中的存储[上]](/img/6a/ac723cee2543cd2403a7e58d556c8e.png)
【C语言进阶】——剖析入微数据在内存中的存储[上]

从非儿童网站看基线安全到底有多重要

ionic 中遇到的一些东西

AMQP protocol details

Backup and restore of SNAT and DNAT firewall rules

Sed of shell programming
随机推荐
软件测试就业前景如何?
dos命令大全 基础命令+网络的常用命令
软件测试零基础入门好学吗?
掌握JVM面试专题和答案Offer拿到手软(附学习路线图)
Uparse rich text style of uni app
ggplot2地图
蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新
Random talk on test platform - platform construction ideas (Part 1)
100+医学影像数据集集锦
一篇带你走近Kubernetes概貌与原理
面试官:算法刷题实录.pdf我居然答不上来
生信人的20个R语言习题
Jdwp unauthorized rapid utilization
零基础软件测试培训可靠吗?
软件测试培训两个月可以就业吗?
医学公共数据库
The browser has no Internet, and wechat can connect to the Internet (solution)
Can‘t use an undefined value as an ARRAY reference at probe2symbol
Editor MAVON editor for offline use
Master JVM interview topics and answers offer get soft (with learning roadmap)