最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT串口通信的實(shí)現(xiàn)方法

 更新時(shí)間:2021年05月20日 10:07:09   作者:菜頭大大  
如果用qt寫程序作為上位機(jī),然后通過和usb和下位機(jī)通信的時(shí)候,就需要用到qt中的串口通信了。本文介紹了QT串口通信的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下

前言:如果用qt寫程序作為上位機(jī),然后通過和usb和下位機(jī)通信的時(shí)候,就需要用到qt中的串口通信了。

使用qt中的串口通信的時(shí)候需要用到的兩個(gè)頭文件分別為:

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

除了加上面兩個(gè)頭文件之外,還需要在工程文件中加下面一行代碼:

QT  += serialport

我們一般都需要先定義一個(gè)全局的串口對(duì)象,記得在自己的頭文件中添加上:

QSerialPort *serial;

到這里我們就可以調(diào)用qt串口通信中的函數(shù)了,一般來講qt串口通信需要經(jīng)過7步:

1、設(shè)置串口名(如COM1)

 serial = new QSerialPort;
 serial->setPortName(ui->PortBox->currentText());

這里我使用自動(dòng)尋找可用串口的方法,直接自動(dòng)設(shè)置了

foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        QSerialPort serial;
        serial.setPort(info);
        if(serial.open(QIODevice::ReadWrite))
        {
            ui->PortBox->addItem(serial.portName());
            serial.close();
        }
    }

2、打開串口

serial->open(QIODevice::ReadWrite);

3、設(shè)置波特率(如115200)

serial->setBaudRate(QSerialPort::Baud115200);//設(shè)置波特率為115200

4、設(shè)置數(shù)據(jù)位(如8)

serial->setDataBits(QSerialPort::Data8);//設(shè)置數(shù)據(jù)位8

5、設(shè)置校驗(yàn)位(如0)

serial->setParity(QSerialPort::NoParity); //校驗(yàn)位設(shè)置為0

6、設(shè)置停止位(如1)

 serial->setStopBits(QSerialPort::OneStop);//停止位設(shè)置為1

7、設(shè)置流控制

 serial->setFlowControl(QSerialPort::NoFlowControl);//設(shè)置為無流控制

到這里串口通信的設(shè)置就完成了,下面我們需要實(shí)現(xiàn)對(duì)數(shù)據(jù)的發(fā)送和接收

1、連接數(shù)據(jù)接收槽函數(shù),下位機(jī)中一有數(shù)據(jù)發(fā)送過來的時(shí)候就會(huì)響應(yīng)這個(gè)槽函數(shù)

QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadData);

2、從上位機(jī)發(fā)送數(shù)據(jù)到下位機(jī)

serial->write(ui->textEdit_2->toPlainText().toLatin1());

主要使用的函數(shù)就這些了,我們來看看代碼:

1、工程文件SerialPortTool.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-11-17T15:43:04
#
#-------------------------------------------------

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SerialPortTool
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 \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

2、頭文件mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_OpenSerialButton_clicked();

    void ReadData();

    void on_SendButton_clicked();

private:
    Ui::MainWindow *ui;
    QSerialPort *serial;
};

#endif // MAINWINDOW_H

3、源文件mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


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();
        }
    }
    //設(shè)置波特率下拉菜單默認(rèn)顯示第0項(xiàng)
    ui->BaudBox->setCurrentIndex(0);

}

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

void MainWindow::on_OpenSerialButton_clicked()
{
    if(ui->OpenSerialButton->text() == tr("打開串口"))
    {
        serial = new QSerialPort;
        //設(shè)置串口名
        serial->setPortName(ui->PortBox->currentText());
        //打開串口
        serial->open(QIODevice::ReadWrite);
        //設(shè)置波特率
        serial->setBaudRate(QSerialPort::Baud115200);//設(shè)置波特率為115200
        //設(shè)置數(shù)據(jù)位數(shù)
        switch (ui->BitBox->currentIndex())
        {
        case 8:
            serial->setDataBits(QSerialPort::Data8);//設(shè)置數(shù)據(jù)位8
            break;
        default:
            break;
        }
        //設(shè)置校驗(yàn)位
        switch (ui->ParityBox->currentIndex())
        {
        case 0:
            serial->setParity(QSerialPort::NoParity);
            break;
        default:
            break;
        }
        //設(shè)置停止位
        switch (ui->BitBox->currentIndex())
        {
        case 1:
            serial->setStopBits(QSerialPort::OneStop);//停止位設(shè)置為1
            break;
        case 2:
            serial->setStopBits(QSerialPort::TwoStop);
        default:
            break;
        }
        //設(shè)置流控制
        serial->setFlowControl(QSerialPort::NoFlowControl);//設(shè)置為無流控制

        //關(guān)閉設(shè)置菜單使能
        ui->PortBox->setEnabled(false);
        ui->BaudBox->setEnabled(false);
        ui->BitBox->setEnabled(false);
        ui->ParityBox->setEnabled(false);
        ui->StopBox->setEnabled(false);
        ui->OpenSerialButton->setText(tr("關(guān)閉串口"));

        //連接信號(hào)槽
        QObject::connect(serial,&QSerialPort::readyRead,this,&MainWindow::ReadData);
    }
    else
    {
        //關(guān)閉串口
        serial->clear();
        serial->close();
        serial->deleteLater();

        //恢復(fù)設(shè)置使能
        ui->PortBox->setEnabled(true);
        ui->BaudBox->setEnabled(true);
        ui->BitBox->setEnabled(true);
        ui->ParityBox->setEnabled(true);
        ui->StopBox->setEnabled(true);
        ui->OpenSerialButton->setText(tr("打開串口"));

    }




}
//讀取接收到的信息
void MainWindow::ReadData()
{
    QByteArray buf;
    buf = serial->readAll();
    if(!buf.isEmpty())
    {
        QString str = ui->textEdit->toPlainText();
        str+=tr(buf);
        ui->textEdit->clear();
        ui->textEdit->append(str);

    }
    buf.clear();
}

//發(fā)送按鈕槽函數(shù)
void MainWindow::on_SendButton_clicked()
{
    serial->write(ui->textEdit_2->toPlainText().toLatin1());
}

4、界面文件mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>547</width>
    <height>470</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>50</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>串口</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>90</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>波特率</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>130</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>數(shù)據(jù)位</string>
    </property>
   </widget>
   <widget class="QComboBox" name="PortBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>50</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
   </widget>
   <widget class="QComboBox" name="BaudBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>90</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <item>
     <property name="text">
      <string>9600</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>19200</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>38400</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>57600</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>115200</string>
     </property>
    </item>
   </widget>
   <widget class="QComboBox" name="BitBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>120</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <item>
     <property name="text">
      <string>8</string>
     </property>
    </item>
   </widget>
   <widget class="QComboBox" name="ParityBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>160</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>0</string>
     </property>
    </item>
   </widget>
   <widget class="QLabel" name="label_4">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>160</y>
      <width>61</width>
      <height>16</height>
     </rect>
    </property>
    <property name="text">
     <string>校驗(yàn)位</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_6">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>200</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>停止位</string>
    </property>
   </widget>
   <widget class="QComboBox" name="StopBox">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>200</y>
      <width>69</width>
      <height>22</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>1</string>
     </property>
    </item>
   </widget>
   <widget class="QPushButton" name="OpenSerialButton">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>240</y>
      <width>71</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>打開串口</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>30</y>
      <width>221</width>
      <height>291</height>
     </rect>
    </property>
   </widget>
   <widget class="QTextEdit" name="textEdit_2">
    <property name="geometry">
     <rect>
      <x>200</x>
      <y>330</y>
      <width>221</width>
      <height>31</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="SendButton">
    <property name="geometry">
     <rect>
      <x>430</x>
      <y>330</y>
      <width>75</width>
      <height>31</height>
     </rect>
    </property>
    <property name="text">
     <string>發(fā)送</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>547</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

效果圖如下,自己設(shè)置對(duì)應(yīng)下位機(jī)的波特率就可以實(shí)現(xiàn)數(shù)據(jù)收發(fā)了

這里注意一下,使用串口通信的時(shí)候是按字節(jié)發(fā)送的,所以如果你定義一個(gè)char buff[10],而且你想這樣定義buff[0] = '255'發(fā)送255這個(gè)字符給下位機(jī)的時(shí)候,下位機(jī)是接收不完整的,經(jīng)過測(cè)試發(fā)現(xiàn)發(fā)送大于或等于10的字符是會(huì)被截?cái)嗟?,只?huì)留下最后一個(gè)字符,比如說發(fā)送10字符的時(shí)候,下位機(jī)很有可能只能接收到0這個(gè)字符,當(dāng)然如果想要完整的發(fā)送過去的話可以定義成字符串形式。比如char buff[] ="255",這樣就可以發(fā)送一個(gè)完整的255過去了,但是需要注意的是這是一個(gè)字符串不是一個(gè)字符,所以如果你在下位機(jī)如果要根據(jù)上位機(jī)發(fā)送的數(shù)據(jù)來處理一些事情的時(shí)候一定要清楚你發(fā)送的是字符還是字符串。

到此這篇關(guān)于QT串口通信的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)QT串口通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++ 實(shí)戰(zhàn)開發(fā)一個(gè)猜單詞的小游戲

    C++ 實(shí)戰(zhàn)開發(fā)一個(gè)猜單詞的小游戲

    眾所周知紙上得來終覺淺,我們要在實(shí)戰(zhàn)中才能真正的掌握技術(shù),小編為大家?guī)硪环萦肅++編寫的猜單詞小游戲,給大家練練手,快來看看吧
    2021-11-11
  • C語言sizeof和strlen區(qū)別小結(jié)

    C語言sizeof和strlen區(qū)別小結(jié)

    C語言中的sizeof和strlen是兩個(gè)常用的操作符/函數(shù),但它們的功能和用途有很大的區(qū)別,本文就詳細(xì)的來介紹一下C語言sizeof和strlen區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • C++實(shí)現(xiàn)并行計(jì)算的兩種方式

    C++實(shí)現(xiàn)并行計(jì)算的兩種方式

    本文介紹了使用C++實(shí)現(xiàn)并行計(jì)算的兩種方式,包括OpenMP和MPI,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • 使用C++實(shí)現(xiàn)迷宮游戲

    使用C++實(shí)現(xiàn)迷宮游戲

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)迷宮游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++核心編程之類和對(duì)象詳解

    C++核心編程之類和對(duì)象詳解

    這篇文章主要介紹了C++編程進(jìn)階之類和對(duì)象用法等相關(guān)使用技巧,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09
  • C語言實(shí)現(xiàn)圖書館管理系統(tǒng)

    C語言實(shí)現(xiàn)圖書館管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)圖書館管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 分析C語言一個(gè)簡單程序

    分析C語言一個(gè)簡單程序

    本文主要介紹C語言簡單的程序,這里給大家詳細(xì)介紹C語言代碼,對(duì)函數(shù)概念、頭文件、自定義函數(shù)等基礎(chǔ)信息的講解,希望能幫助剛剛學(xué)習(xí)的同學(xué)
    2016-07-07
  • 利用c++寫一個(gè)簡單的推箱子小游戲

    利用c++寫一個(gè)簡單的推箱子小游戲

    推箱子想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用c++寫一個(gè)簡單的推箱子小游戲的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • C++控制臺(tái)強(qiáng)化如何實(shí)現(xiàn)一定界面效果(簡潔版)

    C++控制臺(tái)強(qiáng)化如何實(shí)現(xiàn)一定界面效果(簡潔版)

    這篇文章主要介紹了C++控制臺(tái)強(qiáng)化如何實(shí)現(xiàn)一定界面效果(簡潔版),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Matlab實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)顯示方法

    Matlab實(shí)現(xiàn)數(shù)據(jù)的動(dòng)態(tài)顯示方法

    這篇文章主要為大家詳細(xì)介紹了Matlab使用Plot函數(shù)實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)顯示方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06

最新評(píng)論

阳西县| 黑河市| 沿河| 新宁县| 磐石市| 青铜峡市| 平罗县| 双流县| 芮城县| 沅陵县| 新乐市| 澎湖县| 宁海县| 上饶市| 许昌县| 泰和县| 柳江县| 蓝田县| 额敏县| 突泉县| 通道| 乐都县| 寻甸| 玉龙| 玉门市| 平塘县| 尼玛县| 揭西县| 灌云县| 泸定县| 湘潭县| 介休市| 高要市| 深州市| 辰溪县| 蛟河市| 长寿区| 阳新县| 兰州市| 舞钢市| 青岛市|