Representation of the C-structure of pointers using Simulink Bus

How can I associate a C-structure of pointers with Simulink.Bus?

Say I have a C struct:

typedef struct 
{
    int32_T *a;
    uint8_T *b;
} Bus_X ;

then for Simulink.Bus.objectToCell

 { ...
           'Bus_X', ...
           'Bus.h', ...
           sprintf(''), ...
           'Imported', {...
           {'a', 1, '???', -1, 'real', 'Sample', 'Fixed'}; ...
           {'b', 1, '???', -1, 'real', 'Sample', 'Fixed'}; ...
           } ...
 } ...

What should be "???"

I want to use it like the following in external C code as

Bus_X x_data = { &a_sig, &b_sig  };
Bus_X* x_ptr = &x_data;

where x_ptrwill be ImportedExternPointeron the simulink signal

Then, if possible, Simulink should do the magic in the generated code, for example:

*(x_ptr->a) = 42.0 ;
+4
source share
1 answer

You can use the pointer in Simulink buses, which can be used in C in the generated code or in S-functions, and not in pure Simulink.

, , simulink : . , . , . - .

:

typedef struct MyBus_t { union { int32 myRefBus_int[2]; MyRefBus* myRefBus; } /*unnamed union*/ ; //... some more elements; } MyBus;

Simulink Bus myRefBus_int int32 2 . - S-, int32 []. S- , . . S- "Bus2ptr" mex.c:

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

int32, 64- . tlc :

 %function Outputs(block, system) Output
  %assign u1_ptr = LibBlockInputSignal(0, "", "", 0)
  %assign y1_ptr = LibBlockOutputSignalAddr(0, "", "", 0)
  *(int32*)(u1_ptr) = (int32)(u1_ptr);

32- 1 . S- int32- int32, . C union .

, - www.vishia.org. , Simulink . simulink.

0

Source: https://habr.com/ru/post/1589522/


All Articles