//读串口 void MainWindow::readMyCom() { static unsigned char buffer[BUFFER_SIZE] = {0}; static unsigned char counter = 0; char ch; double _x1, _x2, _pwm; while(myCom->bytesAvailable()){ myCom->getChar(&ch); buffer[counter] = ch; if(buffer[0] != BUFFER_HEAD1 && counter == 0){ return; } counter++; if(counter == 2 && buffer[1] != BUFFER_HEAD2){ counter = 0; return; } if(counter == BUFFER_SIZE){ counter = 0; _x1 = (double)((buffer[2]<<24)|(buffer[3]<<16)|(buffer[4]<<8)|buffer[5])/1000.0f-180; _x2 = (double)((buffer[6]<<24)|(buffer[7]<<16)|(buffer[8]<<8)|buffer[9])/1000.0f-180; _pwm = (double)((buffer[10]<<24)|(buffer[11]<<16)|(buffer[12]<<8)|buffer[13])/1000.0f-100; plotUpdate(ui->customPlot, _x1, _x2, _pwm); } } }
void MainWindow::setupPlot() { plotTimer.setInterval(1000/settingsDialog->rate); connect(&plotTimer, SIGNAL(timeout()), this, SLOT(plotUpdate())); plotTimer.start(); if (!ui->plot->legend()) { QwtLegend* legend = new QwtLegend; legend->setItemMode(QwtLegend::CheckableItem); ui->plot->insertLegend(legend, QwtPlot::RightLegend); } ui->plot->setAxisTitle(QwtPlot::xBottom, "seconds"); ui->plot->setCanvasBackground(Qt::white); QColor gridColor; gridColor.setNamedColor("grey"); QPen gridPen(gridColor); gridPen.setStyle(Qt::DotLine); QwtPlotGrid* grid = new QwtPlotGrid; grid->setMajPen(gridPen); grid->attach(ui->plot); connect(ui->plot, SIGNAL(legendChecked(QwtPlotItem*,bool)), this, SLOT(showCurve(QwtPlotItem*,bool))); }