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. QByte Array for 16 Bytes
Forum Update on Tuesday, May 27th 2025

QByte Array for 16 Bytes

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 956 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.
  • N Offline
    N Offline
    nanamo
    wrote on last edited by
    #1

    Hello
    I am reading in a 16 Byte stream from a device.
    The first byte is always 0x18
    The second byte is always 0xFF
    And the last byte is always 0x00

    So I only need a QByteArray of that size (16 Byte)
    Does this look correct or is there a cleaner method of setting up my QByte Array?

    const unsigned char str[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    QByteArray oxy_data(reinterpret_cast<const char*>(&str[0]), std::extent<decltype(str)>::value);
    
    	oxy_data = serPort->readAll();
    	qDebug("myVariable : %u ", oxy_data);
    
    J.HilkJ 1 Reply Last reply
    0
    • N nanamo

      @JonB Oh OK thanks Jon. I understand now. Is there a way to ensure that the QByteArray that is read from the serial port is 16 Bytes long only?

      As I need to be able to extract individual bytes from the message.

      For example byte 15 is the checksum (8 bit 2s compliment of the sum of bytes 1-14)
      So I would like a 16 byte message to work with

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #13

      @nanamo
      Stop concentrating on demanding that some QByteArray (you create) be 16 bytes long and instead concentrate on receiving the actual 16 bytes.

      Furthermore, you should not rely on any individual call to readAll() returning exactly 16 bytes. The way Qt/asynchronous operation works is that each call to readAll() could return either the first/next n bytes less than 16, or even that (theoretically) it could return more than the 16 bytes you want if (somehow) a further "message" was sent after the 16 bytes one. This is true even if you know the device sends 16 bytes: Qt does not guarantee that all 16 bytes will be received in a call to readAll().

      There are a couple of algorithms possible for this. You could buffer the incoming bytes until they reach 16, and then process the first 16, leaving any remainder for future processing. For your purposes I think you could use the following simpler code:

      signals:
      void  signal16received(const QByteArray &ba);
      
      if (serPort->bytesAvailable() >= 16)
      {
          QByteArray ba = serPort->read(16);
          Q_ASSERT(ba.length() == 16);
          emit signal16received(ba);
      }
      

      and attach slot to this signal to process the 16 bytes.

      NOTE
      @Bonnie's post has crossed with mine. I do not agree with his code [on this occasion, I usually do!], which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

      N B 3 Replies Last reply
      3
      • N nanamo

        Hello
        I am reading in a 16 Byte stream from a device.
        The first byte is always 0x18
        The second byte is always 0xFF
        And the last byte is always 0x00

        So I only need a QByteArray of that size (16 Byte)
        Does this look correct or is there a cleaner method of setting up my QByte Array?

        const unsigned char str[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        QByteArray oxy_data(reinterpret_cast<const char*>(&str[0]), std::extent<decltype(str)>::value);
        
        	oxy_data = serPort->readAll();
        	qDebug("myVariable : %u ", oxy_data);
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @nanamo QIODevice::readAll returns a QByteArray .... so why are you doing this ridiculous setup beforehand ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        N 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @nanamo QIODevice::readAll returns a QByteArray .... so why are you doing this ridiculous setup beforehand ?

          N Offline
          N Offline
          nanamo
          wrote on last edited by
          #3

          @J-Hilk Because I only want to store one response i.e. One 16 byte value in the array so I was setting it up of size - 16 Bytes

          JonBJ J.HilkJ 3 Replies Last reply
          0
          • N nanamo

            @J-Hilk Because I only want to store one response i.e. One 16 byte value in the array so I was setting it up of size - 16 Bytes

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #4

            @nanamo
            We don't know what you mean. Your oxy_data content is discarded. readAll() returns a brand new QByteArray.

            1 Reply Last reply
            0
            • N nanamo

              @J-Hilk Because I only want to store one response i.e. One 16 byte value in the array so I was setting it up of size - 16 Bytes

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @nanamo well ok, but this oxy_data = serPort->readAll(); completely overrides your previous QByteArray.

              I wouldn't even be surprised if the compiler optimised these two lines away

              const unsigned char str[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
              QByteArray oxy_data(reinterpret_cast<const char*>(&str[0]), std::extent<decltype(str)>::value);
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • N nanamo

                @J-Hilk Because I only want to store one response i.e. One 16 byte value in the array so I was setting it up of size - 16 Bytes

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #6

                @nanamo
                BTW, you could initialse a QByteArray to 16 bytes of 0 via QByteArray(16, '\0'). But that is not relevant to your current code per the explanation earlier.

                N 1 Reply Last reply
                0
                • JonBJ JonB

                  @nanamo
                  BTW, you could initialse a QByteArray to 16 bytes of 0 via QByteArray(16, '\0'). But that is not relevant to your current code per the explanation earlier.

                  N Offline
                  N Offline
                  nanamo
                  wrote on last edited by
                  #7

                  @JonB Yes I am just creating an array and initializing it to all zeroes.
                  Then it will be overwritten from reading from the serial port, that is fine.

                  Your method here looks better to create a QByteArray of 16 bytes.
                  Once it is full I just want to read it and process.
                  Then it can be overwritten again and I can reread.
                  Was just wondering the best way to create a 16 byte QByteArray

                  J.HilkJ JonBJ 2 Replies Last reply
                  0
                  • N nanamo

                    @JonB Yes I am just creating an array and initializing it to all zeroes.
                    Then it will be overwritten from reading from the serial port, that is fine.

                    Your method here looks better to create a QByteArray of 16 bytes.
                    Once it is full I just want to read it and process.
                    Then it can be overwritten again and I can reread.
                    Was just wondering the best way to create a 16 byte QByteArray

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #8

                    @nanamo
                    great please mark the topic as solved, thank you.

                    Can't wait for your next topic in 2 weeks:
                    "My QByteArray is longer/shorter than 16 Byte and I don't know why"


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    N 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @nanamo
                      great please mark the topic as solved, thank you.

                      Can't wait for your next topic in 2 weeks:
                      "My QByteArray is longer/shorter than 16 Byte and I don't know why"

                      N Offline
                      N Offline
                      nanamo
                      wrote on last edited by
                      #9

                      @J-Hilk Fantastic help, thanks a lot

                      1 Reply Last reply
                      1
                      • N nanamo

                        @JonB Yes I am just creating an array and initializing it to all zeroes.
                        Then it will be overwritten from reading from the serial port, that is fine.

                        Your method here looks better to create a QByteArray of 16 bytes.
                        Once it is full I just want to read it and process.
                        Then it can be overwritten again and I can reread.
                        Was just wondering the best way to create a 16 byte QByteArray

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @nanamo said in QByte Array for 16 Bytes:

                        Your method here looks better to create a QByteArray of 16 bytes.
                        Once it is full I just want to read it and process.
                        Then it can be overwritten again and I can reread.

                        You do not seem to have understood that your QByteArray is not being reused or overwritten. In Qt that rarely happens, instead a new QByteArray is returned.

                        N 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @nanamo said in QByte Array for 16 Bytes:

                          Your method here looks better to create a QByteArray of 16 bytes.
                          Once it is full I just want to read it and process.
                          Then it can be overwritten again and I can reread.

                          You do not seem to have understood that your QByteArray is not being reused or overwritten. In Qt that rarely happens, instead a new QByteArray is returned.

                          N Offline
                          N Offline
                          nanamo
                          wrote on last edited by
                          #11

                          @JonB Oh OK thanks Jon. I understand now. Is there a way to ensure that the QByteArray that is read from the serial port is 16 Bytes long only?

                          As I need to be able to extract individual bytes from the message.

                          For example byte 15 is the checksum (8 bit 2s compliment of the sum of bytes 1-14)
                          So I would like a 16 byte message to work with

                          B JonBJ 2 Replies Last reply
                          0
                          • N nanamo

                            @JonB Oh OK thanks Jon. I understand now. Is there a way to ensure that the QByteArray that is read from the serial port is 16 Bytes long only?

                            As I need to be able to extract individual bytes from the message.

                            For example byte 15 is the checksum (8 bit 2s compliment of the sum of bytes 1-14)
                            So I would like a 16 byte message to work with

                            B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by
                            #12

                            @nanamo

                            QByteArray oxy_data(16, 0x00);
                            qint64 bytesRead = serPort->read(oxy_data.data(), 16);
                            

                            or

                            QByteArray oxy_data =  serPort->read(16);
                            if(oxy_data.size() < 16)
                                oxy_data.append(16 - oxy_data.size(), 0x00);
                            
                            1 Reply Last reply
                            2
                            • N nanamo

                              @JonB Oh OK thanks Jon. I understand now. Is there a way to ensure that the QByteArray that is read from the serial port is 16 Bytes long only?

                              As I need to be able to extract individual bytes from the message.

                              For example byte 15 is the checksum (8 bit 2s compliment of the sum of bytes 1-14)
                              So I would like a 16 byte message to work with

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #13

                              @nanamo
                              Stop concentrating on demanding that some QByteArray (you create) be 16 bytes long and instead concentrate on receiving the actual 16 bytes.

                              Furthermore, you should not rely on any individual call to readAll() returning exactly 16 bytes. The way Qt/asynchronous operation works is that each call to readAll() could return either the first/next n bytes less than 16, or even that (theoretically) it could return more than the 16 bytes you want if (somehow) a further "message" was sent after the 16 bytes one. This is true even if you know the device sends 16 bytes: Qt does not guarantee that all 16 bytes will be received in a call to readAll().

                              There are a couple of algorithms possible for this. You could buffer the incoming bytes until they reach 16, and then process the first 16, leaving any remainder for future processing. For your purposes I think you could use the following simpler code:

                              signals:
                              void  signal16received(const QByteArray &ba);
                              
                              if (serPort->bytesAvailable() >= 16)
                              {
                                  QByteArray ba = serPort->read(16);
                                  Q_ASSERT(ba.length() == 16);
                                  emit signal16received(ba);
                              }
                              

                              and attach slot to this signal to process the 16 bytes.

                              NOTE
                              @Bonnie's post has crossed with mine. I do not agree with his code [on this occasion, I usually do!], which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

                              N B 3 Replies Last reply
                              3
                              • JonBJ JonB

                                @nanamo
                                Stop concentrating on demanding that some QByteArray (you create) be 16 bytes long and instead concentrate on receiving the actual 16 bytes.

                                Furthermore, you should not rely on any individual call to readAll() returning exactly 16 bytes. The way Qt/asynchronous operation works is that each call to readAll() could return either the first/next n bytes less than 16, or even that (theoretically) it could return more than the 16 bytes you want if (somehow) a further "message" was sent after the 16 bytes one. This is true even if you know the device sends 16 bytes: Qt does not guarantee that all 16 bytes will be received in a call to readAll().

                                There are a couple of algorithms possible for this. You could buffer the incoming bytes until they reach 16, and then process the first 16, leaving any remainder for future processing. For your purposes I think you could use the following simpler code:

                                signals:
                                void  signal16received(const QByteArray &ba);
                                
                                if (serPort->bytesAvailable() >= 16)
                                {
                                    QByteArray ba = serPort->read(16);
                                    Q_ASSERT(ba.length() == 16);
                                    emit signal16received(ba);
                                }
                                

                                and attach slot to this signal to process the 16 bytes.

                                NOTE
                                @Bonnie's post has crossed with mine. I do not agree with his code [on this occasion, I usually do!], which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

                                N Offline
                                N Offline
                                nanamo
                                wrote on last edited by
                                #14

                                @JonB OK thanks a lot Jon for your input. I will try

                                1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @nanamo
                                  Stop concentrating on demanding that some QByteArray (you create) be 16 bytes long and instead concentrate on receiving the actual 16 bytes.

                                  Furthermore, you should not rely on any individual call to readAll() returning exactly 16 bytes. The way Qt/asynchronous operation works is that each call to readAll() could return either the first/next n bytes less than 16, or even that (theoretically) it could return more than the 16 bytes you want if (somehow) a further "message" was sent after the 16 bytes one. This is true even if you know the device sends 16 bytes: Qt does not guarantee that all 16 bytes will be received in a call to readAll().

                                  There are a couple of algorithms possible for this. You could buffer the incoming bytes until they reach 16, and then process the first 16, leaving any remainder for future processing. For your purposes I think you could use the following simpler code:

                                  signals:
                                  void  signal16received(const QByteArray &ba);
                                  
                                  if (serPort->bytesAvailable() >= 16)
                                  {
                                      QByteArray ba = serPort->read(16);
                                      Q_ASSERT(ba.length() == 16);
                                      emit signal16received(ba);
                                  }
                                  

                                  and attach slot to this signal to process the 16 bytes.

                                  NOTE
                                  @Bonnie's post has crossed with mine. I do not agree with his code [on this occasion, I usually do!], which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

                                  B Offline
                                  B Offline
                                  Bonnie
                                  wrote on last edited by
                                  #15

                                  @JonB said in QByte Array for 16 Bytes:

                                  @Bonnie's post has crossed with mine. I do not agree with his code, which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

                                  Sure, I haven't considered that much. :)
                                  What I want is just tell OP how to use QByteArray and read().

                                  1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @nanamo
                                    Stop concentrating on demanding that some QByteArray (you create) be 16 bytes long and instead concentrate on receiving the actual 16 bytes.

                                    Furthermore, you should not rely on any individual call to readAll() returning exactly 16 bytes. The way Qt/asynchronous operation works is that each call to readAll() could return either the first/next n bytes less than 16, or even that (theoretically) it could return more than the 16 bytes you want if (somehow) a further "message" was sent after the 16 bytes one. This is true even if you know the device sends 16 bytes: Qt does not guarantee that all 16 bytes will be received in a call to readAll().

                                    There are a couple of algorithms possible for this. You could buffer the incoming bytes until they reach 16, and then process the first 16, leaving any remainder for future processing. For your purposes I think you could use the following simpler code:

                                    signals:
                                    void  signal16received(const QByteArray &ba);
                                    
                                    if (serPort->bytesAvailable() >= 16)
                                    {
                                        QByteArray ba = serPort->read(16);
                                        Q_ASSERT(ba.length() == 16);
                                        emit signal16received(ba);
                                    }
                                    

                                    and attach slot to this signal to process the 16 bytes.

                                    NOTE
                                    @Bonnie's post has crossed with mine. I do not agree with his code [on this occasion, I usually do!], which relies on read(16) finding all 16 bytes in one call. It will not work correctly if a "packet" of less than 16 bytes arrives, which Qt does not guarantee will not happen.

                                    N Offline
                                    N Offline
                                    nanamo
                                    wrote on last edited by
                                    #16

                                    @JonB Thanks Jon, this works well. You're a genius

                                    1 Reply Last reply
                                    0
                                    • N nanamo has marked this topic as solved on

                                    • Login

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