QCustomPlot Discussion and Comments

plot data from serial port in real-timeReturn to overview

Hi to everybody, someone can help me to solve this problem?
I need to make a real-time graph with a data that I receive from a sensor linked with a serial port to Qt.
I started from the tutorial "terminal example" of Qt.
In mainwindow.cpp I wrote this:


    m_plot->graph(0)->setPen(QPen(QColor(40, 110, 255)));   //blue line

    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
        m_plot->yAxis->setRange(0, 2000);    //the measurement of yAxis  must be in a range of (0,2000)mm

    connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData);

In this section, the xAxis not scroll with the second, maybe there is some kind of problem?
In readData();
void MainWindow::readData()
    const QByteArray data = m_serial->readAll();   //  I want to plot 'data' in the yAxis
  //  qDebug()<<data;                           
    inviaData(data);                               // This function should "clean" the value inside data.
}                                                            // ( I have this [i]("2000")[/i], but I want this [i]2000[/i])

so, how can I write inviaData(QByteArray &data) to clean 'data'? And how can I represent it on the yAxis?

Finally I resolve the first problem. Now I have a plot with an xAxis that scroll with time and an yAxis that receive a value ('data') but I still need to clean 'data' from ("2000\n") to 2000 to have a linear plot, how can I do it?

Try this:

QByteArray x("2000\n");
QByteArray y = x.mid(0, x.size()-1);
int result = y.toInt();
qDebug() << x << y << result;

Thank you Igor,
Now I have to solve another problem with the time of measurement of my device, but now result show me only the number.

"2000 \n"          // Application output of data. 
"2000 \n"

2000       //Application output of 'result' ( I changed it in a double value).

At the end of a problem, another begins, ahahah.

I surprised, but this work too:

QByteArray x("2000        \n");
int result = y.toInt()

Good luck! ;)

Ok, maybe I understand which is the problem.
In this situation:

void MainWindow::inviaData(const QByteArray &data)
  QStringList List;
        QString ran;

        if (data.contains("\n"))
           QString myString(charBuffer);
           List = myString.split(':');      // i'm not sure if it's correct. 
        }   [...]

qDebug shows me this: ("2000 \n")
so how can i take only the number of this QString?

Sadly I had to eliminate the code that you suggest me to use because it "translates" the error of the serial port comunication like:

00 \n"



Try this to read com port while symbol is not '\n':

void MainWindow::readData()
    while (com_port_->canReadLine())
        QByteArray data = com_port_->readLine();

Thank you Igor, it's working.

how did you plot both xaxis and yaxis please.. i'm blocked at this stage

void init_diagram()
    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);

void MainWindow::readData()
    QString line = serialPort->readLine().data();