Model Client-Server Communication 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. 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. If a model uses synchronous functions, the top model that contains the client and server reference models must also be an export-function model. For the configuration, 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
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 first text field in the block label provides the port name.
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 Function Caller block with the port, use dot notation with the port name first and the function element name second.
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.
You can specify the Data type of the Function Element and Function Element Call blocks as a service interface type which allows you to associate function ports with service interfaces authored in System Composer™.
When you reference a model with a function port, the port displays on the 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 function prototypes of function elements. Function ports can connect only to function ports.
You can also create your client-server interfaces using System Composer software architectures. This provides you with a Simulink behavior with predefined and configured Function Element Call and Function Element blocks. For more information regarding this workflow, see Author Service Interfaces for Client-Server Communication (System Composer).
Configure Asynchronous Execution 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 block in immediate mode. The Message Triggered Subsystem block 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 parameter. 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)