Qcustomplot生成串口波形

网上关于Qcustomplot的应用非常多,但是大多数讲的都不好,尤其是讲到串口+波形时大多数都是应用一个随机函数产生随机数,可移植性非常差,于是我重新写了一下关于Qcustomplot接收串口函数并实时显示波形,波形可随时间移动。先上代码,具体解析见后面:

 QString str1 ="FE 04 00 00 00 02 65 C4";
    QByteArray senddata;
    StringToHex(str1,senddata);//将str字符串转换为16进制的形式
    serial->write(senddata);
    serial->waitForReadyRead(1);

    QByteArray temp = serial->readAll();
    // QDataStream out(&temp1,QIODevice::ReadWrite);    //将字节数组读入
    QString data1=temp.toHex();

    QString data2=data1.mid(10,4);//字符串

    int t =data2.toInt(0,16) ;//转化成int(不带小数点)
    double ts=(double)t/100;
    double F =((double)t/100.0)*1.8+32.0;
    QString str = QString::number(ts,'f',2);
    QString strF = QString::number(F,'f',2);
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("hh:mm:ss");
  
    serial->waitForBytesWritten(1);
 

    /*写入曲线图*/

    static int graph_count = 0;
    static unsigned long count = 0;
    static unsigned long count1 = 0;
    QString data3=temp.toHex();

    QString data4=data3.mid(10,4);//字符串
    int num1 =data4.toInt(0,16) ;//转化成int(不带小数点)
  
    double num;
    num=(double) num1/100;

    if(temp.isEmpty() != true)
    {

        ui->curveShowWidget->xAxis->setTickStep(3);
        graph_count = ui->curveShowWidget->graphCount();

        ui->curveShowWidget->addGraph();
        ui->curveShowWidget->graph(0)->setPen(QColor(0,0,0,0));
        ui->curveShowWidget->addGraph();
        ui->curveShowWidget->graph(1)->setPen(QPen(Qt::red,2));

        ui->curveShowWidget->graph(1)->addData(ui->doubleSpinBox_3->value()*count++,num*1.8+32.0);

        ui->curveShowWidget->addGraph();

        if(ui->checkBox->isChecked()==true)
        {
            ui->curveShowWidget->yAxis->setRange(num-ui->doubleSpinBox->value(),num+ui->doubleSpinBox_2->value());
            ui->curveShowWidget->graph(1)->setPen(QColor(0,0,0,0));
            ui->curveShowWidget->graph(2)->setPen(QPen(Qt::blue,2));

        }
        else if(ui->checkBox->isChecked()!=true)
        {
            ui->curveShowWidget->yAxis->setRange(num*1.8+32-ui->doubleSpinBox->value(),num*1.8+32+ui->doubleSpinBox_2->value());
            ui->curveShowWidget->graph(2)->setPen(QColor(0,0,0,0));
            ui->curveShowWidget->graph(1)->setPen(QPen(Qt::red,2));
        }
        ui->curveShowWidget->yAxis->setTickStep(ui->doubleSpinBox_4->value());
        ui->curveShowWidget->graph(2)->addData(ui->doubleSpinBox_3->value()*count1++,num);
        ui->curveShowWidget->graph(2)->visible()==0;
        curveZoom_flag =1;
        if(curveZoom_flag == 1)//打开 自动缩放
        {
          
            ui->curveShowWidget->graph(1)->rescaleKeyAxis(true);
            
            ui->curveShowWidget->graph(2)->rescaleKeyAxis(true);
          
        }
        ui->curveShowWidget->replot();
        QApplication::processEvents();
    }
    else
    {
        temp=0;
        ui->curveShowWidget->addGraph();
        ui->curveShowWidget->graph(0)->setPen(QColor(0,0,0,0));
        ui->curveShowWidget->addGraph();
        ui->curveShowWidget->graph(1)->setPen(QPen(Qt::red,2));
        ui->curveShowWidget->graph(1)->addData(ui->doubleSpinBox_3->value()*count++,num*1.8+32.0);
        ui->curveShowWidget->addGraph();
    }

首先本代码通过串口发送一串16进制数 QString str1 =“FE 04 00 00 00 02 65 C4”;该数据发送之后,QByteArray temp = serial->readAll();工控机会返回数据temp,返回的数据中有波形图所需要的信息,所以需要解析,经过一系列解析之后,得到波形数据 num,然后按照下面的程序,即可完成波形的显示。
Qcustomplot生成串口波形

Qcustomplot生成串口波形Qcustomplot生成串口波形 包子同学 发布了3 篇原创文章 · 获赞 0 · 访问量 23 私信 关注
上一篇:Qt 图片适应QLabel控件大小(饱满缩放和按比例缩放)


下一篇:QT 绘图设备的总结