Main Content

Write Code that Interacts with C API Code

This step of the example demonstrates how to add external code that interacts with the generated C API code.

Open the model CapiGetStarted.

capiMdl = "CapiGetStarted";
open_system(capiMdl);

Write Code to Interact with Generated C API Code

The C function exploreCapiModelElements is defined in the file intro2Capi.c (located in the folder CapiGetStartedFiles), and interacts with the generated C API model code. This function uses the C API interface to access metadata for model signals and model parameters.

This is the definition of the function in the source file:

/** exploreCapiModelElements **
 * Use the C API to obtain and print information about model signals and model parameters.
 */
void exploreCapiModelElements()
{
  // Get a pointer to the main C API map from the realtime object:
  rtwCAPI_ModelMappingInfo *mainCapiMap = &CapiGetStarted_M->DataMapInfo.mmi;

  // Get a pointer to the main C API static map:
  rtwCAPI_ModelMappingStaticInfo *capiStaticMap = mainCapiMap->staticMap;

  // Get the number of model signals:
  int sigCount = (capiStaticMap->Signals).numSignals;

  // Get a pointer to the signal struct array.
  const rtwCAPI_Signals *capiSigArr = (capiStaticMap->Signals).signals;

  // Get the number of model parameters:
  int modelParamCount = (capiStaticMap->Params).numModelParameters;

  // Get a pointer to the model parameter struct array.
  const rtwCAPI_ModelParameters *capiModelParamArr = (capiStaticMap->Params).modelParameters;

  // Print the number of model signals and their names:
  printf("There are %i model signals, and their names are: ",sigCount);
  for(int idx=0 ; idx<sigCount ; idx++)
  {
    printf(capiSigArr[idx].signalName);
    if(idx < sigCount-1)
    {
      printf(" , ");
    }
  }
  printf(".\n");

  // Print the number of model parameters and their names:
  printf("There are %i model parametes, and their names are: ",modelParamCount);
  for(int idx=0 ; idx<modelParamCount ; idx++)
  {
    printf(capiModelParamArr[idx].varName);
    if(idx < modelParamCount-1)
    {
      printf(" , ");
    }
  }
  printf(".\n");
}

Instruct the code generator to compile intro2Capi.c along with the model and invoke the exploreCapiModelElements function during the initialization stage of the model code execution. To do this, specify this custom code information:

Specified Information

Custom Code Instruction

Where the function is declared

Include header directive

Where the function is defined

Source file path

When and how to invoke the function

Add function invocation code to model initialization code

Store these custom code information instructions in variables.

includeHeaderDirective = '# include "CapiGetStartedFiles/intro2Capi.h"';
srcFilePath = "CapiGetStartedFiles/intro2Capi.c";
addInitCode = "exploreCapiModelElements();";

To add these code generation instructions:

  1. Open the Configuration Parameters dialog box.

  2. Navigate to the Code Generation > Custom Code pane.

  3. In the Custom code settings section, select the Code Information tab and enter the paths of the header and source files in the Include headers and Source files boxes, respectively.

  4. Select the Additional source code tab and enter the call to the exploreCapiModelElements in the Initialize code box.

  5. Click OK.

Configuration Parameters dialog box. The Code Generation > Custom Code pane is open. The include header directive is specified in the Code information tab of the Custom code settings section as '# include "CapiGetStartedFiles/intro2Capi.h"'

Alternatively, you can specify these parameters programmatically by entering these commands in the Command Window:

set_param(capiMdl,CustomHeaderCode=includeHeaderDirective)
set_param(capiMdl,CustomSource=srcFilePath)
set_param(capiMdl,CustomInitializer=addInitCode)

Generate Model Code

Use the slbuild command to generate code from your model. Use evalc to suppress the output of the slbuild command.

evalc("slbuild(capiMdl,GenerateCodeOnly=false)");

The code generator generates a standalone executable from the model. Use the system command to run this executable with the "-echo" input argument so that you can see the output of the model executable, with the printing from the exploreCapiModelElements function.

system(capiMdl,"-echo");
/bin/bash: line 1: CapiGetStarted: command not found