Communicate with Stateflow Charts by Sending Messages - MATLAB & Simulink (original) (raw)

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 containing data. In the receiving chart, a queue receives the message and holds it until the chart can evaluate it.

Simulink model with two Stateflow charts that communicate using a message.

Messages combine some of the functionality of data and events. Like data, messages can transmit numeric and textual information. Like events, messages can trigger transition and state actions. However:

For more information, see View Differences Between Stateflow Messages, Events, and Data.

When a chart transition or state action evaluates a 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 Symbols pane, the Stateflow Editor menu, or the Model Explorer.

Add Messages Through the Symbols Pane

  1. In the Modeling tab, under Design Data, select Symbols Pane.
  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 pane, right-click the row for the message and select Explore. For more information, see Set Properties for a Message.

Add Messages by Using the Stateflow Editor Menu

  1. In the Stateflow Editor, select the option corresponding to the scope of the message that you want to add.
    Scope Option
    Input In the Modeling tab, under Design Data, select Message Input.
    Output In the Modeling tab, under Design Data, select Message Output.
    Local In the Modeling tab, under Design Data, select Message.
  2. In the Message dialog box, specify data properties. For more information, seeSet Properties for a Message.

Add Messages Through the Model Explorer

  1. In the Modeling tab, under Design Data, select 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 > . 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 a receiving 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:

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:

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

Limitations for Messages

You cannot use messages in:

In charts that use C as the action language, messages do not support multiword fixed-point data.

Charts that contain local messages do not support external mode simulation or rapid accelerator mode simulation.

See Also

discard | forward | isvalid | receive | send | Queue (Simulink) | Sequence Viewer

Topics