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"} (?)

  • Qt Champions 2017

    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.

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

    For QByteArray - No.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.