I currently have an issue. I am trying to write a real-time plotting program. I receive data from an Arduino. I can successfully open the serial port and read the information properly and plot the graph as well. The problem is, if I don't specify a buffer size, the input buffer is assumed to be infinite (now reading data at 300Hz-4800Hz you can imagine your computer's memory will probably fill up eventually and everything crashes!).
Now I have tried something like:
serial->setReadBufferSize(5000);
Now this does successfully set the buffer size, I used serial->readBufferSize() to confirm whether it worked or not.
The problem is once the buffer is full, the program stops plotting. Now I imagine what I should do is:
Repeat steps 2 - 4.
But this doesn't seem to work.
I am making use of QCustomPlot to do real-time plotting.
You can read data in an asynchronous way. Just connect the readyRead() signal of QSerialPort to a slot. readyRead() is emitted whenever new data is available :
connect(&serial, SIGNAL(readyRead()), this, SLOT(readData()));
readData() is a slot that is called everytime QSerialPort emits the readyRead() signal. readData() appends any available data to a QByteArray class member. You can check in this slot whether a specific amount of data is received or not :
void MyClass::readData()
{
receivedData.append(serial.readAll());
if(receivedData.count()>=5000)
{
//Plot data and remove plotted data from receivedData
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With