Control Message Activity in Stateflow Charts
A message is a Stateflow® object that communicates data locally or between charts in a Simulink® model. 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.
Using Stateflow operators, you can access message data, and send, receive, discard, or forward a message. You can also determine whether a message is valid and find the number of messages in a queue. For more information, see Communicate with Stateflow Charts by Sending Messages.
Access Message Data
Stateflow messages have a data field. To read or write to the message data field of a valid message, use dot notation syntax:
message_name.data
enumeration
section of the definition, unless you specify otherwise in
the methods
section of the definition.You cannot access message data for messages that are still in the queue or that have already been discarded.
Send a Message
To send an output or local message, use the send
operator:
send(message_name)
A
sends a message
M
with a data value of 3
. If the message scope is
Local
, then the message goes in the local receiving queue. If the
message scope is Output
, then the chart sends the message through the
output port to another block in the Simulink model.
In a single time step, you can send multiple messages through an output port or to a local receiving queue.
If a chart sends a message that exceeds the capacity of the receiving queue, a queue overflow occurs. The result of the queue overflow depends on the type of receiving queue.
When an overflow occurs in an internal queue, the Stateflow chart drops the new message. You can control the level of diagnostic action by setting the Queue Overflow Diagnostic property for the message. See Queue Overflow Diagnostic.
When an overflow occurs in an external queue, the Queue (Simulink) block either drops the new message or overwrites the oldest message in the queue, depending on the configuration of the block. See Overwrite the oldest element if queue is full (Simulink). An overflow in an external queue always results in a warning.
Guard Transitions and Actions
Messages can guard transitions or state actions of type on
. During a
time step, when the guarding message is evaluated for the first time, the chart removes the
message from the queue and makes the message valid. While the message is valid, other
transitions or actions can access the message data but they do not remove another message
from the queue.
Guard a Transition with a Message
In this chart, a message M
guards the transition from state
A
to state B
. The transition occurs when both of
these conditions are true:
A message is present in the queue.
The data value of the message is equal to 3.
If a message is not present or if the data value is not equal to 3, then the transition does not occur. If a message is present, it is removed from the queue regardless of whether the transition occurs.
Guard a State on
Action with a Message
In this chart, a message M
guards the on
action
in state A
. When state A
becomes active, it
increments the value of x
if both of these conditions are true:
A message is present in the queue.
The data value of the message is equal to 3.
If a message is not present or if the data value is not equal to 3, then
the value of x
does not change. If a message is present, it is removed
from the queue regardless of whether x
is modified.
Receive a Message
To extract an input or local message from its receiving queue, use the receive
operator:
receive(message_name)
M
exists, receive(M)
returns
true
. If a valid message does not exist but there is a message in the
queue, then the chart removes the message from the queue and receive(M)
returns true
. If a valid message does not exist and there are no messages
in the queue, receive(M)
returns false
.For example, in this chart, the during
action in state
A
checks the queue for message M
and increments the
value of x
if both of these conditions are true:
A message is present in the queue.
The data value of the message is equal to 3.
If a message is not present or if the data value is not equal to 3, then the
value of x
does not change. If a message is present, the chart removes it
from the queue regardless of the data value.
Discard a Message
To discard a valid input or local message, use the discard
operator:
discard(message_name)
For example, in this chart, the during
action in state
A
checks the queue for message M
. If a message is
present, the chart removes it from the queue. If the message has a data value equal to 3,
the chart discards the message.
Forward a Message
To forward a valid input or local message to a local queue or an output port, use the
forward
operator:
forward(message_in_name,message_out_name)
Forward an Input Message
In this chart, state A
checks the input queue for message
M_in
. If a message is present, the chart removes the message from the
queue and forwards it to the output port M_out
. After the chart
forwards the message, the message is no longer valid in state A
.
Forward a Local Message
In this chart, the transition between state A
and state
B
checks the local queue for message M_local
. If a
message is present, the transition removes the message from the M_local
message queue and forwards it to the output port M_out
.
Determine if a Message Is Valid
To check if an input or local message is valid, use the isvalid
operator:
isvalid(message_name)
For example, this chart first executes state A
, as described in Discard a Message. When
the chart executes state B
, the during
action checks
that the message M
is valid. If the message is valid and has a data value
equal to 6, the chart discards the message.
Determine the Length of the Queue
To check the number of messages in an internal receiving queue of an input or local
message, use the length
operator:
length(message_name)
For example, in this chart, the during
action in state
A
checks the queue for message M
. If a message is
present, the chart removes it from the queue. If exactly seven messages remain in the queue,
the chart increments the value of x
.
The length
operator is not supported for input messages that use
external receiving queues managed by a Queue (Simulink) block.
See Also
discard
| forward
| isvalid
| length
| receive
| send
| Queue (Simulink)