Send Messages with String Data
This example shows how to configure a pair of Stateflow® charts that communicate by sending messages that carry string data. For more information, see Communicate with Stateflow Charts by Sending Messages.
This model contains two Stateflow charts. During simulation, the Emitter
chart reads an input string key
from the String Constant block and sends a message to the Receiver
chart. The message data consists of the input string key
. The Receiver
chart compares the string with a constant keyword and returns an output string that grants or denies access.
Emitter Chart
The Emitter
chart consists of a single state. When the state becomes active, it sets the data for the output message M
to the input value key
and sends the message to the Receiver
chart.
In this chart, key
has type Inherit: Same as Simulink
while M
has type string
.
Receiver Chart
The Receiver
chart consists of two states joined by a transition. The input message M
guards the transition. If there is a message present and its data value equals the constant string lock, then the state activity transitions from state Off
to state On
. The chart outputs the string value "Access Granted"
. If there is no message present, or if the data value does not equal lock
, the chart does not take the transition and the output value is "Access Denied"
.
In this chart, M
has type Inherit: Same as Simulink
, while lock
and sout
have type string
. The constant string lock
contains a secret password, initially set to "Open Sesame"
. You can change the value of lock in the Value field of the Property Inspector.
View Simulation Results
During simulation, the model responds to the password that you enter in the String Constant block:
If you enter an incorrect password, such as
"Abracadabra"
, the model displays the output string"Access Denied"
.
If you enter the correct password, in this case,
"Open Sesame"
, the model displays the output string"Access Granted"
.