Main Content

Model Client and Server Components Using Function Ports

You can model client and server components in Simulink® using function ports. Function ports allow you to facilitate data sharing between component models. This topic shows you how to use Simulink functions, function callers, and function ports to model client and server components.

You can integrate and simulate client and server components and their interfaces in a System Composer™ software architecture model. For more information, see Service Interfaces Overview (System Composer).

Model Server that Provides Services

A server component is one that provides services. Services are modeled as Simulink functions that are scoped to exporting function ports. You can implement the desired algorithm of your service using a Simulink Function block. The function must be scoped to an exporting function port using a Function Element block. Server components must be export-function models. For more information, see Export-Function Models Overview.

In this example, the fetchData function is defined in a Simulink Function block and scoped to the Server port represented by a Function Element block.

Simulink model with Simulink Function block labeled data equals fetch data and a Function Element block labeled Server.fetchData.

To associate a function with an exporting function port, you must configure the Trigger block located inside of the Simulink Function block. Follow these steps to configure your Simulink function:

  1. Double-click the Trigger block to open the block parameters.

  2. Select the Treat as Simulink function parameter.

  3. Set the Function name parameter to the name of the Simulink function. In this example, the Function name is set to fetchData.

  4. Set the Function visibility parameter to port.

  5. Set the Scope to port parameter to the name of the exporting function port. In this example, the Scope to port is set to Server.

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

After you configure the Simulink Function block, you can implement the desired algorithm of your service inside of the Simulink Function. Add any necessary blocks to the function subsystem and ensure these blocks are connected to the argument blocks. ArgIn and ArgOut blocks represent the function input and output arguments, respectively.

Model Client that Requires Services

A client component is one that requires services from other models. Required services are modeled as function callers that are associated with an invoking function port. You can call a required service with a Function-Call Subsystem block and Function Caller block. The function caller must be associated with an invoking function port using a Function Element Call block.

In this example, the fetchData function is associated with the invoking function port, Client. A function connector is shown between the Function Element Call block labeled Client.fetchData and the Function-Call Subsystem block to represent the association between the function call and the port.

Simulink model with Function-Call Subsystem block labeled callfetchData, a Function Element Call block labeled Client.fetchData, and an Inport block labeled callfetchDataPort.

To associate a function caller with an invoking function port, you must configure the Function Caller block located inside of the Function-Call Subsystem block. Follow these steps to configure your Function Caller:

  1. Double-click the Function Caller block to open the block parameters.

  2. Set the Function prototype parameter to the prototype of the Simulink function. In this example, the Function prototype parameter is set to data = Client.fetchData.

Function Caller block labeled fetchData Caller connected to an outport.

The Function Caller block is associated with the invoking function port. The block label of the Function Element Call block shows this association of the function with the port. The port name is first, followed by the function name.

After you configure the Function Caller block, you can implement the call to the required service inside the Function-Call Subsystem block. Add any necessary blocks to the function call subsystem to represent any inputs and outputs.

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.

Asynchronous calls to a service require the use of message payloads to store function output arguments. 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.

To associate a function with an exporting function port in an asynchronous system, you must configure the Trigger block located inside of the Simulink Function block. The Trigger block parameters are different than those of a synchronous system. Follow these steps to configure your Simulink function for asynchronous execution:

  1. Double-click the Trigger block to open the block parameters.

  2. Set the Trigger type parameter to function-call.

  3. Select the Treat as a Simulink function parameter.

  4. Set the Function visibility parameter to port.

  5. Select the Execute function call asynchronously parameter.

To associate a function caller with an invoking function port in an asynchronous system, you must configure the Function Caller block located inside of the Function-Call Subsystem block. The Function Caller block parameters are different than those of a synchronous system. Follow these steps to configure your Function Caller:

  1. Double-click the Function Caller block to open the block parameters.

  2. Select the Execute function call asynchronously parameter.

In an asynchronous system, the Function-Call Subsystem block must connect to a Message Triggered Subsystem block. The Message Triggered Subsystem block acts as a callback for the function.

Simulink model with Function-Call Subsystem block labeled callfetchData, a Function Element Call block labeled Client.fetchData, an Inport block labeled callfetchDataPort, and a Message Triggered Subsystem block.

When implementing asynchronous services, client components must be export-function models.

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

Composition Model to Simulate Client and Server Components

Once you implement the algorithm of each client and server component in a Simulink model, you can integrate components into a composition model.

Consider utilizing System Composer software architectures as your composition model. Create a software architecture, link software components to your client and server component models, and simulate the behavior of your service from the architecture level. For more information, see Service Interfaces Overview (System Composer).

Note

If using a Simulink model for the composition of synchronous services, the composition model must be an export-function model.

See Also

| | | | | | |

Related Topics