03_QT上位機開發之串列埠助手

爱吃冰激凌的黄某某發表於2024-04-13

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>

相關文章