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. Serial port buffer Size
Forum Update on Monday, May 27th 2025

Serial port buffer Size

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 2.3k Views
  • 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.
  • I Offline
    I Offline
    isan
    wrote on 23 Dec 2021, 10:46 last edited by
    #1

    I open the serial port in ReadWrite mode. First I have to send a command to a device to receive the data.
    The packet size sent from the device is 10240 but in serial-> readAll() I only get 4096 bytes
    I also use serial-> setReadBufferSize (10240) But nothing changes. Is there a limit to the size of the serial port buffer?

    RandomWalk::RandomWalk(RandomWalk::DataHandler *handler, QString port,
                           QString Config, QString model, void *param)
        : stopThread(false), command(false), handler(handler), param(param),
          recievedData{0} {
      serial = new QSerialPort(this);
      connect(serial, &QSerialPort::readyRead, this, &RandomWalk::readData);
      openSerialPort(port, QIODevice::ReadWrite);
      writeData(Config.toUtf8());
    }
    
    void RandomWalk::openSerialPort(QString port, QIODevice::OpenModeFlag device) {
      serial->setPortName(port);
      serial->setBaudRate(115200);
      serial->setDataBits(QSerialPort::Data8);
      serial->setParity(QSerialPort::NoParity);
      serial->setStopBits(QSerialPort::OneStop);
      serial->setFlowControl(QSerialPort::NoFlowControl);
      serial->setReadBufferSize(10240);
     // serial->waitForReadyRead(2000);
      if (serial->open(device)) {
    
        qDebug() << "Connected";
    
      } else {
        qDebug() << "Open error";
      }
    }
    
    void RandomWalk::writeData(const QByteArray &data) { serial->write(data); }
    
    void RandomWalk::readData() {
      while (serial->bytesAvailable()) {
      recievedData.append(serial->readAll());
      }
      qDebug() << "recieveddata" << recievedData.size();
    
    }
    
    J 1 Reply Last reply 23 Dec 2021, 10:58
    0
    • I isan
      23 Dec 2021, 10:46

      I open the serial port in ReadWrite mode. First I have to send a command to a device to receive the data.
      The packet size sent from the device is 10240 but in serial-> readAll() I only get 4096 bytes
      I also use serial-> setReadBufferSize (10240) But nothing changes. Is there a limit to the size of the serial port buffer?

      RandomWalk::RandomWalk(RandomWalk::DataHandler *handler, QString port,
                             QString Config, QString model, void *param)
          : stopThread(false), command(false), handler(handler), param(param),
            recievedData{0} {
        serial = new QSerialPort(this);
        connect(serial, &QSerialPort::readyRead, this, &RandomWalk::readData);
        openSerialPort(port, QIODevice::ReadWrite);
        writeData(Config.toUtf8());
      }
      
      void RandomWalk::openSerialPort(QString port, QIODevice::OpenModeFlag device) {
        serial->setPortName(port);
        serial->setBaudRate(115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setReadBufferSize(10240);
       // serial->waitForReadyRead(2000);
        if (serial->open(device)) {
      
          qDebug() << "Connected";
      
        } else {
          qDebug() << "Open error";
        }
      }
      
      void RandomWalk::writeData(const QByteArray &data) { serial->write(data); }
      
      void RandomWalk::readData() {
        while (serial->bytesAvailable()) {
        recievedData.append(serial->readAll());
        }
        qDebug() << "recieveddata" << recievedData.size();
      
      }
      
      J Offline
      J Offline
      JonB
      wrote on 23 Dec 2021, 10:58 last edited by JonB
      #2

      @isan
      You should not assume there is any relationship between how many bytes readAll() returns and the size of any buffer. readAll()/bytesAvailable() return whatever they currently "see" as there. Repeated calls will later pick up any further data. I do not think you can influence the minimum/maximum number of bytes it returns in any one call.

      Since you are (correctly) appending bytes received to a member buffer use that to accumulate whatever you want. If you need to wait for > 4096 bytes for some reason that is the place to do it.

      In fact reading QSerialPort::setReadBufferSize() I think you might be better leaving it at the default of 0 which means

      If the buffer size is limited to a certain size, QSerialPort will not buffer more than this size of data. The special case of a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.

      So it sounds like specifying a buffer size actually places a maximum bytes limit, risking dropping of data if you do not read it fast enough.

      1 Reply Last reply
      5
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 23 Dec 2021, 11:04 last edited by
        #3

        Hi
        Depending on the SENDING hardware, the payload will come in one or more
        "waves"/readAll()

        So once recievedData.size() is the expected size, all data has been read.

        There is no setting to avoid this. Its how it works.

        1 Reply Last reply
        4

        1/3

        23 Dec 2021, 10:46

        • Login

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