首页 > 其他分享 >03_QT上位机开发之串口助手

03_QT上位机开发之串口助手

时间:2024-04-13 23:23:42浏览次数:25  
标签:03 Widget QT void QSerialPort serialPort currentText ui 串口

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

相关文章

  • 02_QT信号和槽
    1.给控件改名字为了分析代码方便,我们要给控件改名字。要通俗易懂。2.什么是信号和槽信号:信号就是指控件发出的特定的信号。比如按钮的信号:![img](file:///C:/Users/机械革命/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png)槽:槽就是槽函数的意思,我们可以把槽函数绑......
  • 4-WIFI&蓝牙(ESP32)转CAN或RS485总线&串口TTL模块-CSDK-设备作为TCP客户端,实现上位机
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/ESP32_CAN"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p> 说明这节设备作为TCP客户端,连接上位......
  • Qt | 一文总结QObject
    Qt|一文总结QObject来源 https://zhuanlan.zhihu.com/p/608004472 一、QObject的重要知识QObject是Qt对象模型的核心。这个模型的核心特性是一个强大的无缝对象通信机制,即信号和槽。可以使用connect()将信号连接到槽函数,并使用disconnect()破坏已经存在的连接。为了避免永......
  • 手把手教你做阅读理解提高002-A Boy's Journey with Old Shirts-一个男孩与旧衬衫的旅
    PDF格式公众号回复关键字:ZKYDT002阅读理解技巧,在帮助读者有效获取和理解文本信息方面发挥着重要作用,熟练掌握如下6个技巧,可快速突破阅读理解1预览文章结构在开始深入阅读之前,快速浏览文章的标题、段落开头和结尾,可以迅速把握文章的主题、大致内容和结构标题通常能概括文......
  • 3-WIFI&蓝牙(ESP32)转CAN或RS485总线&串口TTL模块-CSDK--设备作为TCP服务器,实现上位
    <p><iframename="ifd"src="https://mnifdv.cn/resource/cnblogs/ESP32_CAN"frameborder="0"scrolling="auto"width="100%"height="1500"></iframe></p> 说明这节设备作为TCP服务器,上位机T......
  • Docker mysql 配置文件读取失败 [Warning] World-writable config file '/etc/mysql/c
    这个警告表明MySQL正在忽略/etc/mysql/conf.d/my.cnf这个配置文件,因为它被设置为了全世界可写。这个警告是基于安全性的考虑。当配置文件被设置为全世界可写时,任何人都可以修改它,这可能会导致安全风险,因为恶意用户可以更改MySQL的配置,从而影响数据库的行为和安全性。为了......
  • 【Qt】编写的qt程序,如何把依赖的dll自动拷贝到exe同级目录
    如果你的Qt程序在VisualStudio中可以正常启动但直接启动exe文件会报错,可能是因为缺少依赖的QtDLL文件导致的。为了确定缺少了哪些DLL文件,你可以尝试以下方法:使用DependencyWalker:DependencyWalker是一个工具,可以帮助你分析可执行文件的依赖关系,并找出缺失的D......
  • BZOJ 4403序列统计
    假设存在一个满足条件的长度为i的不下降序列(显然是一定存在的)那么只需要从中选出i个数即可(不必在意选出具体数的大小,可以把满足条件的序列写下来,选几个数感受一下)。但是$n\choosem$里的\(m\)的是就是\((r-l+1)\)吗?乍一看是这样的,但是这样会出现一个问题,单调不下降子序......
  • 2.创建Qt项目
    2.创建Qt项目2.1使用向导创建打开QtCreator界面选择NewProject或者选择菜单栏【文件】-【新建文件或项目】菜单项弹出NewProject对话框,选择QtWidgetsApplication,选择【Choose】按钮,弹出如下对话框设置项目名称和路径,按照向导进行下一步,选择编译套件向导会默认......
  • ESP32 Arduino开发 MQTT
    ESP32Arduino开发MQTT目录ESP32Arduino开发MQTT1.安装程序库2.编写相关程序2.1.引入头文件2.2.定义MQTT相关参数2.3.创建对象2.4.连接网络2.5.连接MQTT服务器2.6.MQTT回调函数3.完整的代码例程4.MQTT连接测试1.安装程序库打开库管理工具工具->管理库.........