Qt World Summit: Register Today!

QSerialPort reading using QByteArray

  • Hello guys!

    I have a question, in QByteArray docs i can see info that:

    QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a '\0' terminator (...)

    So when i read loooong data from serial port

    QSerialPort *serial = new QSerialPort(this);
    // and set parameters here ...
    QByteArray response;
    while(response.size() < MAX_SIZE)
       if(serial->waitForReadReady(TIMEOUT)) // lets say TIMOEUT =500

    So does the QT appends '\0' terminator after every append? (from QByteArray::append() docs i cant figure out if it is true or not)

    {0x01,0x02,0x03, 0x04,0x05}
    processed like:
    {0x01,0x02 <read>,0x03, 0x04,<read>, 0x05}
    {0x01,0x02,"/0", 0x03, 0x04,"/0",0x05,"/0"} (?)

  • Moderators

    It appends the zero character internally so it can be used with strings, but it is not included in the actual byte array (or its size). So what you read is what you get.

  • Qt Champions 2020

    So does the QT appends '\0' terminator after every append? (

    For QByteArray - No.