Main Content

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.

Simulink canvas with 2 Model blocks. One Model block is named "mClient1" and displays a port labeled "client". The other Model block is named "mServer1" and displays a port labeled "server". The 2 ports are connected by a signal line, with a solid black circle at the end of the line next to the port labeled "server".

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.

Block dialog for Function Element block

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.

Simulink canvas with a Simulink Function block with "scoped: serverPort" superimposed on the block icon, and a Function Element block with 3 function elements labeled "serverPort". A function connector line connects the Simulink Function block to one of the function elements.

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.

Block dialog of Function Element Call block

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: PortName.FunctionElementName. The block icon indicates the association with the port.

Simulink canvas with 2 Function Element Call blocks labeled "clientPort.f0" and "clientPort.f1" and a Function-Call Subsystem. Inside the subsystem there is a Function Caller block with "clientPort.f0()" superimposed on the block icon. The Function Caller block is connected to the first Function Element Call block via a function connector line.

Note

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 associate function ports with service interfaces authored in System Composer™ using the Function Element and Function Element Call blocks that correspond to the port.

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.

Simulink canvas with a Model block referencing model "serverModel" displaying a port labeled "serverPort" and a Model block referencing model "clientModel" displaying a port labeled "clientPort". The ports are connected by a line, with a filled-in black circle at the end of the port labeled "serverPort".

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 dialog.

Server model for asynchronous interfaces.

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.

Client model for asynchronous interfaces.

For more information, see Simulate Asynchronous Services for Vehicle Headlight Management (System Composer).

See Also

| | | | | | |

Related Topics