Communicate with Stateflow Charts by Sending Messages

To communicate within and between Stateflow® charts in a Simulink® model, use messages. A message is a Stateflow object that communicates data locally or between charts. From a sender chart, you can send or forward a message. In the receiving chart, a queue receives the message and holds it until the chart can evaluate it.

Messages are queued until the chart wakes up. When the chart wakes up, it can respond to the messages in the queue.

  • Messages do not trigger charts to wake up.

  • Messages are not lost when the receiver chart cannot immediately respond.

When a chart transition or state action evaluates the message, the chart determines if the queue contains any messages. If it does, the chart removes the message from the queue. The message remains valid until the end of the time step or until the chart forwards or discards it. While the message is valid, other transitions or actions can access the message data and the chart does not remove another message from the queue. The chart destroys all valid messages at the end of the current time step.

Define Messages in a Chart

You can add messages to a Stateflow chart by using the Chart menu in the Stateflow Editor, through the Symbols window, or through the Model Explorer.

Add Messages by Using the Stateflow Editor Menu

  1. In the Stateflow Editor, select the menu option corresponding to the scope of the message that you want to add.

    ScopeMenu Option
    InputChart > Add Inputs & Outputs > Message Input From Simulink
    OutputChart > Add Inputs & Outputs > Message Output To Simulink
    LocalChart > Add Other Elements > Local Message

  2. In the Message dialog box, specify data properties. For more information, see Set Properties for a Message.

Add Messages Through the Symbols Window

  1. To open the Symbols window, select View > Symbols.

  2. Click the Create Message icon .

  3. In the row for the new message, under TYPE, click the icon and choose:

    • Input Message

    • Local Message

    • Output Message

  4. Edit the name of the message.

  5. For input and output messages, click the PORT field and choose a port number.

  6. To specify properties for the message, open the Property Inspector. In the Symbols window, right-click the row for the message and select Explore. For more information, see Set Properties for a Message.

Add Messages Through the Model Explorer

  1. In the Stateflow Editor, select View > Model Explorer.

  2. In the Model Hierarchy pane, select the object in the Stateflow hierarchy where you want to make the new message visible. The object that you select becomes the parent of the new message.

  3. In the Model Explorer menu, select Add > Message. The new message with a default definition appears in the Contents pane of the Model Explorer.

  4. In the Message pane, specify the properties of the message. For more information, see Set Properties for a Message.

Lifetime of a Stateflow Message

A Stateflow message has a finite lifetime. The lifetime begins when you send a message to an input or local queue with the send operator. The message remains in the queue until a transition or state on action evaluates it or the chart receives it by using the receive operator.

A message becomes valid when a chart evaluates or receives it. The message remains valid until:

  • The end of the current time step, when the chart destroys any remaining valid messages.

  • The chart forwards the message to another queue. The message continues its lifetime in the receiving queue.

  • The chart discards the message.

While a message is valid, other transitions and actions can evaluate the message and access its data. To check if a message is valid, use the isvalid operator.

To view the interchange of messages during simulation, add a Sequence Viewer block to your Simulink model. The Sequence Viewer block displays:

  • Sent messages

  • Received messages

  • Forwarded messages

  • Dropped messages

  • Destroyed messages

  • Discarded messages

For more information, see Use the Sequence Viewer Block to Visualize Messages, Events, and Entities.

Limitations for Messages

You cannot use messages in:

  • Moore charts

  • Atomic subcharts

  • Breakpoint condition expressions

  • Model reference inputs and outputs

Messages do not support fixed-point data.

Related Examples

More About