How to create a QByteArray to send in QSerialPort
-
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:
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
-
@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;
-
@jsulm That is what i want, but with the DataStream I'm still having the same issue:
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.
-
@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 howqDebug() << 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 (forqDebug()
) instead if you find that clearer....