Observe Messages
Simulink® uses messages to communicate between model components. When your model includes one or more message signals, you can create an Observer model to observe the message data. When you create the Observer model, it creates metadata associated with the message. You can test the message semantics and verify the message properties by using both the message data and metadata.
Message Bus Elements
When you create an Observer for a message signal, the Observer model automatically includes an Observer Port block and a Bus Selector block. The bus has two elements:
OrigPayload
— The message data being observed.Metadata with these elements:
sltestEventMetadata.Message.id
— ID of message being observed, returned as anint32
integer.sltestEventMetadata.Message.order
— Order of message action in the simulation, returned as an int32 integer.sltestEventMetadata.Message.eventType
— The event type, returned asslTestEventType
object. Valid values areMessageArrival
,MessageDeparture
,MessageDrop
, andInvalid
.sltestEventMetadata.Message.time
— Simulation time when the message was sent, received, or dropped, returned as adouble
.
Add a Message Observer
To add an observer for messages:
In the model that has one or more blocks that output messages, right-click the output message signal to observe.
Note
Messages from blocks with asynchronous sample times are supported only for SimEvents® blocks that generate their own sample times.
Select Observers > Observe selected signals > New Observer to create an Observer model for the message signal. An Observer Reference block is added to the main model and an Observer model is created.
The new Observer model contains an Observer Port block and Bus Selector block. The Bus Selector block has two outputs,
OrigPayload
andMetadata
. You can use the default settings in the Observer Port block to observe the outputs from the bus.Connect the outputs from the bus to a block, such as a Test Assessment block or a Stateflow® chart, to specify the logic to analyze or verify the message data or metadata.
Observe a Message Signal
Open a model that has a block with message output. For this example, open the Overflow
model, which contains an Observer model.
open_system('Overflow')
In this model, the Sine Wave2
block sends data every 0.1 seconds, but the Receive2
block receives data only every 0.5 seconds. Because the Queue2 FIFO
block holds a maximum of three messages, an overflow occurs and some messages are dropped from the queue.
The Observer model was set up by selecting the message signals, right-clicking on one of them, and selecting Observers > Observe selected signals > New Observer. The Observer block, which is an Observer Reference block, was automatically added to the main model.
Open the Observer model by double-clicking the Observer block in the main model, or by using this command:
open_system('Overflow_Observer_1')
The Observer model contains two Observer Port blocks, Send
and Receive
, which correspond to the signals selected in the main model. Each of these blocks is connected to a Bus Selector block with two outputs. The OrigPayload
output is the message data being observed and the Metadata
output contains the message ID, order of the message in the simulation, the message event type, and the simulation time of the message.
The bus signal outputs connect to two Stateflow charts, which analyze the message data.
The chart for the messages from the Receive
Observer port block contains logic that verifies that the message data and metadata are not corrupted due to messages being dropped because of Queue block overflows.
The chart logic verifies that:
Sine wave data is in the expected range, which is between 0 and 1
The order of the messages and simulation time increments and that each order value is unique
Message event type is a valid type
See Also
Observer Port | Observer Reference