I have some code on a Cortex-M4 microcontroller and'd like to communicate with a PC using a binary protocol. Currently, I'm using packed structs using the GCC-specific packed attribute.
Here is a rough outline:
struct Sensor1Telemetry {
int16_t temperature;
uint32_t timestamp;
uint16_t voltageMv;
// etc...
} __attribute__((__packed__));
struct TelemetryPacket {
Sensor1Telemetry tele1;
Sensor2Telemetry tele2;
// etc...
} __attribute__((__packed__));
My question is:
- Assuming that I use the exact same definition for the
TelemetryPacketstruct on the MCU and the client app, will the above code be portable accross multiple platforms? (I'm interested in x86 and x86_64, and need it to run on Windows, Linux and OS X.) - Do other compilers support packed structs with the same memory layout? With what syntax?
EDIT:
- Yes, I know packed structs are non-standard, but they seem useful enough to consider using them.
- I'm interested in both C and C++, although I don't think GCC would handle them differently.
- These structs are not inherited and don't inherit anything.
- These structs only contain fixed-size integer fields, and other similar packed structs. (I've been burned by floats before...)