Generate C Messages to Communicate Data Between Simulink Components
To generate C code that supports message-based communication between model components in the Simulink environment, use the Simulink Messages & Events Library blocks Send and Receive. To customize the communication, use the Queue block (from the same library) to set parameters for capacity, sorting policy (LIFO, FIFO, and priority), and overwriting policy (behavior when the queue exceeds capacity). You can generate C code for GRT-based system target files by using Simulink Coder or for ERT-based system target files by using Embedded Coder.
Messages are an effective communication technique for distributed and complex systems that you can model within Simulink.
How to Prepare Models in Simulink for Message-Based Communication
In Simulink, you can model message-based communication between model components:
Create a model that contains a Send block (referenced model).
Create a model that contains a Receive block (referenced model).
Create a model that has two Model blocks (top model).
Set the first Model block to the model that contains the Send block (model from step 1).
Set the second Model block to the model that contains the Receive block (model from step 2).
When you run the model, a queue generates in the top model above the message line. A queue, explicit or implicit, controls the message communication. You can use the generated queue or you can add a Queue block in the top model to explicitly specify communication parameters.
How Model Components Pass Messages and How the Code Implements the Behavior
Referenced models pass messages:
In the model that contains the Send block, the Send block converts signals into messages.
The top model that contains the queue manages messages according to parameters that define capacity, order of delivery, and other quality of service (QoS) metrics.
In the model that contains the Receive block, the Receive block converts messages back to signals.
In the generated C code, the top model facilitates the connection between the send and receive referenced models by establishing a set interface that the referenced models can access independently of one another.
The generated C code implements message behavior:
A service is created at each model boundary. A service contains a reference to the top model and an entry point function, referred to as a service function, for referenced models to use to pass messages to the top model.
The top model initializes each service to create a connection to each referenced model.
The referenced models invoke service functions to pass messages to the top model.
C Code Generation Example
This example generates and examines C code from the model provided in Establish Message Send and Receive Interfaces Between Software Components.
Generate C Code:
Open the model.
In the Apps gallery, click Embedded Coder.
For each model (top and both referenced models), in the Configuration Parameters dialog box, in the Code Generation pane, set Language to
C
and save the model.Generate code. On the C Code tab, click Build.
View the generated code. On the C Code tab, click View Code.
Examine C Code:
A service is created at each model boundary. In C, referenced models represent services as DWork. The service provides a pointer to an instance of the top model and a service function that referenced models invoke to pass messages.
To view the service to send messages, open the send referenced model header file
mSend.h
. View the DWork allocation.To view the send message data type, if the data type is shareable (for example, built-in data types, imported bus types, or exported bus types that have a specified data type) the information is located in a shared header file. If the data type is not shareable, the information is located the model header file
mSend.h
. For this example, view the shareable data type by opening the shared header fileSendData_real_T.h
.To view the service to receive messages, open the receive referenced model header file,
mReceive.h
. View the DWork allocation.To view the received messages data type, if the data type is shareable (for example,built-in data types, imported bus types, or exported bus types that have a specified data type) the information is located in a shared header file. If the data type is not shareable the information is located the model header file,
mReceive.h
. For this example, view the shareable data type by opening the shared header fileRecvData_real_T.h
.
The top model initializes each service to create a connection to each referenced model. In C, the top model initializes each referenced model DWork. To view the initialization, open the top model C file
MessageSendReceiveDefaultBufferModel.c
.The referenced models invoke service functions to pass messages to the top model. In C, a referenced model invokes a service function by dereferencing the service function pointer and passing a pointer to the instance of the top model. You can view the prototypes of the service functions in the top model header file. You can view the referenced model invocations of those service functions in the referenced model C files.
To view the prototype of the service function to send messages, open the top model header file,
MessageSendReceiveDefaultBufferModel.h
.To view the implementation of the service function to send messages, open the top model C file,
MessageSendReceiveDefaultBufferModel.c
.To view how the send referenced model invokes the service function, open the C file for the model
mSend.c
. In the step function, the model invokes the service to send instance data and a message to the top model and receives back a return status.To view the prototype of the service function to receive messages, open the top model header file
MessageSendReceiveDefaultBufferModel.h
.To view the implementation of the service function to receive messages, open the top model C file
MessageSendReceiveDefaultBufferModel.c
.To view how the receive referenced model invokes the service function, open the C file for the model
mReceive.c
. In the step function, the model invokes the service to receive a message payload and a return status.
Considerations and Limitations
C code support is available for GRT-based system target files by using the Simulink Coder App.
C code support is available for ERT-based system target files by using the Embedded Coder App.
To generate code, for top and referenced models, select the same language (C) and the same system target file.
You cannot configure function prototype control (FPC) for a top model that has root message ports.
External models and variant models are not supported.
Software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulations are not supported.