QT上位机开发之串口助手
一.qt的三驾马车
1.qt下的串口编程
2.qt下的网络编程
3.qt下操作GPIO
二.仿写串口助手
MySerial.pro
#-------------------------------------------------
#
# Project created by QtCreator 2024-04-12T18:07:26
#
#-------------------------------------------------
QT += core gui serialport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MySerial
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
RC_ICONS=usb.ico
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtSerialPort>
#include <QString>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QSerialPort *serialPort;
private slots:
void on_openBt_clicked();
void on_closeBt_clicked();
void serialPortReadyRead_slot();
void on_sendBt_clicked();
void on_clearBt_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QSerialPortInfo>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QStringList serialNamePort;
serialPort = new QSerialPort(this);
connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialPortReadyRead_slot()));
/* 获取串口号 */
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
serialNamePort<<info.portName();
}
ui->serialCb->addItems(serialNamePort);
}
Widget::~Widget()
{
delete ui;
}
void Widget::serialPortReadyRead_slot()
{
QString buf;
buf = QString(serialPort->readAll());
ui->receiveEdit->appendPlainText(buf);
}
void Widget::on_openBt_clicked()
{
QSerialPort::BaudRate baudRate; //比特率
QSerialPort::DataBits dataBits; //数据位
QSerialPort::StopBits stopBits; //停止位
QSerialPort::Parity cheakBits; //校验位
if(ui->baudrateCb->currentText() == "4800")
{
baudRate = QSerialPort::Baud4800;
}else if(ui->baudrateCb->currentText() == "9600")
{
baudRate = QSerialPort::Baud9600;
}else if(ui->baudrateCb->currentText() == "115200")
{
baudRate = QSerialPort::Baud115200;
}
if(ui->dataCb->currentText() == "5")
{
dataBits = QSerialPort::Data5;
}else if(ui->dataCb->currentText() == "6")
{
dataBits = QSerialPort::Data6;
}else if(ui->dataCb->currentText() == "7")
{
dataBits = QSerialPort::Data7;
}else if(ui->dataCb->currentText() == "8")
{
dataBits = QSerialPort::Data8;
}
if(ui->stopCb->currentText() == "1")
{
stopBits = QSerialPort::OneStop;
}else if(ui->stopCb->currentText() == "1.5")
{
stopBits = QSerialPort::OneAndHalfStop;
}else if(ui->stopCb->currentText() == "2")
{
stopBits = QSerialPort::TwoStop;
}
if(ui->checkCb->currentText() == "none")
{
cheakBits = QSerialPort::NoParity;
}
serialPort->setPortName(ui->serialCb->currentText());
serialPort->setBaudRate(baudRate);
serialPort->setDataBits(dataBits);
serialPort->setStopBits(stopBits);
serialPort->setParity(cheakBits);
/* 检测串口是否打开成功 */
if(serialPort->open(QIODevice::ReadWrite) == true)
{
QMessageBox::information(this, "提示", "成功");
}else
{
QMessageBox::critical(this, "提示", "错误");
}
}
void Widget::on_closeBt_clicked()
{
serialPort->close();
}
void Widget::on_sendBt_clicked()
{
serialPort->write(ui->sendEdit->text().toLocal8Bit().data());
}
void Widget::on_clearBt_clicked()
{
ui->receiveEdit->clear();
}
widget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QPlainTextEdit" name="receiveEdit">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>串口号:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="serialCb">
<property name="minimumSize">
<size>
<width>87</width>
<height>21</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>波特率:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="baudrateCb">
<property name="minimumSize">
<size>
<width>87</width>
<height>21</height>
</size>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<item>
<property name="text">
<string>4800</string>
</property>
</item>
<item>
<property name="text">
<string>9600</string>
</property>
</item>
<item>
<property name="text">
<string>115200</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>数据位:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="dataCb">
<property name="minimumSize">
<size>
<width>87</width>
<height>21</height>
</size>
</property>
<property name="currentIndex">
<number>3</number>
</property>
<item>
<property name="text">
<string>5</string>
</property>
</item>
<item>
<property name="text">
<string>6</string>
</property>
</item>
<item>
<property name="text">
<string>7</string>
</property>
</item>
<item>
<property name="text">
<string>8</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>停止位:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="stopCb">
<property name="minimumSize">
<size>
<width>87</width>
<height>21</height>
</size>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>1.5</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>校验位:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="checkCb">
<property name="minimumSize">
<size>
<width>87</width>
<height>21</height>
</size>
</property>
<item>
<property name="text">
<string>none</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="1" column="1">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="minimumSize">
<size>
<width>531</width>
<height>80</height>
</size>
</property>
<property name="title">
<string/>
</property>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>450</width>
<height>50</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>450</width>
<height>50</height>
</size>
</property>
<property name="font">
<font>
<pointsize>22</pointsize>
</font>
</property>
<property name="text">
<string>自制串口助手</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</widget>
</item>
<item>
<widget class="QLineEdit" name="sendEdit">
<property name="minimumSize">
<size>
<width>536</width>
<height>30</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QPushButton" name="openBt">
<property name="text">
<string>打开串口</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="closeBt">
<property name="text">
<string>关闭串口</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="sendBt">
<property name="text">
<string>发送数据</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="clearBt">
<property name="text">
<string>清空数据</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
标签:03,Widget,QT,void,QSerialPort,serialPort,currentText,ui,串口
From: https://www.cnblogs.com/mzx233/p/18133574