Reputation: 1020
I want to send a structure having many structures in it to another controller(not the same make of the controller,having a different Architecture). Im required to develop a sort of serialisation to send data across without using invented serialisation methods available. How do i find out where padded bytes are added so that I can remove them out before sending it to the other controller? How can i implement a metadata so that the other controller can reconstruct every member of the structure from the transmitted bytes of data? Any other points that may be required also would be welcome.
Upvotes: 1
Views: 57
Reputation: 213999
Fully portable serialization/de-serialization is quite manual. You just go through ever member of the struct and copy it. You could use offsetof
to spot padding, but that's mostly meaningless practice, since it means you would need to do some "manual coding" per struct member anyway.
#pragma pack(1)
and similar isn't ideal. Structs are packed for a reason, and there are no portable means to disable padding. It may be feasible on some smaller CPUs like 8 or 16 bit, but not for fully portable code.
What's important is to have a well-defined network protocol, that states how data is stored, including endianess. That is, the sender, the protocol and the receiver all have endianess and they are not necessarily the same.
(Traditionally, data protocols often used big endian since that is the only thing that makes sense for implementing hardware CRC checks with XOR gates, but that's mostly a non-issue nowadays. Some MCUs even have CRC check hardware built-in.)
Upvotes: 3