| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Vehicle Network Toolbox |
| Contents | Index |
| Learn more about Vehicle Network Toolbox |
| On this page… |
|---|
This section provides an example that builds a simple model using the Vehicle Network Toolbox blocks with other blocks in the Simulink library. The 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 a CAN Transmit block to send the data to the virtual CAN channel.
Use this section in combination with the Build a Message Receive Model, and the Save and Run The Model to build your complete model and run the simulation.
To open the Vehicle Network Toolbox block library, start the Simulink Library Browser.
To start the Simulink Library Browser, enter
simulink
at 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 entry icon. Then, click CAN Communication to open the CAN blocks. See Using the Simulink Library Browser for more information.
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 the File menu in the Simulink Library Browser and select New > Model. Simulink opens an empty model window on the display. To name the new model, use the Save option.
To use the blocks in a model, click a block in the library and, holding the mouse button down, drag it into the model window. For this example, you need one instance each of the CAN Configuration, CAN Pack, and the CAN Transmit block in your model.
Drag Vehicle Network Toolbox Blocks into Model Window

This example requires a source block that feeds data to the CAN Pack block. Add a Constant block into your model.
Drag Constant Block to the Model Window

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.
You set parameters for the blocks in your model by double-clicking on 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 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:

This section provides an example that builds a simple model using the Vehicle Network Toolbox blocks with other blocks in the Simulink library. The example illustrates how to receive data via a CAN network.
Use a virtual CAN channel to receive messages.
You use the CAN Configuration block to configure your virtual CAN channels.
Use the CAN Receive block to receive the message sent by the blocks built in Build a Message Transmit Model.
Use a Function–Call Subsystem block that contains the CAN Unpack block. This function takes in the data from the CAN Receive block and uses the parameters of the CAN Unpack to unpack your message data.
Use a Scope block to show the transfer of data visually.
Use this section in combination with the Build a Message Transmit Model, and the Save and Run The Model to build your complete model and run the simulation.
For this example, you need one instance each of the CAN Configuration, the CAN Receive, and the CAN Unpack block 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.
Drag Vehicle Network Toolbox Blocks into Model Window

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

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

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.
Drag The Scope Block into Model Window

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

Open the Function–Call Subsystem block and:
Double-click on In1 to rename it to CAN Msg.
Double-click on Out1 to rename it to data.
Rename the Function–Call Subsystem block to CAN Unpack Subsystem.
Connect the f() output port on the CAN Receive block to the function() input port on the Function–Call Subsystem block.

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.
You set parameters for the blocks in your model by double-clicking on 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 model. 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:

This section shows you how to save the models you have built in the previous two sections, Build a Message Transmit Model and Build a Message Receive Model.
Before you run the simulation, save your model by clicking the Save icon or selecting File > Save from the menu.
In your model window, select Simulation > Configuration Parameters. The Configuration Parameters dialog box opens.
In the Solver Options section, select :
Fixed-step from the Type list.
Discrete (no continuous states) from the Solver list.
To run the simulation, click the Start button on the model window toolbar. Alternatively, you can use the Simulation menu in the model window and choose the Start 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.

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

If you are not able to 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 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 Message Receive model, these values are received and unpacked. The output in the scope window represents the received data values.
![]() | Opening the Vehicle Network Toolbox Block Library | Function Reference | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |