Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSerialPort - setReadBufferSize

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:

  1. Set the buffer size (only once)
  2. Read the serial port
  3. Plot the data
  4. Clear the buffer (serial->clear()).

Repeat steps 2 - 4.

But this doesn't seem to work.

I am making use of QCustomPlot to do real-time plotting.

like image 664
skandebaba Avatar asked Mar 24 '26 18:03

skandebaba


1 Answers

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
    }
}
like image 110
Nejat Avatar answered Mar 26 '26 06:03

Nejat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!