Main Content

Configure Model to Generate C API Code for Root-Level Ports

This example shows how to configure a model to include the C API for root-level input and output ports in the generated code.

Open the model CapiConfigDemo.

capiMdl = "CapiConfigDemo";
open_system(capiMdl);

Enable C API Generation for Root-Level Ports

To enable C API code generation for root-level ports:

  1. Open the Configuration Parameters dialog box.

  2. Navigate to the Code Generation > Interface pane.

  3. In the Data exchange interface > Generate C API for section, select root-level I/O.

  4. Click OK.

Configuration Parameters dialog box. The Code Generation > Interface pane is open. In the Generate C API for section, the root-level I/O box is selected.

Alternatively, enter this in the Command Window:

set_param(capiMdl,RTWCAPIRootIO=true)

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=true)");

Verify That Root-Level Ports Are Included in C API

The C API structure arrays that correspond to model root-level ports are rtRootInputs and rtRootOutputs. To view the instantiation code for these structures, examine the generated C API source file CapiConfigDemo_capi.c (located in the folder CapiConfigDemo_grt_rtw).

This is the instantiation code for the structures in this file:

/* Root Inputs information */
static const rtwCAPI_Signals rtRootInputs[] = {
  /* addrMapIndex, sysNum, blockPath,
   * signalName, portNumber, dataTypeIndex, dimIndex, fxpIndex, sTimeIndex
   */
  { 0, 0, TARGET_STRING("CapiConfigDemo/Inport1"),
    TARGET_STRING(""), 1, 0, 0, 0, 0 },

  { 1, 0, TARGET_STRING("CapiConfigDemo/Inport2"),
    TARGET_STRING(""), 2, 0, 0, 0, 0 },

  {
    0, 0, (NULL), (NULL), 0, 0, 0, 0, 0
  }
};

/* Root Outputs information */
static const rtwCAPI_Signals rtRootOutputs[] = {
  /* addrMapIndex, sysNum, blockPath,
   * signalName, portNumber, dataTypeIndex, dimIndex, fxpIndex, sTimeIndex
   */
  { 2, 0, TARGET_STRING("CapiConfigDemo/Outport1"),
    TARGET_STRING(""), 1, 0, 0, 0, 0 },

  { 3, 0, TARGET_STRING("CapiConfigDemo/Outport2"),
    TARGET_STRING(""), 2, 0, 0, 0, 0 },

  {
    0, 0, (NULL), (NULL), 0, 0, 0, 0, 0
  }
};

Related Topics