Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Tips to make QSerialPort task more optimal
Forum Updated to NodeBB v4.3 + New Features

Tips to make QSerialPort task more optimal

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 270 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Do_Imag
    wrote last edited by Do_Imag
    #1

    Hey,
    I am using QSearialPort to receive data from my embedded system at 1516800 baud rate, buffer size is 1936 bytes. Buffer is sent every 30 ms, (Sending whole buffer takes about 12 ms). Everything works with this set up, but my end result should be reading 10 channels, so I need to read 10 COMPORTS. I created a new thread for every serial port reading, so now I have 10 threads just for reading data and emitting signal to a parse slot, every thread signals same parser, because I need to log received data in a single .txt file. Inside each thread, read function waits for correct buffer length to be received: if less is received, it stores data in static buffer, if too much was received, it drops it and emits error GUI indication. On the embedded side, I have FTDI chips going to 10 ports of USB 3.0 hub with external power supply and this hub is connected to USB SS port. Everything seems to work correctly, I get no CRC errors, I receive data that I send, but sometimes, some tests tend to get a lot more those too big buffer errors, then others. I do these receive and log tests for 30 s and some work without errors, some get up to 70 errors on various channels. Maybe you can give me some tips to optimize my algorythm, so I would always receive correct amount? As I understood, QSerialPort doesn't just receive whole data, it receives it in chunks because of OS, my job is to buffer this data until correct amount is received, but as I said, sometimes it works flawless, but sometimes it just receives random lengths which causes errors. Everything is constant, because all devices are synced (with +/- 1% offset in their systick), so same buffe size, same baud rate, same send time.
    Here is read code:

    void serialThread::serial_readData()
    {
        qsizetype    data_size       = 0;
        rs_485_tx_packet_t          received_data = {0};
    
        //get data
        data.append(serial->readAll());
    
        if (data.size() < sizeof(rs_485_tx_packet_t))
        {
            // qDebug() << "too short packed received: " << data.size();
    
            return;
        }
        else if (data.size() > sizeof(rs_485_tx_packet_t))
        {
            //save data size and pass it to debug
            data_size = data.size();
    
            data.clear();
    
            //call ui function
            emit signal_fault(data_size);
    
            return;
        }
    
        QByteArray packet = data.first(sizeof(rs_485_tx_packet_t));
    
        emit signal_dataToParse(packet, serial_channel_number);
    
        data.clear();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Rather than basing your logic on the data size, you should have a protocol that allows you to know when you have a full frame of data so you only drop stuff when you have an "end of frame" without a matching "start of frame".

      Out of curiosity, why are you using a static buffer ? Or maybe, what do you mean by static buffer ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote last edited by
        #3

        I wonder if OP has done the math to determine the aggregate bandwidth needed to do "serial uart concentrator" over ten high speed UARTs and has made sure their equipment is beefy enough. I mean I don't care if they are multiplexing over a USB channel. UARTS are still an interrupt-per-char device. If anything, a USB wrapper adds more overhead. and flow control? what's that?

        I light my way forward with the fires of all the bridges I've burned behind me.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved