Main Content

Access Real-time C API Information

The real-time values of model elements are stored in computer memory. The number of memory units used for storing the value of a specific element depends on the number of components in the element. For example, if the signal mySig is a three-dimensional array with size 3-by-2-by-4, then its real-time value is stored in 24 memory units. The pointers to these memory cells are stored in a contiguous region of the C API dataAddrMap array, where the number of entries in that region is equal to the number of components in the model element. So, if mySig is a signal with size 3-by-2-by-4, the pointers to the memory cells that hold the value of mySig are stored in 24 consecutive entries in dataAddrMap. Suppose that the 24 consecutive entries are entries 23 through 46 of the array. To access the three-dimensional real-time value of mySig, your code:

  1. Gets the dataTypeIndex, dimIndex, and addrMapIndex indices for mySig. To learn how to get these indices, see C Code for Accessing Signal C API Information.

  2. Uses the dimensions of mySig to calculate the number of components its value consists of. In this example, mySig has 24 components. To learn how to obtain the dimensions of model elements, see C Code for Obtaining Element Dimensions Through C API.

  3. Gets the data type of mySig. The data type is required to read the signal values correctly. To learn how to obtain the data type of elements, see C Code for Accessing Data Type C API Information.

  4. References into the correct entry in dataAddrMap using the value of addrMapIndex, which in this example is 23.

  5. Uses the pointers in entries 23 through 46 of dataAddrMap to obtain the 24 real-time value components of mySig.

    Note

    The pointers in dataAddrMap are of the generic type void*. Your code needs to downcast them to the actual pointer type they are, based on the signal data type, which your code previously obtained.

Print Real-time Signal Values Example

This example uses the instance specific map to print the actual value of the model signals.

The example code is also provided as standalone code (see Standalone C Code for Printing Real-time Signal Values Example) so that you can copy it more easily.

C Code for Printing Real-time Signal Values Example

DescriptionSample C Code with Direct Data Structure AccessSample C Code with Macros and Get-Functions
Include the generated model header so you can access the real-time model object. Also include rtw_modelmap.h and rtw_capi.h so in which the structures you need to access are defined.
# include "capiDemoRef.h"
# include "rtw_modelmap.h"
# include "rtw_capi.h"
# include <stdio.h>
# include "capiPrintSignalsDemo.h"
# include "capiDemoRef.h"
# include "rtw_modelmap.h"
# include "rtw_capi.h"
# include <stdio.h>
# include "capiPrintSignalsDemo.h"
Declare variables to use for storing the map objects and the signals information.
const rtwCAPI_ModelMappingInfo *mainCapiMap;
const rtwCAPI_ModelMappingStaticInfo *capiStaticMap;

uint_T sigCount;
const rtwCAPI_Signals *capiSigArr;

void** dataAddrMap;

/* Index into the data map array */
uint_T dataMapIdx;

/* Assume each signal is a one dimension double. */
double* sigVal;
const rtwCAPI_ModelMappingInfo *mainCapiMap;

uint_T sigCount;
const rtwCAPI_Signals *capiSigArr;

void** dataAddrMap;

/* Index into the data map array */
uint_T dataMapIdx;

/* Assume each signal is a one dimension double. */
double* sigVal;
Get the pointers to the C API model maps (see Obtain Access to C API Maps Example for more details). Then extract the number of signals that are included in the generated C API code, and obtain the pointer to the signal structure array.
void printModelSignalVals()
{
  mainCapiMap = &capiDemoRef_M->DataMapInfo.mmi;
  capiStaticMap = mainCapiMap->staticMap;

  sigCount = (capiStaticMap->Signals).numSignals;
  capiSigArr = (capiStaticMap->Signals).signals;
void printModelSignalVals()
{
  mainCapiMap = &rtmGetDataMapInfo(capiDemoRef_M).mmi;

  sigCount = rtwCAPI_GetNumSignals(mainCapiMap);
  capiSigArr = rtwCAPI_GetSignals(mainCapiMap);
To get the pointers to the (primitive type) data addresses structure array, use the instance map pointer to access the dataAddrMap field of the map.
  dataAddrMap = mainCapiMap->InstanceMap.dataAddrMap;
  dataAddrMap = rtwCAPI_GetDataAddressMap(mainCapiMap);
Start a for-loop to go over the model signals.
  printf("The values of the model signals are: ");
  for(int idx=0 ; idx<sigCount ; idx++)
  {
  printf("The values of the model signals are: ");
  for(int idx=0 ; idx<sigCount ; idx++)
  {
For each signal, get the index that references into the data map array.
    dataMapIdx = capiSigArr[idx].addrMapIndex;
    dataMapIdx = rtwCAPI_GetSignalAddrIdx(capiSigArr,idx);
Use the index to access the corresponding entry in the data map array and print the value of each signal.
    sigVal = (double*)dataAddrMap[dataMapIdx];
    printf("%f", *sigVal);
    if(idx < (sigCount-1))
    {
      printf(" , ");
    }
  }
  printf("\n");
}
    sigVal = (double*)rtwCAPI_GetDataAddress(dataAddrMap,dataMapIdx);
    printf("%f", *sigVal);
    if(idx < (sigCount-1))
    {
      printf(" , ");
    }
  }
  printf("\n");
}

Standalone C Code for Printing Real-time Signal Values Example

 Ready-to-Copy Standalone C Code

Here is a schematic illustration of the map structure that is accessed by the code in the example:

Related Topics