Model a Message Receive Interface that Runs on Message Availability
This example shows how to synchronize execution with data availability in message-based systems. We show how to model this behavior using events and actions. This example builds on another example, Establish Message Send and Receive Interfaces Between Software Components, where two software components communicate using messages.
As with the previous example, the code generation process should apply to the SendComponent and the ReceiveComponent. The Message Storage models the middleware and data notification events provided by the middleware.
Below is the composition model with a SendComponent and a ReceiveComponent created by using two Model blocks. These two Model blocks are connected through a Queue block with message lines.
When a message arrives at the Queue block, its arrival is communicated to the ReceiveComponent by using Queue entry event action. This activates the Simulink Function block inside the ReceiveComponent to accept a new message for processing.
To achieve this behavior, in the Queue block, in the Event action tab, in the Entry field, the block calls the Simulink Function
onMessageAvailable(). See Event Action Languages and Random Number Generation (SimEvents) for more information on Event Actions.
When the The Simulink Function block is activated, it accepts a message and converts it to a signal.
As a result, messages sent from the SendComponent trigger execution within the ReceiveComponent. To observe this behavior, use the Sequence Viewer block.
For example, observe the simulation time
The arrow from the Send block to the Queue block represents that a message is sent with a data value of
The arrow from the Queue block to the Simulink Function block indicates a call to the
An arrow from the Queue block illustrates that the message with data value
0is received by the Receive block within this function call.
The horizontal, dashed arrow from the Simulink Function block to the Queue block indicates the return of function
For more information about the Sequence Viewer block, see Use the Sequence Viewer to Visualize Messages, Events, and Entities.
- Simulink Messages Overview
- Establish Message Send and Receive Interfaces Between Software Components
- Generate C Messages to Communicate Data Between Simulink Components (Embedded Coder)
- Generate C++ Messages to Communicate Data Between Simulink Components (Embedded Coder)
- Generate C++ Messages to Communicate Data Between Simulink and an Operating System or Middleware (Embedded Coder)
- Model Message-Based Communication Integrated with POSIX Message Queues (Embedded Coder)