QCustomPlot Discussion and Comments

Issues from Serialread to Qcustomplot displayReturn to overview

Hi ,
I have a project which needs to read adc at 115200 baudrate on serial and display it on Qcustomplot on QT. Qcustomplot is on main thread and serialread is on another thread. I am having hard time to catch up the incoming data to display. Iam also using a hdlc wrapper in other thread which extracts the data from serial read.
The code for serial side is briefly below:

connect(this->mySerialObj,&QSerialPort::readyRead,this,&SerialCommunication::readFromSerial);
QByteArray data = mySerialObj->readAll();
emit dataReceived(data);

HDLC wraps and sends the extracted data to Qcustomplot on main thread.
HDLC wrapper code:
/**
 * All data is passed to HDLC receiver.
 * Here we check the packet consistency, and pass on the data only.
 * @brief HDLC_qt::charReceiver
 * @param dataArray Received ch         unck of data of some length.
 * @return emit hdlcValidFrameReceived(data, length) When packet arrived.
 */
void HDLC_qt::charReceiver(QByteArray dataArray)
{
  //  static qint64 hdlcValidFrameReceivedcall = 0;
  //  QMutexLocker locker(&(SerialCommunication::m_Instance->mutexSerial));
 //  QMutexLocker locker(&mutexcharreceive);
 //   qDebug() << "hdlccharReceiverentry  is ";
//    for(int i =10;i<20;i++)
//    {
//         qDebug() << i ;
//    }
   // qDebug() << this->thread();
    char data = 0;
    for(QByteArray::iterator it = dataArray.begin(); it != dataArray.end(); it++) {
        data = (*it);
        /* Start flag or end flag */
        if(data == FRAME_BOUNDARY_OCTET)
        {
            if(escape_character == true)
            {
                escape_character = false;
            }
            /* Do CRC check if frame is valid */
            else if(  (frame_position >= 2)
                      &&(this->hdlcFrameCRC_check(frame_position)) )
            {
                /* Call user defined function to handle HDLC frame */
//                 qDebug() << "hdlcValidFrameReceivedcall  is ";
//                qDebug() << hdlcValidFrameReceivedcall++;
                receive_frame_buffer.chop(2);//Remove CRC data which is 2 bytes
                emit hdlcValidFrameReceived(receive_frame_buffer, static_cast<quint16>(frame_position-2));
            }
            /* Reset all for next frame */
            frame_position = 0;
            frame_checksum = 0;
            receive_frame_buffer.clear();
            continue;
        }

        if(escape_character)
        {
            escape_character = false;
            data ^= INVERT_OCTET;
        }
        else if(data == CONTROL_ESCAPE_OCTET)
        {
            escape_character = true;
            continue;
        }
        receive_frame_buffer[frame_position] = data;

        /* In Qt we don't calculate CRC here iteratively.
         * In Arduino, we needed to save RAM, but here small
         * buffer is no problem, we have more RAM.
         */

        frame_position++;

        /* If we don't ever receive valid frame,
         * buffer will keep growing bigger and bigger.
         * Hard coded max size limit and then reset
         */
        if(frame_position >= 2048)
        {
            receive_frame_buffer.clear();
            frame_position = 0;
            frame_checksum = 0;
        }
    }
  //  qDebug() << "hdlccharReceiverexit  is ";
   // qDebug() << this->thread();
}

I use blocking connection. SO GUI is smooth and plots the graph, however it slow in catching the incoming serial data.
At 115200baud, should the customplot be displaying the data atleast 115200/10 = 11520 bytes persecond. even with 7 bytes wrapper overhead(CRC etc) it should have atleast 11520/7 = 1645 bytes per second. But at the moment the customplot displays max 200bytes a second and when i stop sendingthe data from microcontroller, qcustomplot still takes quite a while to display all data filled in Qbyte array buffer.
Any suggestion on how to overcome this? THanks inadvance.

it is definitely good that you are using a different thread, but you shouldnt be using blocking queued, just queued.

I dont see this section of code, but you are probably drawing the graph every time you get new data... For every real time graph, i suggest that you update the data buffer constantly with new data on a queued signal and then only redraw every 200-1000ms.

Hi Ian,
Thank you so much for your inputs.
I am now updating the plot every 100ms and it is a lot better. And also removed blocking queued as suggested by you. It really helped. THanks a lot.
Another question is, what is the max uartrate i need to acheive highest sampling frequency ,may be atleast 2Khz sinewaveform?
Thank you