MATLAB Answers

Define Pointer in Bus (structure)

14 ビュー (過去 30 日間)
Simulinker 2016 年 9 月 13 日
編集済み: Antoine Bostem 2018 年 4 月 9 日
Hi all,
I need to define a structure that has a pointer to another structure in the generated code.
It should look like this:
typedef struct {
uint8_T sig1;
uint8_T sig2;
uint8_T sig3;
} structure1;
typedef struct {
structure1* MyStructure;
uint8_T sig4;
uint8_T sig5;
} structure1;
Can anyone tell if this is possible and how to define it in Bus Editor? Thanks a lot!

回答 (2 件)

Ning Zhang
Ning Zhang 2016 年 9 月 19 日
Hi there,
I understand that you want to have a nested struct with pointer for the child struct.
The resulting "struct" in the generated code must be a contiguous block of memory, so you cannot have a child element of the structure exist outside of the parent's block of memory.
However, for a nested struct without pointer, it is possible. Just follow the following documentation. Just follow this documentation and the generated code has a nested struct.

JzHartmut 2016 年 12 月 2 日
It is possible to deal with pointer in Simulink Buses which can be used in C in generated code, or in S-functions, not in pure Simulink.
The last one, I think simulink has a paradigm: Data flow from input to output. Pointers offers the possibility to set data backward, from an output bus into the module. That is not desired, therefore pointers in buses are not firstly supported. But for solutions in the C-environment pointers may be a point of interest.
In the header file you should define a union:
typedef struct MyBus_t {
union { int32 myRefBus_int[2]; MyRefBus* myRefBus; } /*unnamed union*/ ;
//... some more elements;
} MyBus;
The Simulink Bus definition should contain only the myRefBus_int element as int32 array of 2 dimensions. The next topic is a S-function which converts any desired Bus to the int32[]. S-functions have pointers as input if the input is a bus at model level. Arrays are also presented as pointners. Therefore the S-function "Bus2ptr" has the following definition in the mex.c file:
static void mdlInitializeSizes(SimStruct *simstruct) {
ssSetInputPortWidth(simstruct, 0, 1);
ssSetInputPortDataType(simstruct, 0, DYNAMICALLY_TYPED);
ssSetOutputPortDataType(simstruct, 0, SS_INT32);
ssSetOutputPortWidth(simstruct, 0, 2);
static void mdlOutputs(SimStruct *simstruct, int_T tid)
void const*const* ptrs = ssGetInputPortSignalPtrs(simstruct, 0); //InputPtrsType, InputRealPtrsType etc.
void** x = (void**) ptrs[0];
int32* y = (int32*)ssGetOutputPortSignal(simstruct, 0);
int64 ptr = (int64)(*x);
y[0] = (int32)ptr;
y[1] = (int32)(ptr >>32); //assume little endian. It is proper for all PC processors.
There are used two int32 because it should run on a 64 bit PC. The tlc-file contains the line:
%function Outputs(block, system) Output
%assign u1_ptr = LibBlockInputSignal(0, "", "", 0)
%assign y1_ptr = LibBlockOutputSignalAddr(0, "", "", 0)
*(int32*)(u1_ptr) = (int32)(u1_ptr);
For a 32-bit target system you need only 1 element. With this S-function you can convert any bus to int32-array and store the int32 values which is the address in the bus structure. From C level you can use the other union element in the bus header struct.
If I have enougth time this weekend I will describe some more at my internet page Yet there is nothing about simulink stored there. But I will open a presentation corner for simulink in the next time.
  2 件のコメント
Antoine Bostem
Antoine Bostem 2018 年 4 月 9 日
I am interested by using pointer in S-function and so Bus. If I have understand your post, there is many ways to use pointer in Simulink, Header definition and S-function use ? Did the header definition need int16 variable if we use a 32-bit target ?
Could I just create un function with the .tlc code that you give ?
Thank you,


Community Treasure Hunt

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

Start Hunting!

Translated by