Main Content

Design Data Interface by Configuring Inport and Outport Blocks

The data interface of a model is the means by which the model exchanges data (for example, signal values) with other, external models or systems. Customize the data interface of a model to:

  • Enable integration of the generated code with your own code.

  • Improve traceability and readability of the code.

At the top level of a model, Inport and Outport blocks represent the input and output signals of the model. To customize the data interface in the generated code, configure these blocks. Early in the design process, when a model can contain unconnected Inport and Outport blocks, use this technique to specify the interface before developing the internal algorithm.

When you apply storage classes to Inport and Outport blocks, each block appears in the generated code as a field of a global structure or as a separate global variable that the generated algorithm references directly. If you have Embedded Coder, you can use function prototype control instead of storage classes to pass data into and out of the modelstepfunction as formal parameters. For more information, seeConfigure Generated C Function Interface for Model Entry-Point Functions(Embedded Coder).

Design Data Interface

Open the example modelrtwdemo_configrpinterface.

open_system('rtwdemo_configrpinterface')

Configure the model to show the generated names of blocks.

set_param('rtwdemo_configrpinterface','HideAutomaticNames','off')

On theModelingtab, clickModel Data Editor.

In the Model Data Editor, select theInports/Outportstab. Each row in the table represents an Outport block or a signal that exits an Inport block.

For each of the signals that exit the Inport blocks, setData Typetosingleor to a different data type. Due to the data type inheritance settings that the other blocks in the model use by default, downstream signals in the rest of the model use the same or a similar data type.

Optionally, configure other design attributes such asMinandMax(minimum and maximum values).

Configure Default Storage Class

You can configure a default storage class for Inport blocks and Outport blocks then select the blocks that should use the specified default.

  1. Open theSimulink Coder应用程序。

  2. In the C Code tab, selectCode Interface > Default Code Mappings.

  3. In the Code Mappings editor, underInports and Outports, set the default storage class for bothInportsandOutportstoExportedGlobal.

  4. Next, on theInportstab, select the data items that should use the default storage class. PressCtrland click each inport.

  5. For the selected inports, set the storage class tod模型efault: ExportedGlobal.

  6. Similarly, on theOutportstab, set the storage class for the outport tod模型efault: ExportedGlobal.

Alternatively, to configure the data defaults at the command prompt, use these commands:

cm = coder.mapping.api.get('rtwdemo_configrpinterface'); setDataDefault(cm,'Inports','StorageClass','ExportedGlobal'); setDataDefault(cm,'Outports','StorageClass','ExportedGlobal'); setInport(cm,'In1','StorageClass','Model default'); setInport(cm,'In2','StorageClass','Model default'); setInport(cm,'In3','StorageClass','Model default'); setInport(cm,'In4','StorageClass','Model default'); setOutport(cm,'Out1','StorageClass','Model default','Identifier','output_sig');

通用电气nerate code from the model.

slbuild('rtwdemo_configrpinterface');
### Starting build procedure for: rtwdemo_configrpinterface ### Successful completion of code generation for: rtwdemo_configrpinterface Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================================= rtwdemo_configrpinterface Code generated Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 13.936s

View the generated filertwdemo_configrpinterface.c. Because you applied the storage classExportedGlobalto the Inport and Outport blocks, the code creates separate global variables that represent the inputs and the output.

file = fullfile('rtwdemo_configrpinterface_grt_rtw','rtwdemo_configrpinterface.c'); rtwdemodbtype(file,/ *数据和导出存储* /”,'real_T output_sig;',1,1)
/* Data with Exported storage */ real_T input1; /* '/In1' */ real_T input2; /* '/In2' */ real_T input3; /* '/In3' */ real_T input4; /* '/In4' */ real_T output_sig; /* '/Out1' */

The generated algorithm in the modelstepfunction directly references these global variables to calculate and store the output signal value,output_sig.

信号数据路由到多个输出

Once you have configured the default storage classes for Inports and Outports, you can choose to override this default and set the storage class for some data elements individually. For example, you can route a single signal to multiple Outport blocks and apply a different storage class to each Outport. For example, use this technique to send signal data to a custom function (such as a device driver) and to a global variable that your custom algorithmic code can use:

  1. Branch the target signal line to each Outport block.

  2. For more efficient code, leave the storage class of the target signal line configured toAuto(the default). Optimizations can then eliminate the signal line from the generated code.

  3. In the Code Mappings editor, on theOutportstab, apply the storage class通用电气tSetto one Outport block andExportToFileto the other Outport block.

  4. In the Property Inspector, configure the properties associated with each applied storage class.

open_system('ex_route_sig')

Limitations

You cannot apply a storage class to an Outport block if the input to the block has a variable size. Instead, apply the storage class to the signal line.

Related Topics

See Also

|