Main Content

Model Client-Server Communication Using Function Ports

You can model service-oriented communication in Simulink® using export-function models and function ports. Function ports allow you to facilitate data sharing between component models. This topic shows you how to use function ports to model client and server components in Simulink.

To start from Simulink, client or server component models that use function ports must be an export-function model. If a model uses synchronous functions, the parent model of the components must also be an export-function model. For the configuration, see Export-Function Models Overview.

If you want to model service-oriented communication at the architecture level, you can use preconfigured Simulink export-function models for components in System Composer™ software architectures. For more information, see Service Interfaces Overview (System Composer).

Model Server Component with Function Element Block

A server component is one that provides services. Services are modeled as Simulink Function blocks that are scoped to Function Element ports. For each server port in your service interface, there is a Simulink Function block where you can implement the desired algorithm. The Function Element block is a function port with the function elements specified in your service interface.

Referenced model, ServerModel, with Simulink Function block labeled y equals f0 of u and a Function Element block labeled Server.f0.

To modify the port name or function elements, open the properties of the Function Element block.

Block dialog for Function Element block.

Inside the Simulink Function block, there is a Trigger block. To associate the Simulink function with the server port:

  1. Set the Function visibility parameter of the Trigger block inside the Simulink Function block to port.

  2. Set the Scope to port parameter of the Trigger block to the port name.

Block dialog for Trigger Port block.

The Simulink Function block is scoped to the server port, meaning that the port is not visible elsewhere in the model hierarchy and you can only call the function through the server port. The dot notation near the Function Element block shows the association of the function with the port. The port name is first, followed by the function name.

Function Element block labeled Server.f0.

To implement function behavior, double-click the Simulink Function block. Inside the Simulink Function block is a Trigger block, an ArgIn block, and an ArgOut block. Add any necessary blocks to the function subsystem and connect elements to the ArgIn block and the ArgOut block.

Model Client that Requires Services

A client model is one that requires services from other models. A required service is modeled using a Function Caller block and a Function Element Call block.

Referenced model, ClientModel, with Function-Call Subsystem block labeled callf0, a Function Element Call block labeled Client.f0, and an Inport block labeled callf0Port.

To modify the port name or function elements, open the properties of the Function Element Call block.

The Function Element Call block is connected to the Function-Call Subsystem block because the you call the function through the function port.

Inside the Function-Call Subsystem block is a Function Caller block. To associate the function caller with the client port, set the Function prototype parameter of the Function Caller block to use dot notation with the port name followed by the function name.

Function Caller block labeled f0 Caller connected to an inport and an outport.

The dot notation near the Function Element Call block shows the association of the function with the port. The port name is first, followed by the function name. The Inport block is connected to the Function-Call Subsystem block with a root-level input port connection.

Function Element Call block labeled Client.f0

To implement the function call inside the Function-Call Subsystem block, double-click the Function-Call Subsystem block. Add any necessary blocks to the function call subsystem to represent an input and an output.

Configure Asynchronous Execution with Function Ports

Asynchronous execution is when the client sends a request to the server, the function executes asynchronously based on the priority order, and the function returns the output arguments to the client.

The function executes based on the ordering you define in the Schedule Editor. The behavior of the message payload can change depending on the number of function output arguments.

  • If the function has one output argument, the output argument becomes the message payload.

  • If the function has more than one output argument, the Function Caller block bundles the output arguments as a structure that becomes the message payload.

For the server model, the Trigger block parameters are different than those of a synchronous system. To associate the Simulink function with the server port:

  1. Set the Trigger type parameter of the Trigger block to function-call.

  2. Set the Treat as a Simulink Function parameter of the Trigger block to on.

  3. Set the Function visibility parameter of the Trigger block to port.

  4. Set the Execute function call asynchronously parameter of the Trigger block to on.

For the client model, to associate the function caller with the client port, set the Execute function call asynchronously parameter of the Function Caller block to on. The Message Triggered Subsystem block acts as a callback for the function. You must connect the Message Triggered Subsystem block to the message output port of the Function Call Subsystem block.

Referenced model, ClientModel, with Function-Call Subsystem block labeled callf0, a Function Element Call block labeled Client.f0, an Inport block labeled callf0Port, and a Message Triggered Subsystem block.

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

See Also

| | | | | | |

Related Topics