qt系列之一个简单上位机

qt系列之一个简单上位机

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,串口通信这门技术也越来越重要,很多人都开启了学习串口,本文就介绍了串口通信的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、实现简单读取数据

使用qt5的QSerialPort类,所有代码都在文中。
采用qt5 c++实现,
创建一个qt工程文件夹,名字命名为samp2_7,基类选择widget类。

二、使用步骤

1.项目文件,在里面增加一个serialport

//samp2_7.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-07-18T17:43:55
#
#-------------------------------------------------

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = samp2_7
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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

2.头文件,写函数声明

//widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:


    void on_openPortButton_clicked();

    void read_com();

private:
    Ui::Widget *ui;
    QSerialPort serial1;
};

#endif // WIDGET_H

3.类中函数的实现

//widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}



void Widget::on_openPortButton_clicked()
{
    //打开串口
    if(ui->portNameBox->isEnabled())  //如果串口能够操作
    {
        ui->openPortButton->setText("closePort");  //调用函数修改按钮显示为closePort
        ui->portNameBox->setDisabled(true); //禁止再修改COM串口

        serial1.setPortName(ui->portNameBox->currentText());  //得到串口名,设置COM口
        serial1.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);  //设置波特率和读写方向
        serial1.open(QIODevice::ReadWrite);
    }
    else{
        ui->openPortButton->setText("openPort");  //按下closeport之后,按键显示为openport
        ui->portNameBox->setEnabled(true);  //按下closeport后,COM口可以修改
        serial1.close();  //关闭串口
    }

}

//槽函数,在槽函数之后使用readAll()把收到的数据读到requestData中。
void Widget::read_com()
{
    QByteArray requestData;
    requestData = serial1.readAll();
    QString buf;
    ui->plainTextEdit->appendPlainText(requestData.toHex());
}

4.主函数,生成可执行文件

//main.cpp
#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

5.ui文件,注意:ui文件不可修改

//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>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QPlainTextEdit" name="plainTextEdit">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>10</y>
     <width>351</width>
     <height>181</height>
    </rect>
   </property>
  </widget>
  <widget class="QComboBox" name="portNameBox">
   <property name="geometry">
    <rect>
     <x>50</x>
     <y>230</y>
     <width>87</width>
     <height>22</height>
    </rect>
   </property>
   <item>
    <property name="text">
     <string>1200</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>2400</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>4800</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>9600</string>
    </property>
   </item>
   <item>
    <property name="text">
     <string>12500</string>
    </property>
   </item>
  </widget>
  <widget class="QPushButton" name="openPortButton">
   <property name="geometry">
    <rect>
     <x>240</x>
     <y>230</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>open</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


生成的可执行文件:
在这里插入图片描述

总结

提示:这里对文章进行总结:

以上就是今天要讲的内容,本文仅仅简单介绍了qt串口组件的简单使用,而qt提供了大量能使我们快速便捷地处理串口数据的函数和方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CN-JackZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值