Call Simulink Functions in Other Models Using Function Ports
Function ports allow a referenced model to call a Simulink® function defined in a second referenced model. A model can issue a function call through an invoking function port to invoke a function that is defined and exported in another model with an exporting function port block. You can use function ports to model client and server components in a distributed service architecture as well as to facilitate data sharing using a functional interface between component models.
Connect Client and Server Models with Function Ports
A model that uses function ports must be an export-function model. See Export-Function Models Overview.
To create an exporting function port, add a Function Element block to your model, specify a name for the port, and specify one or more function elements.
For each function element, add a Simulink Function block to the model
and implement the desired algorithm inside the Simulink Function block. To connect
the Simulink Function block to the
function port, set the Function visibility parameter of the
Trigger block inside the Simulink Function block to
port, and enter the port name in the Scope
to port box.
Because the block is scoped to the port, it is not visible elsewhere within the model hierarchy and can be called only through the port. The block icon indicates the port scope.
To create an invoking function port, add a Function Element Call block to your model, specify a name for the port, and specify one or more function elements.
For each function element, add a Function-Call Subsystem block, and
place a Function Caller block inside the subsystem. To associate the
Caller block with the port, use dot notation:
The block icon indicates the association with the port.
To display green connector lines from the Function Element Call blocks to the Function-Call Subsystem blocks, on the toolstrip, navigate to Debug > Information Overlays > Blocks and select Function Connectors.
When you reference a model with a function port, the port displays on Model block. To allow functions to be called through the ports, add a line between them. A ball icon appears at the end of the line next to the exporting function port. The attributes of the ports must match in number, names, and prototypes of function elements. Function ports can connect only to function ports and not to any other signal.
Configure Asynchronous Interfaces with Function Ports
Asynchronous execution occurs when the caller (client) makes a request to call the function (server). The function is executed based on the ordering defined in the Schedule Editor and then returns the output arguments to the caller. The block outputs these arguments using a message output port.
If there is one function output argument, the output argument becomes the message payload.
If there is more than one function output argument, the Function Caller block bundles the output arguments as a structure that becomes the message payload.
Connect the message output port to a Message Triggered Subsystem in immediate mode. The Message Triggered Subsystem acts as a callback for the function.
For the server model, in the Simulink Function block, configure the
Trigger block parameters. From the
Trigger type list, select function-call, then
select the Treat as a Simulink Function check box.
Then, from the Function visibility list, select
port. Select the
Execute function call asynchronously check box
on the Trigger block parameters
In the client model, configure the Function
Caller block. to execute function calls asynchronously. Select the
Execute function call asynchronously check box on the Function
Caller block parameters dialog.
For more information, see Simulate Asynchronous Services for Vehicle Headlight Management (System Composer).
- Define Port Interfaces Between Components (System Composer)
- Author and Extend Functions for Software Architectures (System Composer)
- Software Component Modeling
- Author Service Interfaces for Client-Server Communication (System Composer)
- Service-Oriented Sensor Modeling (System Composer)
- Simulate Asynchronous Services for Vehicle Headlight Management (System Composer)