Documentation Center

  • Trials
  • Product Updates

Build CAN Communication Simulink Models

Build a Message Transmit Model

This section provides an example that builds a simple model using Vehicle Network Toolbox™ blocks with other blocks in the Simulink® library. This example illustrates how to send data via a CAN network.

  • Use virtual CAN channels to transmit messages.

  • Use the CAN Configuration block to configure your CAN channels.

  • Use the Constant block to send data to the CAN Pack block.

  • Use the CAN Transmit block to send the data to the virtual CAN channel.

Use this section with Build a Message Receive Model and Save and Run the Model to build your complete model and run the simulation.

Step 1: Open the Block Library

To open the Vehicle Network Toolbox block library, start the Simulink Library Browser by entering:

simulink

in the MATLAB® Command Window. The left pane in the Simulink Library Browser lists the available block libraries. To open the Vehicle Network Toolbox block library, click its icon. Then click CAN Communication to open the CAN blocks. See Using the Simulink Library Browser for more information.

Step 2: Create a New Model

To use a block, add it to an existing model or create a model.

For this example, create a model by clicking the New model button on the toolbar.

You can also select File > New > Model from the Simulink Library Browser. Simulink opens an empty editor. To name the new model, use the Save option.

Step 3: Drag Vehicle Network Toolbox Blocks into the Model

To use the blocks in a model, click a block in the library and, holding the mouse button down, drag it into the editor. For this example, you need one instance each of the CAN Configuration, CAN Pack, and CAN Tarnsmit blocks in your model.

Step 4: Drag Other Blocks to Complete the Model

This example requires a source block that feeds data to the CAN Pack block. Add a Constant block to your model.

Step 5: Connect the Blocks

Make a connection between the Constant block and the CAN Pack block. When you move the pointer near the output port of the Constant block, the pointer becomes a cross hair. Click the Constant block output port and, holding the mouse button, drag the pointer to the input port of the CAN Pack block. Then release the button.

In the same way, make a connection between the output port of the CAN Pack block and the input port of the CAN Transmit block.

The CAN Configuration block does not connect to any other block. This block configures the CAN channel used by the CAN Transmit block to transmit the packed message.

Step 6: Specify the Block Parameter Values

You set parameters for the blocks in your model by double-clicking the block.

Configure the CAN Configuration Block.  Double-click the CAN Configuration block to open its parameters dialog box. Set the:

  • Device to Vector Virtual 1 (Channel 1)

  • Bus speed to 500000

  • Acknowledge Mode to Normal

Click OK.

Configure the CAN Pack Block.  Double-click the CAN Pack block to open its parameters dialog box. Set the:

  • Data is input as to raw data

  • Name to the default value CAN Msg

  • Identifier type to the default Standard (11-bit identifier) type

  • Identifier to 500

  • Length (bytes) to the default length of 8

Click OK.

Configure the CAN Transmit Block.  Double-click the CAN Transmit block to open its parameters dialog box. Set Device to Vector Virtual 1 (Channel 1). Click Apply, then OK.

Configure the Constant Block.  Double-click the Constant block to open its parameters dialog box. On the Main tab, set the:

  • Constant value to [1 2 3 4 5 6 7 8]

  • Sample time to 0.01 seconds

On the Signal Attributes tab, set the Output data type to uint8. Click OK.

Your model looks like this figure.

Build a Message Receive Model

This section provides an example that builds a simple model using the Vehicle Network Toolbox blocks with other blocks in the Simulink library. This example illustrates how to receive data via a CAN network.

Use this section with Build a Message Transmit Model and Save and Run the Model to build your complete model and run the simulation.

Step 7: Drag Vehicle Network Toolbox Blocks into the Model

For this example, you need one instance each of the CAN Configuration, CAN Receive, and CAN Unpack blocks in your model. However, you add only the CAN Configuration and the CAN Receive blocks here. Add the CAN Unpack block into the Function–Call Subsystem described in Step 8: Drag Other Blocks to Complete the Model.

    Tip   Configure a separate CAN channel for the CAN Receive and CAN Transmit blocks.

Step 8: Drag Other Blocks to Complete the Model

Use the Function–Call Subsystem block from the Simulink Ports & Subsystems block library to build your CAN Message pack subsystem.

  1. Drag the Function–Call Subsystem block into the model.

  2. Double-click the Function–Call Subsystem block to open the subsystem editor.

  3. Drop the CAN Unpack block from the Vehicle Network Toolbox block library in this subsystem.

To see the results of the simulation visually, drag the Scope block from the Simulink block library into your model.

Step 9: Connect the Blocks

  1. Connect the CAN Msg output port on the CAN Receive block to the In1 input port on the Function–Call Subsystem block.

  2. Open the Function–Call Subsystem block and:

    • Double-click In1 to rename it to CAN Msg.

    • Double-click Out1 to rename it to data.

  3. Rename the Function–Call Subsystem block to CAN Unpack Subsystem.

  4. Connect the f() output port on the CAN Receive block to the function() input port on the Function–Call Subsystem block.

  5. Connect the CAN Unpack Subsystem output port to the input port on the Scope block.

Your model looks like this figure.

    The CAN Configuration block does not connect to any other block. This block configures the CAN channel used by the CAN Receive block to receive the CAN message.

    Step 10: Specify the Block Parameter Values

    Set parameters for the blocks in your model by double-clicking the block.

    Configure the CAN Configuration1 Block.  Double-click the CAN Configuration block to open its parameters dialog box. Set the:

    • Device to Vector Virtual 1 (Channel 2)

    • Bus speed to 500000

    • Acknowledge Mode to Normal

    Click OK.

    Configure the CAN Receive Block.  Double-click the CAN Receive block to open its Parameters dialog box. Set the:

    • Device to Vector Virtual 1 (Channel 2)

    • Sample time to 0.01

    • Number of messages received at each timestep to all

    Click OK.

    Configure the CAN Unpack Subsystem.  Double-click the CAN Unpack subsystem to open the Function–Call Subsystem editor. In the model, double-click the CAN Unpack block to open its parameters dialog box. Set the:

    • Data to be output as to raw data

    • Name to the default value CAN Msg

    • Identifier type to the default Standard (11-bit identifier) type

    • Identifier to 500

    • Length (bytes) to the default length of 8

    Click OK.

    Your subsystem looks like this figure.

    Save and Run the Model

    This section shows you how to save the models you built, Build a Message Transmit Model and Build a Message Receive Model.

    Step 11: Save the Model

    Before you run the simulation, save your model by clicking the Save icon or selecting File > Save from the menu bar.

    Step 12: Change Configuration Parameters

    1. In your model window, select Simulation > Model Configuration Parameters. The Configuration Parameters dialog box opens.

    2. In the Solver Options section, select:

      • Fixed-step from the Type list.

      • Discrete (no continuous states) from the Solver list.

    Step 13: Run the Simulation

    To run the simulation, click the Run button on the model window toolbar. Alternatively, you can use the Simulation menu in the model window and choose the Run option.

    When you run the simulation, the CAN Transmit block gets the message from the CAN Pack block. It then transmits it via Virtual Channel 1. The CAN Receive block on Virtual Channel 2 receives this message and hands it to the CAN Unpack block to unpack the message.

    While the simulation is running, the status bar at the bottom of the model window updates the progress of the simulation.

    Step 14: View the Results

    Double-click the Scope block to view the message transfer on a graph.

    If you cannot see all the data on the graph, click the Autoscale toolbar button, which automatically scales both axes to display all stored simulation data.

    In the graph, the horizontal axis represents the simulation time in seconds and the vertical axis represents the received data value. In the Message Transmit model, you configured blocks to pack and transmit an array of constant values, [1 2 3 4 5 6 7 8], every 0.01 second of simulation time. In the Message Receive model, these values are received and unpacked. The output in the Scope window represents the received data values.

    Was this topic helpful?