Use Messages in Test Sequences
Messages carry data between Test Sequence blocks and other blocks such as Stateflow® charts. Messages can be used to model asynchronous events. A message is queued until you evaluate it, which removes it from the queue. You can use messages and message data inside a test sequence. The message remains valid until you forward it, or the time step ends. For more information, see Messages in the Stateflow® documentation.
Receive Messages and Access Message Data
If your Test Sequence block has a message input, you can use queued messages in test sequence actions or transitions. Use the receive command before accessing message data or forwarding a message.
To create a message input, hover over Input in the Symbols sidebar, click the add message icon, and enter the message name.
receive(M) determines whether a message is present in the input queue M, and removes the message from the queue. receive(M) returns true if a message is in the queue, and false if not. Once the message is received, you can access the message data using the dot notation, M.data, or forward the message. The message is valid until it is forwarded or the current time step ends.
The order of message removal depends on the queue type. Set the queue type using the message properties dialog box. In the Symbols sidebar, click the edit icon next to the message input, and select the Queue type. For more information see Queuing Behavior of Stateflow Messages.
To send a message, create a message output and use the send command. To create a message output, hover over Output in the Symbols sidebar, click the add message icon, and enter the message name.
You can assign data to the message using the dot notation M.data, where M is the message output of the Test Sequence block. send(M) sends the message.
You can forward a message from an input message queue to an output port. To forward a message:
- Receive the message from the input queue using receive.
- Forward the message using the command forward(M,M_out) where M is the message input queue and M_out is the message output.
Compare Test Sequences Using Data and Messages
This example demonstrates message inputs and outputs, sending, and receiving a message. The model compares two pairs of test sequences. Each pair is comprised of a sending and receiving test sequence block. The first pair sends and receives data, and the second sends and receives a message.
Set the following path and model name variables.
filePath = fullfile(matlabroot,'examples','simulinktest'); model = 'sltest_testsequence_data_vs_message';
Open the model.
Test Sequences Using Data
The DataSender block assigns a value to a data output M.
The DataReceiver block waits 3 seconds, then transitions to step S2. Step S2 transitions to step S3 using a condition comparing M to the expected value, and does the same for S3 to S4.
Test Sequences Using Messages
The MessageSender block assigns a value to the message data of a message output M_out, then sends the message to the MessageReceiver block.
The MessageReceiver block waits 3 seconds, then transitions to step S2. Step S2's transition evaluates the queue M with receive(M), removing the message from the queue. receive(M) returns true since the message is present. M.data == 3.5 compares the message data to the expected value. The statement is true, and the sequence transitions to step S3.
When step S3's transition condition evaluates, no messages are present in the queue. Therefore, S3 does not transition to S4.
Run the test and observe the output comparing the different behaviors of the test sequence pairs.
open_system([model '/Scope']) sim(model)