PDA

View Full Version : Sending a "struct" through a serial (with QIODevice::write())



Momergil
5th December 2011, 17:57
Hello!

I have to create a software that communicates with a serial port sending, instead of any chars, a structure. A software that sends bytes I already have, so no problem. The problem is that the function that sends data to the serial port uses the qint64 QIODevice::write ( const QByteArray & byteArray ) in a similar way to this:



{ //.h
#include <Serial/abstractserial.h>
AbstractSerial *port1;
}

{
port1 = new AbstractSerial();
}

bool command::Write(QByteArray com)
{
if(!port1->isOpen())
{
emit sendSBMessage("Port closed");
return false;
}
else
{
port1->write(com + "\r\0");
return true;
}
}


Now what I have to do is to create a function that instead of sending that QByteArray, it rather sends a structure. But the problem is that the function used by AbstractSerial port1 is the QIODevice::write, and this function is not ready for sending structures as it seems.


So how do I do this? I have no idea.


Thanks,


Momergil

Lesiok
5th December 2011, 19:41
You must create a function that performs serialization structure to QByteArray. And the recipient must perform the inverse operation.

Momergil
5th December 2011, 19:53
You must create a function that performs serialization structure to QByteArray. And the recipient must perform the inverse operation.

Hmmm, not sure that I understood. You mean that I must create some sort of protocol that catch the data from the structure, put it into a QByteArray variable and sends it normally through the serial port, and than the other side, knowing that protocol, will "translate" it?


Momergil

sirius
5th December 2011, 21:15
You want to read about the QDataStream (http://doc.qt.nokia.com/latest/qdatastream.html#details) class and check out this example (http://stackoverflow.com/questions/2570679/serialization-with-qt).
Declare the << and >> operator overloads as friend (http://www.parashift.com/c++-faq-lite/friends.html) to get access to private data in your class.

ChrisW67
5th December 2011, 23:58
Lesiok has the general process, and sirius has a good approach for Qt-to-Qt program communications (although it can do other things). Your requirement is too vague to allow much more specific answers.

What is at the other end of the transmission? Another Qt program or something else? Do you understand/have intrinsic variable/byte order/character encoding differences? Is the "structure" a simple C++ struct using only simple types, a list, a tree or other graph...? If it is a graph, does it have cycles?

Lesiok
6th December 2011, 11:24
Hmmm, not sure that I understood. You mean that I must create some sort of protocol that catch the data from the structure, put it into a QByteArray variable and sends it normally through the serial port, and than the other side, knowing that protocol, will "translate" it?
MomergilYes. In addition, data transfer without packing in a protocol is sensitive to transmission errors.