• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

qt串口编程

武飞扬头像
拼命Ⅲ郎
帮助1

第一步在pro文件加入:

  1.  
    QT =serialport
  2.  
     
  3.  
     
  4.  
    代码如下:
  5.  
     
  6.  
    QT = core gui
  7.  
    QT =serialport
  8.  
     
  9.  
    greaterThan(QT_MAJOR_VERSION, 4): QT = widgets
  10.  
     
  11.  
     
  12.  
    CONFIG = c 11
  13.  
     
  14.  
    # The following define makes your compiler emit warnings if you use
  15.  
    # any Qt feature that has been marked deprecated (the exact warnings
  16.  
    # depend on your compiler). Please consult the documentation of the
  17.  
    # deprecated API in order to know how to port your code away from it.
  18.  
    DEFINES = QT_DEPRECATED_WARNINGS
  19.  
     
  20.  
    # You can also make your code fail to compile if it uses deprecated APIs.
  21.  
    # In order to do so, uncomment the following line.
  22.  
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
  23.  
    #DEFINES = QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  24.  
     
  25.  
    SOURCES = \
  26.  
    main.cpp \
  27.  
    mainwindow.cpp
  28.  
     
  29.  
    HEADERS = \
  30.  
    mainwindow.h
  31.  
     
  32.  
    FORMS = \
  33.  
    mainwindow.ui
  34.  
     
  35.  
    # Default rules for deployment.
  36.  
    qnx: target.path = /tmp/$${TARGET}/bin
  37.  
    else: unix:!android: target.path = /opt/$${TARGET}/bin
  38.  
    !isEmpty(target.path): INSTALLS = target
学新通

第二步需要加入的头文件:

#include<QtSerialPort/QtSerialPort>

第三步获取当前电脑可用串口名字:

QSerialPortInfo::availablePorts();//将返回一个含有电脑当前的可用串口名字QList<QSerialPortInfo>容器

第四步定义一个串口对象并且设置串口名字,数据,校验,停止等等:

  1.  
    第一步: QSerialPort *serial=new QSerialPort(this);//定义创建一个串口
  2.  
    第二步:serial->setPortName(ui->comboBox->currentText());//设置串口名字里面参数为字符串
  3.  
    第三步:serial->setBaudRate(ui->comboBox->currentText().toInt());//设置波特率面参数为数字
  4.  
    第四步: serial->setDataBits(QSerialPort::Data5);//设置数据位这个参数是qt定义的枚举类型
  5.  
    第五步:serial->setParity(QSerialPort::EvenParity);//设置校验位,参数也是个qt自定义的枚举类型
  6.  
    第六步:serial->setStopBits(QSerialPort::OneStop);//设置停止位,参数也是个qt自定义的枚举类型
  7.  
    第七步:serial->setFlowControl(QSerialPort::NoFlowControl);//设置流控,参数也是个qt自定义的枚举类型
  8.  
    第八步:serial->open(QIODevice::ReadWrite);//以读写的方式打开串口成功与失败都会有返回值
  9.  
    第八步: serial->close();//关闭串口

第五步设置串口有消息来需要响应的槽函数并且显示消息:

  1.  
    第一步定义一个串口有消息来要处理的槽函数:
  2.  
    private slots:
  3.  
    void serialPort_readyRead();//当串口有消息来的时候促发的槽函数
  4.  
    第二步设置信号和槽:
  5.  
    connect(serial,&QSerialPort::readyRead,this,&MainWindow::serialPort_readyRead);
  6.  
    第三步定义槽函数:
  7.  
    void MainWindow::serialPort_readyRead()//当串口有消息来的时候促发的槽函数
  8.  
    {
  9.  
    qDebug()<<"Reveive data";
  10.  
    }
  11.  
    第四步接收信息:
  12.  
    QString Receivetext=serial->readAll();//这个的格式位QString接收到的信息
  13.  
    可以把接收到的信息转化为两总格式:
  14.  
    第一种://ASCII
  15.  
    Receivetext=Receivetext.toLatin1();
  16.  
    第二种://十六进制
  17.  
    Receivetext=Receivetext.toLatin1().toHex();
学新通

第六步发送消息:

  1.  
    发送消息方法:
  2.  
    QByteArray bytearray;
  3.  
    serial->write(bytearray);//参数类型为QByteArray

下面就是我学习写的一个串口助手源码:

  1.  
    头文件#include "mainwindow.h"
  2.  
     
  3.  
    #ifndef MAINWINDOW_H
  4.  
    #define MAINWINDOW_H
  5.  
    #pragma execution_character_set("utf-8")
  6.  
    #include <QMainWindow>
  7.  
    #include<QtSerialPort/QtSerialPort>
  8.  
    #include<QTimer>
  9.  
    #include<QMessageBox>
  10.  
     
  11.  
     
  12.  
    QT_BEGIN_NAMESPACE
  13.  
    namespace Ui { class MainWindow; }
  14.  
    QT_END_NAMESPACE
  15.  
     
  16.  
    class MainWindow : public QMainWindow
  17.  
    {
  18.  
    Q_OBJECT
  19.  
     
  20.  
    public:
  21.  
    MainWindow(QWidget *parent = nullptr);
  22.  
    ~MainWindow();
  23.  
    private slots:
  24.  
    void TimcrEvent();//定义一个槽函数专门处理定时器槽函数(自定义)
  25.  
     
  26.  
    void serialPort_readyRead();//当串口有消息来的时候促发的槽函数(自定义)
  27.  
     
  28.  
    void on_pushButton_clicked();//打开串口转到槽
  29.  
     
  30.  
    void on_checkBox_clicked();//接收格式ASSCII点击后转到槽
  31.  
     
  32.  
    void on_checkBox_2_clicked();//十六进制点击后转到槽
  33.  
     
  34.  
    void on_checkBox_3_clicked();//暂停转到槽
  35.  
     
  36.  
    void on_pushButton_2_clicked();//串口通信发送信息
  37.  
     
  38.  
    void on_pushButton_3_clicked();//清空接收区转到
  39.  
     
  40.  
    void on_pushButton_4_clicked();//清空发送区转到槽
  41.  
     
  42.  
    private:
  43.  
    Ui::MainWindow *ui;
  44.  
    QTimer *timcr;//定义一个定时器
  45.  
    QStringList portStringList;//定义一个容器用来验证是否有串口需要添加
  46.  
    QSerialPort *serial;//定义一个串口
  47.  
    QString Receivetext;//获取串口发来的信息
  48.  
    long Receive_Byte;//统计收到的字节
  49.  
    QString Sendtext;//要发送的信息
  50.  
    long Send_Byte;//统计发送了多少字节
  51.  
     
  52.  
    };
  53.  
    #endif // MAINWINDOW_H
  54.  
     
  55.  
     
  56.  
     
  57.  
     
  58.  
     
  59.  
     
  60.  
     
  61.  
     
  62.  
    源文件#include "mainwindow.h"
  63.  
     
  64.  
     
  65.  
     
  66.  
     
  67.  
    #include "mainwindow.h"
  68.  
    #include "ui_mainwindow.h"
  69.  
     
  70.  
    MainWindow::MainWindow(QWidget *parent)
  71.  
    : QMainWindow(parent)
  72.  
    , ui(new Ui::MainWindow)
  73.  
    {
  74.  
    ui->setupUi(this);
  75.  
    resize(800,600);
  76.  
    setWindowTitle("串口调试助手");
  77.  
    timcr=new QTimer(this);//new 一个定时器
  78.  
    timcr->start(500);//设置500时间发送一个信号
  79.  
    connect(timcr,&QTimer::timeout,this, &MainWindow::TimcrEvent);
  80.  
    serial=new QSerialPort(this);//创建一个串口
  81.  
    ui->comboBox_2->setCurrentIndex(5);
  82.  
    ui->comboBox_3->setCurrentIndex(3);
  83.  
    ui->comboBox_4->setCurrentIndex(2);
  84.  
    ui->comboBox_5->setCurrentIndex(0);
  85.  
    Receive_Byte=0;
  86.  
    Send_Byte=0;
  87.  
    connect(serial,&QSerialPort::readyRead,this,&MainWindow::serialPort_readyRead);
  88.  
    ui->checkBox->setCheckState(Qt::Checked);
  89.  
    }
  90.  
     
  91.  
    MainWindow::~MainWindow()
  92.  
    {
  93.  
    delete ui;
  94.  
    }
  95.  
     
  96.  
    void MainWindow::TimcrEvent()//检测当前电脑的槽函数
  97.  
    {
  98.  
    QStringList newPortStringList; //定义一个string容器
  99.  
    newPortStringList.clear();
  100.  
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())//foreach是qt定义的一个按照顺序遍历容器的循环
  101.  
    {
  102.  
    newPortStringList =info.portName();//info就是遍历的成员,它的portName()方法是转化为QString类型
  103.  
    }
  104.  
     
  105.  
    if(newPortStringList.size()!=portStringList.size())//判断是否有新的串口或者取出串口
  106.  
    {
  107.  
    portStringList=newPortStringList;
  108.  
    ui->comboBox->clear();
  109.  
    ui->comboBox->addItems( portStringList);
  110.  
    }
  111.  
    }
  112.  
     
  113.  
    void MainWindow::serialPort_readyRead()//当串口有消息来的时候促发的槽函数
  114.  
    {
  115.  
     
  116.  
    QString last_text;
  117.  
    int length;
  118.  
    int i;
  119.  
    if(ui->checkBox_3->checkState()!=Qt::Checked)
  120.  
    {
  121.  
    last_text=ui->textEdit->toPlainText();
  122.  
    Receivetext=serial->readAll();
  123.  
    Receive_Byte =Receivetext.length();
  124.  
    ui->label_9->setText(QString::number(Receive_Byte));
  125.  
    if(ui->checkBox_2->checkState()==Qt::Checked)//十六进制
  126.  
    {
  127.  
    Receivetext=Receivetext.toLatin1().toHex();
  128.  
    length=Receivetext.length();
  129.  
    for(i=0;i<=length/2;i )
  130.  
    {
  131.  
    Receivetext.insert(2 3*i,' ');
  132.  
    }
  133.  
     
  134.  
    }
  135.  
    else//ASCII
  136.  
    {
  137.  
    Receivetext=Receivetext.toLatin1();
  138.  
    }
  139.  
    last_text=last_text.append(Receivetext);
  140.  
    ui->textEdit->setText(last_text );
  141.  
     
  142.  
     
  143.  
    }
  144.  
     
  145.  
    }
  146.  
     
  147.  
    void MainWindow::on_pushButton_clicked()//打开串口
  148.  
    {
  149.  
     
  150.  
    if(ui->pushButton->text()==QString("打开串口"))
  151.  
    {
  152.  
    serial->setPortName(ui->comboBox->currentText());//设置串口名字
  153.  
    serial->setBaudRate(ui->comboBox_2->currentText().toInt());//设置波特率
  154.  
    switch (ui->comboBox_3->currentText().toInt())//设置数据位
  155.  
    {
  156.  
    case 5:serial->setDataBits(QSerialPort::Data5);break;
  157.  
    case 6:serial->setDataBits(QSerialPort::Data6);break;
  158.  
    case 7:serial->setDataBits(QSerialPort::Data7);break;
  159.  
    case 8:serial->setDataBits(QSerialPort::Data8);break;
  160.  
    default:serial->setDataBits(QSerialPort::UnknownDataBits);
  161.  
    }
  162.  
    switch (ui->comboBox_4->currentIndex())//设置校验位
  163.  
    {
  164.  
    case 0: break;//偶校验
  165.  
    case 1:serial->setParity(QSerialPort::OddParity);break;//奇校验
  166.  
    case 2:serial->setParity(QSerialPort::NoParity);break;//无校验
  167.  
    default:serial->setParity (QSerialPort::UnknownParity);break;
  168.  
    }
  169.  
    switch (ui->comboBox_5->currentIndex())//停止位
  170.  
    {
  171.  
    case 0:serial->setStopBits(QSerialPort::OneStop);break;//1
  172.  
    case 1:serial->setStopBits(QSerialPort::OneStop);break;//1.2
  173.  
    case 2:serial->setStopBits(QSerialPort::OneStop);break;//2
  174.  
    default:serial->setStopBits(QSerialPort::UnknownStopBits);
  175.  
     
  176.  
    }
  177.  
     
  178.  
    serial->setFlowControl(QSerialPort::NoFlowControl);//设置流控
  179.  
    if(!serial->open(QIODevice::ReadWrite))
  180.  
    {
  181.  
    QMessageBox::information(this,"错误提示","无法打开串口",QMessageBox::Ok);
  182.  
    return;
  183.  
    }
  184.  
    ui->comboBox->setEnabled(false);
  185.  
    ui->comboBox_2->setEnabled(false);
  186.  
    ui->comboBox_3->setEnabled(false);
  187.  
    ui->comboBox_4->setEnabled(false);
  188.  
    ui->comboBox_5->setEnabled(false);
  189.  
    ui->pushButton->setText("关闭串口");
  190.  
    }
  191.  
    else
  192.  
    {
  193.  
    serial->close();
  194.  
     
  195.  
    ui->comboBox->setEnabled(true);
  196.  
    ui->comboBox_2->setEnabled(true);
  197.  
    ui->comboBox_3->setEnabled(true);
  198.  
    ui->comboBox_4->setEnabled(true);
  199.  
    ui->comboBox_5->setEnabled(true);
  200.  
    ui->pushButton->setText("打开串口");
  201.  
    }
  202.  
    }
  203.  
     
  204.  
    void MainWindow::on_checkBox_clicked()//接收格式ASSCII点击后转到槽
  205.  
    {
  206.  
    ui->checkBox->setCheckState(Qt::Checked);
  207.  
    ui->checkBox_2->setCheckState(Qt::Unchecked);
  208.  
    ui->checkBox_3->setCheckState(Qt::Unchecked);
  209.  
     
  210.  
    }
  211.  
     
  212.  
    void MainWindow::on_checkBox_2_clicked()//十六进制点击后转到槽
  213.  
    {
  214.  
    ui->checkBox->setCheckState(Qt::Unchecked);
  215.  
    ui->checkBox_2->setCheckState(Qt::Checked);
  216.  
    ui->checkBox_3->setCheckState(Qt::Unchecked);
  217.  
    }
  218.  
     
  219.  
    void MainWindow::on_checkBox_3_clicked()//暂停转到槽
  220.  
    {
  221.  
    ui->checkBox->setCheckState(Qt::Unchecked);
  222.  
    ui->checkBox_2->setCheckState(Qt::Unchecked);
  223.  
    ui->checkBox_3->setCheckState(Qt::Checked);
  224.  
    }
  225.  
     
  226.  
    void MainWindow::on_pushButton_2_clicked()//串口通信发送信息
  227.  
    {
  228.  
    QByteArray bytearray;
  229.  
    Sendtext=ui->textEdit_2->toPlainText();
  230.  
    bytearray=Sendtext.toLatin1();
  231.  
    serial->write(bytearray);
  232.  
    Receive_Byte =Sendtext.length();
  233.  
    ui->label_11->setText(QString::number(Receive_Byte));
  234.  
    }
  235.  
     
  236.  
    void MainWindow::on_pushButton_3_clicked()//清空接收区
  237.  
    {
  238.  
    ui->textEdit->clear();
  239.  
    }
  240.  
     
  241.  
    void MainWindow::on_pushButton_4_clicked()//清空发送区
  242.  
    {
  243.  
    ui->textEdit_2->clear();
  244.  
    }
学新通

ui布局:学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfifhkf
系列文章
更多 icon
同类精品
更多 icon
继续加载