zoukankan      html  css  js  c++  java
  • QT串口通信编程

    QT串口编程

    文件夹目录结构如下图所示

    设计的示例界面如下图所示

    首先在项目文件里面添加一句

    QT       += serialport
    

    SerialPortDemo.pro文件如下:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-02-21T13:23:59
    #
    #-------------------------------------------------
    
    QT   += core gui
    QT   += serialport
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = SerialPortDemo
    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
    
    CONFIG += c++11
    
    SOURCES += 
    main.cpp 
    mainwindow.cpp
    
    HEADERS += 
    mainwindow.h
    
    FORMS += 
    mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    在头文件mainwindow.h中引入qt串口通信所需要的头文件,mainwindow.h文件代码如下:

        #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QDebug>
    
    //引入qt中串口通信需要的头文件
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    
    public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    
    private slots:
    void on_OpenSerialButton_clicked();
    
    void ReadData();
    
    void on_btnSend_clicked();
    
    private:
    Ui::MainWindow *ui;
    
    QSerialPort *serial;//全局的串口对象
    
    };
    
    #endif // MAINWINDOW_H
    ##实现效果的mainwindow.cpp代码如下:
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    //添加串口通信需要用到的两个串口头文件
    #include "QtSerialPort/QSerialPort"
    #include "QtSerialPort/QSerialPortInfo"
    
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    
    //查找可用的串口
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
    QSerialPort serial;
    serial.setPort(info);
    if(serial.open(QIODevice::ReadWrite))
    {
    ui->portBox->addItem(serial.portName());
    serial.close();
    }
    }
    
    //设置波特率下拉菜单的第0项默认值
    ui->baudBox->setCurrentIndex(0);
    }
    
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    
    void MainWindow::on_OpenSerialButton_clicked()
    {
    if(ui->OpenSerialButton->text()==tr("打开串口"))
    {
     serial=new QSerialPort;
     //设置串口名
     serial->setPortName(ui->portBox->currentText());
     //打开串口
     serial->open(QIODevice::ReadWrite);
     //设置波特率
     serial->setBaudRate(QSerialPort::Baud115200);
     //设置数据为
     switch(ui->dataBox->currentIndex())
     {
     case 0:
     serial->setDataBits(QSerialPort::Data8);
     break;
     default:
     break;
     }
     //设置校验位
     switch (ui->checkBox->currentIndex())
     {
     case 0:
     serial->setParity(QSerialPort::NoParity);
     break;
     default:
     break;
     }
     //设置停止为
     switch(ui->stopBox->currentIndex())
     {
     case 0:
     serial->setStopBits(QSerialPort::OneStop);
     break;
     case 1:
     serial->setStopBits(QSerialPort::TwoStop);
     break;
     default:
     break;
     }
     //设置流控制
     serial->setFlowControl(QSerialPort::NoFlowControl);//设置为无流控制
    
     //关闭设置菜单使能
     ui->portBox->setEnabled(false);
     ui->dataBox->setEnabled(false);
     ui->checkBox->setEnabled(false);
     ui->stopBox->setEnabled(false);
     ui->baudBox->setEnabled(false);
     ui->OpenSerialButton->setText("关闭串口");
    
    
     QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadData);
    }
    else
    {
    //关闭串口
    serial->clear();
    serial->close();
    serial->deleteLater();
    
    //恢复使能
    ui->portBox->setEnabled(true);
    ui->baudBox->setEnabled(true);
    ui->dataBox->setEnabled(true);
    ui->checkBox->setEnabled(true);
    ui->stopBox->setEnabled(true);
    ui->OpenSerialButton->setText("打开串口");
    }
    
    }
    
    
    void MainWindow::on_btnSend_clicked()
    {
    serial->write(ui->txtWrite->toPlainText().toLatin1());
    }
    
    //读取接收到的消息
    void MainWindow::ReadData()
    {
    QByteArray buf;
    
    buf=serial->readAll();
    if(!buf.isEmpty())
    {
    QString str = buf;
    ui->txtRead->appendPlainText(str);
    }
    buf.clear();
    }
    

    最终运行效果如下图所示:

  • 相关阅读:
    我的第二个思维导图,用来介绍框架
    如何减少基于DataSet框架的代码输入量(一)
    近日
    关于客户端如何获取服务器时间的方法
    匹配用逗号分隔的数字(防sql注入检查)
    十六进制字符串转整形
    sql获取自增行号
    body不出现滚动条方法
    vs2010 无法调试 无法进入断点 断点无效
    Textarea 高度自适应 根据内容自适应高度
  • 原文地址:https://www.cnblogs.com/deng-c-q/p/10413369.html
Copyright © 2011-2022 走看看