zoukankan      html  css  js  c++  java
  • QT5.5实现串口通信

    QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成

    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    这两个库,后续只需要调用函数,对串口名,读写方式,波特率,验证方式,数据位数,结束位进行相应设置,便可以连接相应串口进行通信。

    部分代码:
     if(ui->pushButton->text()==tr("打开串口")){
            my_serialport= new QSerialPort();
            my_serialport->setPortName(ui->comboBox->currentText());
            my_serialport->open(QIODevice::ReadWrite);
            my_serialport->setBaudRate(ui->comboBox_2->currentText().toInt());
            switch(ui->comboBox_3->currentIndex()){
            case 0: my_serialport->setParity(QSerialPort::NoParity);break;
            case 1: my_serialport->setParity(QSerialPort::OddParity);break;
            case 2: my_serialport->setParity(QSerialPort::EvenParity);break;
            default: break;
            }
            switch(ui->comboBox_4->currentIndex()){
            case 0: my_serialport->setDataBits(QSerialPort::Data8);break;
            case 1: my_serialport->setDataBits(QSerialPort::Data7);break;
            case 2: my_serialport->setDataBits(QSerialPort::Data6);break;
            default: break;
            }
            switch(ui->comboBox_5->currentIndex()){
            case 0: my_serialport->setStopBits(QSerialPort::OneStop);break;
            case 1: my_serialport->setStopBits(QSerialPort::TwoStop);break;
            default: break;
            }
            my_serialport->setFlowControl(QSerialPort::NoFlowControl);
            connect(my_serialport,SIGNAL(readyRead()),this,SLOT(my_readuart()));
            ui->comboBox->setEnabled(false);
            ui->comboBox_2->setEnabled(false);
            ui->comboBox_3->setEnabled(false);
            ui->comboBox_4->setEnabled(false);
            ui->comboBox_5->setEnabled(false);
            ui->label_6->setStyleSheet("background-color:red");
            ui->pushButton->setText(tr("关闭串口"));
            ui->pushButton_2->setEnabled(true);
        }
        else {
            my_serialport->clear();
            my_serialport->deleteLater();
            //  my_serialport->deleteLater();
            ui->comboBox->setEnabled(true);
            ui->comboBox_2->setEnabled(true);
            ui->comboBox_3->setEnabled(true);
            ui->comboBox_4->setEnabled(true);
            ui->comboBox_5->setEnabled(true);
         //   ui->label_6->setStyleSheet("background-color:none");
            ui->label_6->setStyleSheet("background-color:rgb(130,130,130)");
            ui->pushButton->setText(tr("打开串口"));
            ui->pushButton_2->setEnabled(false);
        }
    其中参考于csdn的yh_1988
    void serial::on_pushButton_2_clicked()
    {
        my_serialport->write(ui->lineEdit->text().toLatin1());
    }

    值得注意的是,在用于串口通信时,上述函数中只讲lineEdit中的text传递给串口,但是这样是缺少换行符/n的。如果是用在有uboot系统上通过串口进行交互时,应该修改为:

    void serial::on_pushButton_2_clicked()
    {
        my_serialport->write(ui->lineEdit->text().toLatin1());
      my_serialport->write("/n");
    }
    这样就可以正常的通信啦。

    系统启动过程打印

    输入?弹出帮助菜单

    最近用Qt5.9重新编辑了一个Demo,需要下载的同学可以在下面的网址下载

    https://github.com/dong930623/Qt/tree/master/SerialPortToolDemo

  • 相关阅读:
    成长型思维
    Spring Boot 入门详细分析
    我们为什么要学习 Spring Boot
    躲不掉的 lambda 表达式
    Java 并发工具包 | J.U.C
    Java 并发编程整体介绍 | 内含超多干货
    彻底搞懂单例模式如何安全的实现
    atomic 包、synchronized | Java 中线程安全
    AD在更新PCB的时候,每次封装都会改变位置?
    1206封装电容在物料可靠性设计比较低
  • 原文地址:https://www.cnblogs.com/-Donny/p/5039761.html
Copyright © 2011-2022 走看看