1

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.

1 Answer 1

3

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.)

13
  • Depending on requirements, the "well-defined network protocol" could even be ASCII-based. Such a transfer is way safer and easier to implement and monitor/debug than any binary protocol.
    – tofro
    Dec 21, 2017 at 9:05
  • 1
    @tofro Not really, ASCII protocols mean data bloat and slower encoding/decoding. The only reason you would use them is if monitoring should be done by non-programmers. I would avoid implementing old crap like "AT commands".
    – Lundin
    Dec 21, 2017 at 9:27
  • Most, if not all of the thing called Internet is based on such "old crap". Can't be that bad?
    – tofro
    Dec 21, 2017 at 9:30
  • @tofro No, most of the internet is based on TCP/IP.
    – Lundin
    Dec 21, 2017 at 9:39
  • You either haven't had a look into TCP/IP packet payload (not likely) or actively ignore what you've seen.
    – tofro
    Dec 21, 2017 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.