当前位置:网站首页>QT programming serial port assistant
QT programming serial port assistant
2022-07-28 17:44:00 【Floating boast】
QT Write serial port assistant
1.pro File adding serialport

2.widget.h Documentation
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSerialPort> // Provides the function of accessing the serial port
#include <QSerialPortInfo> // Provide the information of the serial port in the system
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void set_baudList(); // Set baud rate option
void set_parityList();// Set check bit options
void set_dataBitsList();// Set data bit options
void set_stopBitsList();// Set the stop bit option
void serial_Init();// Serial port setting initialization
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 Documentation
#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 Documentation
#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(" Serial assistant ");
// Set window fixed size
this->setMaximumSize(800,550);
this->setMinimumSize(800,550);
// adopt QSerialPortInfo Find available serial ports
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(info.portName());
}
// Connecting signals and slots
QObject::connect(&serial, &QSerialPort::readyRead, this, &Widget::serialPort_readyRead);
// Serial port setting initialization
serial_Init();
}
Widget::~Widget()
{
delete ui;
}
// Set baud rate option function **********************************************
void Widget::set_baudList()
{
QStringList baudList; // Baud rate object
baudList<<"1200"<<"2400"<<"4800"<<"9600"
<<"14400"<<"19200"<<"38400"<<"56000"
<<"57600"<<"115200";
ui->comboBox_2->addItems(baudList);
ui->comboBox_2->setCurrentIndex(3);
}
// Set check bit function **********************************************
void Widget::set_parityList()
{
QStringList parityList; // Check bit object
parityList<<" nothing "<<" Odd check "<<" Even check ";
ui->comboBox_5->addItems(parityList);
ui->comboBox_5->setCurrentIndex(0);
}
// Set the data bit function **********************************************
void Widget::set_dataBitsList()
{
QStringList dataBitsList; // Data bit object
dataBitsList<<"5"<<"6"<<"7"<<"8";
ui->comboBox_4->addItems(dataBitsList);
ui->comboBox_4->setCurrentIndex(3);
}
// Set stop bit function **********************************************
void Widget::set_stopBitsList()
{
QStringList stopBitsList; // Stop bit object
stopBitsList<<"1"<<"2";
ui->comboBox_3->addItems(stopBitsList);
ui->comboBox_3->setCurrentIndex(0);
}
// Serial port setting initialization function *********************************************
void Widget::serial_Init()
{
// Set baud rate options
set_baudList();
// Set check bit options
set_parityList();
// Set data bit options
set_dataBitsList();
// Set the stop bit option
set_stopBitsList();
}
// Detect serial port slot function *****************************************************
void Widget::on_pushButton_2_clicked()
{
ui->comboBox->clear();
// adopt QSerialPortInfo Find available serial ports
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(info.portName());
}
}
// Open the serial slot function ****************************************************
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text()==QString(" Open the serial port "))
{
// Set serial port name
serial.setPortName(ui->comboBox->currentText());
// set baud rate
serial.setBaudRate(ui->comboBox_2->currentText().toInt());
// set data bit
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;
}
// Set parity
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;
}
// set flow control
serial.setFlowControl(QSerialPort::NoFlowControl);
// Set stop bit
switch(ui->comboBox_3->currentIndex())
{
case 1: serial.setStopBits(QSerialPort::OneStop); break;
case 2: serial.setStopBits(QSerialPort::TwoStop); break;
default: break;
}
// Open the serial port
if(!serial.open(QIODevice::ReadWrite))
{
QMessageBox::about(NULL, " Tips ", " Unable to open serial port !");
return;
}
// The drop-down menu control fails
ui->comboBox->setEnabled(false);
ui->comboBox_2->setEnabled(false);
ui->comboBox_3->setEnabled(false);
ui->comboBox_4->setEnabled(false);
ui->comboBox_5->setEnabled(false);
// Send key enable
ui->pushButton->setEnabled(true);
}
else
{
serial.close();
// Pull down the key to enable
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
// Search serial port key enable
ui->pushButton_2->setEnabled(true);
// Send key enable
ui->pushButton->setEnabled(false);
}
}
// The receiving window displays the slot function *************************************************
void Widget::serialPort_readyRead()
{
// Read data from receive buffer
QByteArray buffer = serial.readAll();
// Read the previously received data from the interface
QString recv = ui->textBrowser->toPlainText();
recv += QString(buffer);
// Clear the previous display
ui->textBrowser->clear();
// To display
ui->textBrowser->append(recv);
}
// Clear the slot function of the display window *********************************************
void Widget::on_pushButton_3_clicked()
{
ui->textBrowser->clear();
}
// Close the serial slot function *********************************************
void Widget::on_pushButton_5_clicked()
{
serial.close();
// Pull down the key to enable
ui->comboBox->setEnabled(true);
ui->comboBox_2->setEnabled(true);
ui->comboBox_3->setEnabled(true);
ui->comboBox_4->setEnabled(true);
ui->comboBox_5->setEnabled(true);
// Search serial port key enable
ui->pushButton_2->setEnabled(true);
// Send key enable
ui->pushButton->setEnabled(true);
}
5. Effect display

边栏推荐
猜你喜欢
![【C语言进阶】——指针进阶[Ⅰ]](/img/62/d3410a61b931177fc02c1801489b5a.png)
【C语言进阶】——指针进阶[Ⅰ]

C#中virtual(虚方法)的理解以及和abstract(抽象方法)的区别

An article takes you closer to the overview and principle of kubernetes

【p5.js学习笔记】局部变量(let)与全局变量(var)声明

软件测试真有网上说的那么好吗?

Talk about the measurement of "post release problems"

Jdwp unauthorized rapid utilization

In depth sharing of Ali (ant financial) technical interview process, with preliminary preparation and learning direction
![【C语言进阶】——剖析入微数据在内存中的存储[上]](/img/6a/ac723cee2543cd2403a7e58d556c8e.png)
【C语言进阶】——剖析入微数据在内存中的存储[上]

Random talk on test platform - platform construction ideas (Part 1)
随机推荐
mmdetection3d(2)---结果、log可视化
三维点云处理系列----PCA
【 R语言—基础绘图】
软件测试工程师如何突破职业发展瓶颈
软件测试前景如何?该如何进行学习呢?
编译原理学习笔记2(语法分析介绍)
培训软件测试能不能就业
Random talk on test platform - platform construction ideas (Part 1)
Multithreading (ThreadPoolExecutor of thread pool)
ROS系统安装
零基础学习软件测试有什么条件?
The browser has no Internet, and wechat can connect to the Internet (solution)
mmdetection3d(3)---网络输出
软件测试的培训机构靠谱吗
Precautions for $ionicpopup in ionic when calling alert for two consecutive times
Distinguish between the export of ES6 and the module.exports of nodejs
小白如何零基础学习软件测试?
Adding new objects to the object array in JS results in the modification of existing objects in the array
Hgu95av2. Online installation failed
【p5.js学习笔记】局部变量(let)与全局变量(var)声明