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. QSerialPort reading using QByteArray
Forum Updated to NodeBB v4.3 + New Features

QSerialPort reading using QByteArray

Scheduled Pinned Locked Moved Unsolved General and Desktop
qserialportqbytearrayread
3 Posts 3 Posters 2.3k Views 1 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.
  • M Offline
    M Offline
    michelson
    wrote on last edited by
    #1

    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
          response.append(serial->readAll());
    }
    

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

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

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Hello,
      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #3

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

        For QByteArray - No.

        1 Reply Last reply
        1

        • Login

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