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

  • 相关阅读:
    Windows平台分布式架构实践
    压力测试的轻量级具体做法
    vs.net git版本仓库使用 之解决冲突方法 原创
    VS2015 Git 插件使用教程
    HTML页面禁止选择、页面禁止复制、页面禁止右键
    python标准库介绍——3 stat 模块详解
    python标准库介绍——2 os.path模块详解
    python标准库介绍——1 os详解
    Python 多进程 一分钟下载二百张图片 是什么样子的体验
    封ip对爬虫的影响
  • 原文地址:https://www.cnblogs.com/-Donny/p/5039761.html
Copyright © 2011-2022 走看看