Mux and Transmit Multiple CAN Messages
This example shows how to use MathWorks® virtual CAN channels to transmit and receive multiple CAN messages simultaneously in Simulink®. The virtual channels are connected in a loopback configuration.
This example uses the CAN Configuration, CAN Pack, CAN Transmit, CAN Receive, and CAN Unpack blocks from the Vehicle Network Toolbox™. Additionally, this example uses the Mux (Simulink) block to combine the CAN messages to be transmitted into a single vector. If your CAN Pack blocks are configured to output as a bus, then use the Vector Concatenate (Simulink) block to combine the CAN messages instead.
Transmit and Receive Muxed CAN Messages
The included model demonstrates how to:
Generate three signals.
Pack the signals into separate CAN messages with unique CAN IDs.
Combine the CAN messages into a single vector.
Transmit the CAN messages simultaneously through the first virtual channel.
Receive the CAN messages on the second virtual channel and unpack them separately based on their CAN IDs.
Process CAN Messages
The CAN Receive block generates a function-call trigger if it receives a new message at any particular timestep. This function-call trigger can be connected to a Function-Call Subsystem (Simulink), which can then be responsible for signal decoding and processing. Within the subsystem, each individual CAN message is unpacked separately with three CAN Unpack blocks, with each block responsible for unpacking the message with the specified CAN ID.
Visualize Signal Data
Plot the three signal values before and after transmission. The X-axis corresponds to the simulation timestep and the Y-axis corresponds to the value of the signal. The phase shift between the plots on the top (transmitted signals) and the plots on the bottom (received signals) represents the propagation delay as the signal travels across the network.
Extend the Example
This example uses MathWorks virtual CAN channels. You can connect your models to other supported hardware. You can also modify the model to transmit additional signals simultaneously.