栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

qt系列之一个简单上位机

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

qt系列之一个简单上位机

qt系列之一个简单上位机

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


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

文章目录
  • qt系列之一个简单上位机
  • 前言
  • 一、实现简单读取数据
  • 二、使用步骤
    • 1.项目文件,在里面增加一个serialport
    • 2.头文件,写函数声明
    • 3.类中函数的实现
    • 4.主函数,生成可执行文件
    • 5.ui文件,注意:ui文件不可修改
  • 总结


前言

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

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


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

一、实现简单读取数据

使用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 
#include 
#include 
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 

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

    return a.exec();
}

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


 Widget
 
  
   
    0
    0
    400
    300
   
  
  
   Widget
  
  
   
    
     20
     10
     351
     181
    
   
  
  
   
    
     50
     230
     87
     22
    
   
   
    
     1200
    
   
   
    
     2400
    
   
   
    
     4800
    
   
   
    
     9600
    
   
   
    
     12500
    
   
  
  
   
    
     240
     230
     93
     28
    
   
   
    open
   
  
 
 
 
 



生成的可执行文件:

总结

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1019099.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号