I'm having problems casting an array of uint8_t to a struct in C using a Silicon Labs MCU (EFM32GG11B120F2048GQ64). The byte array is the received data from a UART, and I want to cast it to a struct representing the message structure.
When casting the array to the struct, report_type
(uint8_t) is byte [0] in the array, as it should. But start_time
(uint32_t) is not the value of bytes [1..4] as I would think, but rather bytes [4..7]. Bytes [1..3] are not used.
I tried adding a second uint8_t member report_type2
, effectively moving start_time
down one position. report_type2
gets the value of byte [1], but start_time
still gets the value of bytes [4..7].
Anybody knows what's going on here and why this is?
Byte array:
05 29 68 58 3C 0E 10 02 44 0D D5 C3 3F 0F 5C 29 00 05 69 75 00 00 0A B8 07 25 A7 11 00 B5 01 02 4C 1D 00 1C 0A FF FF 00 00 00 0A 01 B9 06 00 09 07 25 A7 11 00 B5 01 02 4C 1D 00 1C 0A FF FF 00 00 00 0A 01 B9 06 00 09 07 25 A7 11 00 B5 01 02 4C 1D 00 1C
Struct:
typedef struct
{
uint8_t report_type;
uint32_t start_time;
uint16_t period_length;
uint8_t status_alarm_code;
float_union_t total;
float_union_t average;
subperiod_t subperiod_1;
subperiod_t subperiod_2;
subperiod_t subperiod_3;
subperiod_t subperiod_4;
subperiod_t subperiod_5;
subperiod_t subperiod_6;
subperiod_t subperiod_7;
subperiod_t subperiod_8;
subperiod_t subperiod_9;
subperiod_t subperiod_10;
}report_t;
Casting array to struct:
report_t *report = (report_t *)packet->data;
typedef struct {...} __attribute__((packed)) report_t;
\$\endgroup\$