Results 1 to 6 of 6

Thread: Assigning quint16_t to QByteArray while sending serial data frame from PC to device

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Assigning quint16_t to QByteArray while sending serial data frame from PC to devi

    I have twice edited your posts in this thread to add CODE tags to your source code listings. Please use them for your future posts. See my signature below if you don't know how.

    My question is How can I make sure irrespective of value in the "len" and "checksum", I want to allocate 2bytes for each fields in the serial frame.
    I told you in my previous answer: Do not append "sizeof( hdr )", append a quint16_t variable which contains the sizeof( hdr ) value. sizeof() will return the smallest representation of the number of bytes in the argument (in your case 4 for "hdr"), which fits into a single byte, so a single byte is what gets appended. If you want 2 bytes to be appended, then you have to append a 2 byte variable.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Assigning quint16_t to QByteArray while sending serial data frame from PC to devi

    There is no QByteArray function to append() anything other than chars (bytes).

    The sizeof(hdr) is a size_t with the value 4 (probably). size_t is variously defined, but must be at least 16-bits long and often an unsigned 32 or 64-bit integer.
    At line 27 you append a single char with the value stored in the size_t (it is an implied cast). One byte in the array is what you get. As a length value it will be broken if the hdr ever exceeds 255 bytes.

    If you want to write a multi-byte integer using only QByteArray then you need to code breaking it into bytes and appending them in the correct order (whichever way round that is). Something like:
    Qt Code:
    1. size_t hdrlen = sizeof(hdr);
    2. tx_msg->append(hdrlen & 0xff)
    3. hdrlen = hdrlen >> 8;
    4. tx_msg->append(hdrlen & 0xff)
    To copy to clipboard, switch view to plain text mode 
    or you can go this way (making the result platform architecture sensitive... your other code already is):
    Qt Code:
    1. uint16_t hdrlen = sizeof(hdr);
    2. tx_msg->append((char *) &hdrlen, sizeof(hdrlen));
    To copy to clipboard, switch view to plain text mode 

    You could also (carefully) use QDataStream.

  3. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (23rd January 2021)

  4. #3
    Join Date
    Jan 2021
    Posts
    20
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Assigning quint16_t to QByteArray while sending serial data frame from PC to devi

    Quote Originally Posted by ChrisW67 View Post
    There is no QByteArray function to append() anything other than chars (bytes).

    The sizeof(hdr) is a size_t with the value 4 (probably). size_t is variously defined, but must be at least 16-bits long and often an unsigned 32 or 64-bit integer.
    At line 27 you append a single char with the value stored in the size_t (it is an implied cast). One byte in the array is what you get. As a length value it will be broken if the hdr ever exceeds 255 bytes.

    If you want to write a multi-byte integer using only QByteArray then you need to code breaking it into bytes and appending them in the correct order (whichever way round that is). Something like:
    Qt Code:
    1. size_t hdrlen = sizeof(hdr);
    2. tx_msg->append(hdrlen & 0xff)
    3. hdrlen = hdrlen >> 8;
    4. tx_msg->append(hdrlen & 0xff)
    To copy to clipboard, switch view to plain text mode 
    or you can go this way (making the result platform architecture sensitive... your other code already is):
    Qt Code:
    1. uint16_t hdrlen = sizeof(hdr);
    2. tx_msg->append((char *) &hdrlen, sizeof(hdrlen));
    To copy to clipboard, switch view to plain text mode 

    You could also (carefully) use QDataStream.
    Thanks Chris. I could figure out the first approach mentioned above.

Similar Threads

  1. sending data in hex with QByteArray
    By QJak in forum Newbie
    Replies: 3
    Last Post: 17th April 2018, 16:31
  2. Sending serial data over ethernet
    By awpitt13 in forum Newbie
    Replies: 2
    Last Post: 27th February 2017, 13:06
  3. Problem sending data over to serial port
    By hovuquocan1997 in forum Qt Programming
    Replies: 6
    Last Post: 28th July 2015, 17:49
  4. How to interrupt serial port sending?
    By redkit_redkit in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2014, 12:58
  5. Replies: 1
    Last Post: 5th December 2013, 06:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.