Simulink coder packed structs

Hello, is there a way to have the Simulink autocoder turn bus objects into packed structs (byte alignment=1)? E.g., the struct definition might look like:
struct __attribute__((__packed__)) mystruct_A {
char a;
int b;
char c;
};
Thanks!

Respuestas (1)

Mark McBroom
Mark McBroom el 29 de Mzo. de 2025

0 votos

See the "Alignment" property for Simulink.Bus elements defined here: https://www.mathworks.com/help/simulink/slref/simulink.bus.html
Thanks.
Mark.

1 comentario

Amin
Amin hace alrededor de 4 horas
The Alignment property of Simulink.Bus does not make the generated C struct packed (byte-aligned). It only controls the alignment of the bus as a whole in memory, not the internal layout of its elements.
For example, even with Alignment = 1, the generated code is still of the form:
typedef struct {
real32_T a;
uint16_T b;
uint8_T c;
} MyStruct;
and the compiler may still insert padding between fields.
So this does not produce:
typedef struct __attribute__((__packed__)) {
...
} MyStruct;
As far as I understand, to achieve a truly packed layout with Simulink Coder, the only robust approaches are:
  1. Define the struct manually in a header with __attribute__((__packed__)) and set the bus to Imported, or
  2. Accept the generated struct layout and design the binary data to match the compiler’s padding.
If there is a way to force packed structs directly from Simulink Coder (e.g., via configuration or TLC), I would be very interested to know.

Iniciar sesión para comentar.

Productos

Preguntada:

el 4 de Abr. de 2014

Comentada:

el 24 de Abr. de 2026 a las 10:49

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by