QSerialPort - setReadBufferSize

I am currently having a problem. I am trying to write a realtime build program. I am receiving data from Arduino. I can successfully open the serial port and read the information and plot correctly. The problem is that if I don't specify the buffer size, the input buffer is considered infinite (now, reading data at 300Hz-4800Hz, you can imagine that your computer memory will probably replenish and everything works!).

Now I tried something like:

serial->setReadBufferSize(5000);

      

Now this sets the buffer size successfully, I used serial->readBufferSize()

to confirm if it works or not.

The problem is that the buffer is full, the program stops printing. Now I imagine what I need to do:

  • Set the buffer size (only once)
  • Read the serial port
  • Highlight the data
  • Clear buffer (serial-> clear ()).

Repeat steps 2 - 4.

But it doesn't work.

I am using QCustomPlot

to create a real time graph.

+3


source to share


2 answers


You can read data in an asynchronous way. Just connect the signal readyRead()

QSerialPort

to the slot. readyRead()

highlighted whenever new data is available:

connect(&serial, SIGNAL(readyRead()), this, SLOT(readData()));

      



readData()

is the slot that is called every time it QSerialPort

emits a signal readyRead()

. readData()

adds any available data to the class member QByteArray

. You can check this slot regardless of whether a specific amount of data is received:

void MyClass::readData()
{
    receivedData.append(serial.readAll());

    if(receivedData.count()>=5000)
    {
        //Plot data and remove plotted data from receivedData
    }
}

      

+2


source


Make sure your serial.waitForReadyRead is not too small.

serial.waitForReadyRead(50)

      



50ms works for me.

0


source







All Articles