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. How to create a QByteArray to send in QSerialPort
QtWS25 Last Chance

How to create a QByteArray to send in QSerialPort

Scheduled Pinned Locked Moved Solved General and Desktop
qserialportqbyteastruct
7 Posts 3 Posters 2.6k 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.
  • D Offline
    D Offline
    Dooham
    wrote on 21 Mar 2019, 09:32 last edited by
    #1

    Hello,
    I have the following trouble: I want to send a message to a device that I have connected in a serial port. I can read the message that this device send me, but I can write a good QByteArray to send.

    void MySerialPort::CodificarMensaje()
    {
        mavlink_msg_heartbeat_pack(2,2,&msgSend,3,2,1,40,0);
        dataSend[0]=static_cast<char>(msgSend.magic);
        dataSend.append(static_cast<char>(msgSend.len));
        dataSend.append(static_cast<char>(msgSend.seq));
        dataSend.append(static_cast<char>(msgSend.sysid));
        dataSend.append(static_cast<char>(msgSend.compid));
        dataSend.append(static_cast<char>(msgSend.msgid));
    
        for (int i=0;i<msgSend.len;i++) {
            dataSend.append(static_cast<char>(msgSend.payload64[i]));
        }
        dataSend.append(static_cast<char>(msgSend.checksum>>8));
        dataSend.append(static_cast<char>(msgSend.checksum));
        qDebug()<<dataSend;
        qDebug()<<msgSend.len;
    
    
    }
    

    The first function creates a structure like this:

    typedef struct __mavlink_message {
    	uint16_t checksum; ///< sent at end of packet
    	uint8_t magic;   ///< protocol magic marker
    	uint8_t len;     ///< Length of payload
    	uint8_t seq;     ///< Sequence of packet
    	uint8_t sysid;   ///< ID of message sender system/aircraft
    	uint8_t compid;  ///< ID of the message sender component
    	uint8_t msgid;   ///< ID of message in payload
        uint8_t payload64[255];
        
    }) mavlink_message_t;
    
    

    This function is from a library (MavLink Library v1.0), and I think this function works well. However, my troubles starts when I try to write the QByteArray to send. I tried with the function append, and I got this QByteArray:

    0_1553160551408_ba7ea8c1-c4e7-45e3-af8d-4d0f178d6862-image.png

    This result is incorrect, because it has a few troubles:
    1-As you can see, in the posicion 1 (the second byte) has to be the len data (msgsend.len), however I got the result \t instead of \x09 that is the correct number (in the structure msgSend is correct).
    2-The checksum is a uint16_t and I the solution to pass from uint16 to 2 uint8 is not correct, so I dont get the correct result.

    Thanks you for your help

    J 1 Reply Last reply 21 Mar 2019, 09:40
    0
    • D Dooham
      21 Mar 2019, 09:32

      Hello,
      I have the following trouble: I want to send a message to a device that I have connected in a serial port. I can read the message that this device send me, but I can write a good QByteArray to send.

      void MySerialPort::CodificarMensaje()
      {
          mavlink_msg_heartbeat_pack(2,2,&msgSend,3,2,1,40,0);
          dataSend[0]=static_cast<char>(msgSend.magic);
          dataSend.append(static_cast<char>(msgSend.len));
          dataSend.append(static_cast<char>(msgSend.seq));
          dataSend.append(static_cast<char>(msgSend.sysid));
          dataSend.append(static_cast<char>(msgSend.compid));
          dataSend.append(static_cast<char>(msgSend.msgid));
      
          for (int i=0;i<msgSend.len;i++) {
              dataSend.append(static_cast<char>(msgSend.payload64[i]));
          }
          dataSend.append(static_cast<char>(msgSend.checksum>>8));
          dataSend.append(static_cast<char>(msgSend.checksum));
          qDebug()<<dataSend;
          qDebug()<<msgSend.len;
      
      
      }
      

      The first function creates a structure like this:

      typedef struct __mavlink_message {
      	uint16_t checksum; ///< sent at end of packet
      	uint8_t magic;   ///< protocol magic marker
      	uint8_t len;     ///< Length of payload
      	uint8_t seq;     ///< Sequence of packet
      	uint8_t sysid;   ///< ID of message sender system/aircraft
      	uint8_t compid;  ///< ID of the message sender component
      	uint8_t msgid;   ///< ID of message in payload
          uint8_t payload64[255];
          
      }) mavlink_message_t;
      
      

      This function is from a library (MavLink Library v1.0), and I think this function works well. However, my troubles starts when I try to write the QByteArray to send. I tried with the function append, and I got this QByteArray:

      0_1553160551408_ba7ea8c1-c4e7-45e3-af8d-4d0f178d6862-image.png

      This result is incorrect, because it has a few troubles:
      1-As you can see, in the posicion 1 (the second byte) has to be the len data (msgsend.len), however I got the result \t instead of \x09 that is the correct number (in the structure msgSend is correct).
      2-The checksum is a uint16_t and I the solution to pass from uint16 to 2 uint8 is not correct, so I dont get the correct result.

      Thanks you for your help

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 21 Mar 2019, 09:40 last edited by
      #2

      @Dooham Do you mean you want to put data from __mavlink_message into QByteArray?
      If so then take a look at https://doc.qt.io/qt-5/qdatastream.html and overload

      &QDataStream::operator<<(const &mavlink_message_t);
      

      then you can do

      QByteArray array;
      QDataStream stream(array);
      mavlink_message_t message;
      stream<<message;
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 2 Replies Last reply 21 Mar 2019, 10:37
      3
      • J jsulm
        21 Mar 2019, 09:40

        @Dooham Do you mean you want to put data from __mavlink_message into QByteArray?
        If so then take a look at https://doc.qt.io/qt-5/qdatastream.html and overload

        &QDataStream::operator<<(const &mavlink_message_t);
        

        then you can do

        QByteArray array;
        QDataStream stream(array);
        mavlink_message_t message;
        stream<<message;
        
        D Offline
        D Offline
        Dooham
        wrote on 21 Mar 2019, 10:37 last edited by
        #3

        @jsulm That is what i want, but with the DataStream I'm still having the same issue:
        0_1553164568193_8a333b0e-ae0f-413d-bbcf-ba6f5e198d73-image.png
        The lenght is still a \t instead of a \x09.

        QDataStream stream(&dataSend, QIODevice::ReadWrite);
            stream<<msgSend.magic;
            stream<<msgSend.len;
            stream<<msgSend.seq;
            stream<<msgSend.sysid;
            stream<<msgSend.compid;
            stream<<msgSend.msgid;
            for (int i=0;i<msgSend.len;i++) {
                stream<<msgSend.payload64[i];
            }
        
        
            qDebug()<<dataSend;
        
        

        I didnt include the checksum.

        J 1 Reply Last reply 21 Mar 2019, 10:56
        0
        • D Dooham
          21 Mar 2019, 10:37

          @jsulm That is what i want, but with the DataStream I'm still having the same issue:
          0_1553164568193_8a333b0e-ae0f-413d-bbcf-ba6f5e198d73-image.png
          The lenght is still a \t instead of a \x09.

          QDataStream stream(&dataSend, QIODevice::ReadWrite);
              stream<<msgSend.magic;
              stream<<msgSend.len;
              stream<<msgSend.seq;
              stream<<msgSend.sysid;
              stream<<msgSend.compid;
              stream<<msgSend.msgid;
              for (int i=0;i<msgSend.len;i++) {
                  stream<<msgSend.payload64[i];
              }
          
          
              qDebug()<<dataSend;
          
          

          I didnt include the checksum.

          J Offline
          J Offline
          JonB
          wrote on 21 Mar 2019, 10:56 last edited by JonB
          #4

          @Dooham said in How to create a QByteArray to send in QSerialPort:

          The lenght is still a \t instead of a \x09.

          No, people keep stating this! Bytes are bytes in a QByteArray, period. And '\t' == '\x09'. The problem is that people keep looking at how qDebug() << QByteArray (or debugger) happens to display values, which it tries to display "as though" it were text to "look nice" to you humans :). Use https://doc.qt.io/Qt-5/qbytearray.html#toHex (for qDebug()) instead if you find that clearer....

          D 1 Reply Last reply 21 Mar 2019, 11:07
          5
          • J jsulm
            21 Mar 2019, 09:40

            @Dooham Do you mean you want to put data from __mavlink_message into QByteArray?
            If so then take a look at https://doc.qt.io/qt-5/qdatastream.html and overload

            &QDataStream::operator<<(const &mavlink_message_t);
            

            then you can do

            QByteArray array;
            QDataStream stream(array);
            mavlink_message_t message;
            stream<<message;
            
            D Offline
            D Offline
            Dooham
            wrote on 21 Mar 2019, 11:06 last edited by
            #5

            @jsulm Sorry, you were right, with this method I havent any problem, thanks you

            1 Reply Last reply
            1
            • J JonB
              21 Mar 2019, 10:56

              @Dooham said in How to create a QByteArray to send in QSerialPort:

              The lenght is still a \t instead of a \x09.

              No, people keep stating this! Bytes are bytes in a QByteArray, period. And '\t' == '\x09'. The problem is that people keep looking at how qDebug() << QByteArray (or debugger) happens to display values, which it tries to display "as though" it were text to "look nice" to you humans :). Use https://doc.qt.io/Qt-5/qbytearray.html#toHex (for qDebug()) instead if you find that clearer....

              D Offline
              D Offline
              Dooham
              wrote on 21 Mar 2019, 11:07 last edited by
              #6

              @JonB Yes, I just realized that. Thanks you.

              J 1 Reply Last reply 21 Mar 2019, 11:08
              0
              • D Dooham
                21 Mar 2019, 11:07

                @JonB Yes, I just realized that. Thanks you.

                J Offline
                J Offline
                JonB
                wrote on 21 Mar 2019, 11:08 last edited by
                #7

                @Dooham You're OK, you are not the first (or the last) here to ask/state this! :)

                1 Reply Last reply
                0

                7/7

                21 Mar 2019, 11:08

                • Login

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