Message-based communication is necessary in various applications, such as control system architectures in which centralized architectures are replaced with distributed architectures due to the complexity of the systems. In a distributed architecture, multiple components of the system communicate via a shared network.
A distributed architecture has these three elements:
Component — Represents partitions of a design that performs a set of functionalities or algorithms with defined I/O interfaces. Generally, components generate events and data asynchronously.
Interface — Provides a shared boundary through which components of the system communicate. To provide asynchronous communication, messages are useful modeling artifacts that combine events with related data.
Middleware — Provides the services needed by the components to support asynchronous communication across the shared network.
Below is an illustration that shows the composition of a distributed architecture and its elements.
When modeling such an architecture, you typically model components that are clearly identifiable, reusable, and deployable. To achieve asynchronous event-based communication between components, use message send and receive interfaces. Model the middleware to facilitate the network topology that represents the connectivity of components, such as one-to-many, many-to-one, or many-to-many based on the number of message sending and receiving components. For an example, see Build a Shared Communication Channel with Multiple Senders and Receivers.
To learn how to model a distributed architecture, using Simulink®, SimEvents®, and Stateflow®, see the illustration below. The illustration includes two message sending and two message receiving components that are created as referenced models. Model components with send and receive interfaces using Simulink Send and Receive blocks. If your send and receive interfaces involve states or require decision logic, use a Stateflow chart.
After you model your components and interfaces:
Simulate the behavior of your distributed architecture by modeling the middleware using SimEvents. Using the blocks from the SimEvents library, you can model custom routing and communication patterns, such as merging, delaying, distributing, and broadcasting messages, and investigate the effects of middleware on your communication network.
Generate code for your components, including the interface, and connect to your middleware or an operating system communication API.
Let us start by understanding how message blocks work. To create a model that uses messages, use Send blocks to convert data and send messages and Receive blocks to receive and convert messages to data. For a simple example that shows how Send and Receive blocks work, see Animate and Understand Sending and Receiving Messages.
Use Send and Receive blocks to model message send and receive interfaces for your components. For a simple example that shows the basics of creating send and receive interfaces, see Establish Message Send and Receive Interfaces Between Software Components. To learn how to generate code for the same model, see Generate C++ Messages to Communicate Between Simulink Components (Embedded Coder).
You can further modify send and receive interfaces for custom behavior. For example, you can synchronize when a receive interface executes to when data is available. For more information, see Model a Message Receive Interface that Runs on Message Availability.
After modeling, generate code for your send and receive interfaces and connect them to the middleware or an operating system communication API. For an example that generates code for a top model and allows your application to communicate in a distributed system that uses an external message protocol service (for example, DDS, ROS, SOMEIP, or POSIX messages), see Generate C++ Messages to Communicate Between Simulink and an Operating System or Middleware (Embedded Coder).
Use Queue blocks to store, sort and queue messages. The Queue block allows you to specify message storage capacity and the overwriting and sorting policies for message transitions. For a simple example that shows how a Queue block works, see Use a Queue Block to Manage Messages.
You can also use SimEvents to model and simulate middleware effects on your communication network. Use the blocks provided by the SimEvents library to model message routing, peer-to-peer communication, wireless communication, packet loss, and channel delays. For more information about SimEvents, see Discrete-Event Simulation in Simulink Models (SimEvents).
For basic communication patterns that can be modeled by SimEvents, see Modeling Message Communication Patterns with SimEvents. You can use combinations of these patterns to create more complex communication behavior. For an example of a system with multiple message sending and receiving components and an ideal shared channel with delay, see Build a Shared Communication Channel with Multiple Senders and Receivers. To see a model with shared wireless channel with channel failure and packet loss, see Model Wireless Message Communication with Packet Loss and Channel Failure.
To see an example that shows how to model more complex network behavior, such as an Ethernet communication network with CSMA/CD protocol, see Model an Ethernet Communication Network with CSMA/CD Protocol.
SimEvents blocks do not support code generation.