Vehicle Network Toolbox

 

Vehicle Network Toolbox

Communicate with in-vehicle networks using CAN, J1939, and XCP protocols

Get Started:

Send and Receive CAN and CAN FD Messages from MATLAB and Simulink

Configuring CAN and CAN FD Channels

CAN channel functions in MATLAB and CAN configuration blocks in Simulink enable you to define a connection to Vector CAN interface hardware that establishes a physical connection with a CAN bus using either the CAN or CAN FD standard. Vehicle Network Toolbox provides CAN channel functions to query and configure CAN interface hardware settings, such as bus speed and transceiver settings. You can also verify other CAN channel properties, such as the number of messages available and the number of messages received or transmitted on the channel. By attaching Vector CAN database files to CAN channels, incoming messages are automatically presented using information stored in the database. After defining a CAN channel, you can send and receive CAN messages on the channel.

An automobile connected to a laptop that is being used to access vehicle bus data from MATAB and Simulink.

Connect MATLAB to your vehicle network using CAN and CAN FD protocols.

Sending and Receiving CAN Messages

Standard CAN messages contain properties for storing the CAN message identifier (standard 11-bit or extended 29-bit), the time stamp, and up to 8 bytes of CAN data. A channel configured for CAN FD transmission can hold messages that contain up to 64 bytes of data.

Transmit and receive functions and blocks in the toolbox enable you to send and receive CAN messages over CAN channels. For large datasets, you can log CAN messages for offline analysis.

CAN messages in a timetable and a live script for analyzing them.

MATLAB code to receive CAN messages and their signals in timetable format.

Building and Extracting Signals from CAN Messages

Vehicle Network Toolbox provides functions and blocks for encoding and decoding CAN messages. CAN message data may contain data representing multiple signals. Unpack functions and blocks let you specify start bit, signal length, data type, and byte ordering. Pack functions and blocks provide the same options for assembling data for CAN message transmission.

Simulink model for decoding CAN messages.

Simulink model that uses the CAN Unpack block to decode CAN messages.

Logging and Replaying CAN Messages

Using the CAN Log block in the toolbox, you can save CAN messages received by your model to a MAT-file. You can then use the CAN Replay block to replay the messages in another Simulink model. The CAN Replay block preserves the time stamps of the logged data so the replayed data will have the same timing characteristics as the recorded data.

Scope view of wheel speeds.

Plot of wheel speed data replayed from a recorded vehicle test run.

Communicate Over the XCP Protocol

Vehicle Network Toolbox provides functions and blocks for communicating with ECUs via XCP—an automotive calibration protocol—over CAN, CAN FD, or Ethernet bus. When communicating with ECUs over XCP, MATLAB or Simulink is the master and the ECUs are slave devices. You can communicate with multiple ECUs by opening multiple XCP channels. For each ECU, you can read and write data to specific memory locations within the ECU. When secured access to an ECU exists, you can use seed and key security to open access to the ECU. The toolbox also provides functions and blocks for linking A2L database files and for creating and viewing dynamic DAQ and STIM measurement lists for an XCP channel. These lists are established based on measurement and event information from the linked A2L file.

Three windows showing a Simulink model and XCP Data Acquisition block mask to configure XCP data acquisition.

Model for acquiring measurements from an ECU slave device. The model uses XCP Configuration and XCP Transport Layer blocks (top), and XCP Data Acquisition blocks (bottom left) to set up the acquisition of the PWM signal (bottom right).

Communicate Over the J1939 Protocol

Vehicle Network Toolbox provides functions and blocks for communicating via J1939—a CAN-based high-level protocol commonly used in the heavy-duty truck industry. When communicating over J1939, you use MATLAB functions or Simulink blocks to set up the communication. Specifically, functions and blocks are provided to associate a database (.dbc) file to the J1939 communication, to specify the CAN interface hardware, and to transmit and receive J1939 parameter groups. You encode and decode signal data on the network using parameter groups defined by the database file associated with the connection. Additionally, you can configure Simulink to operate as network nodes with address claiming.

Model showing Simulink blocks used for J1939 Communication.

Model for sending and receiving J1939 data using the J1939 Transmit and J1939 Receive blocks. The model also uses J1939 Network Configuration, J1939 CAN Transport Layer Configuration, and J1939 Node Configuration blocks to set up the communication.

Visualize CAN and CAN FD Traffic

The toolbox includes CAN Explorer and CAN FD Explorer apps for visualizing active traffic on a particular CAN channel. You can use the apps while performing other tasks in MATLAB or Simulink. For CAN database files associated with your CAN channel, the apps decode the messages and display them in their correct engineering units.

When traffic on the network contains more information than needed for your application, you can limit the number of CAN messages received by a CAN channel to a defined range of CAN message identifiers. Using filter functions and mask settings in the toolbox, you receive only the messages needed for your application.

App view of CAN FD bus traffic showing decoded signals and signal visualizations.

Live CAN FD bus traffic on the network displayed with the CAN FD Explorer app. The display shows raw data, decoded signals, and plots of the signals.

Use Vector CAN Database Files

Vehicle Network Toolbox lets you associate a Vector CAN database file with a CAN channel or message from MATLAB or Simulink, enabling you to encode and decode CAN messages using application-specific message and signal names such as EngineMsg and EngineRPM, as well as scaled engineering units. The ability to work with industry-standard database files simplifies the interaction with the CAN bus because the database not only specifies the message list and component signals, but also provides the bit packing and unpacking rules for the associated signals. Signal data type, start bit, length, and byte order are all predefined for the messages in the database, simplifying signal analysis.

Four code samples that show how to open a CAN database file and view messages and signals.

Code example showing how to view messages using information stored in CAN database files.

Use A2L Description Files

Vehicle Network Toolbox lets you use industry-standard A2L (also known as ASAP2) description files to communicate with ECUs over the XCP protocol from MATLAB or Simulink. Using A2L description files enables you to access internal ECU parameters from a MATLAB program or a Simulink model. A2L description files contain information about the associated memory address for a particular parameter, the storage structure, and the data type. The files also contain rules for converting stored values such as system parameters, sensor characteristics, and correction factors into physical units such as RPM and degrees Celsius. Having this data lets you perform calibration and measurement tasks without needing to parse data and decode memory addresses.

MATLAB function to parse and inspect an A2L file.

Code example showing how to access information stored in A2L files for use with XCP connections. It uses a freely available XCP slave simulator from Vector and Vector Virtual CAN channels.

Work with MDF Files

With Vehicle Network Toolbox, you can easily import and export measurement data format (MDF) files. The toolbox supports version 3.0 and higher of the MDF standard. When you work with MDF files in MATLAB, you can view the basic properties about the file, including initial time stamp, data size, channel group, and channel name information. You can also create MDF files from MATLAB and write timetable data to existing MDF files.

To read in a subset of an MDF file, you specify the channel names or the start and stop times. By default, the output format of your result will be returned as a timetable so that you can easily work with the time-stamped data.

For large MDF files that don’t fit in memory, you can create an MDF datastore and preview the data in batches based on parameters that you specify. You can also create a datastore to easily work with a collection of similar MDF files.

MATLAB interface for inspecting MDF files with the preview function and the Variables editor.

Example code to preview an MDF file via the command-line interface and then inspect the data in the Variables editor.

Use Virtual Channels

You can use virtual CAN and CAN FD channels for testing and simulation of network communications. With a virtual channel, you can test models in a loopback configuration without using physical hardware. Vehicle Network Toolbox supports two kinds of virtual channels: bat365 virtual channels and those from third-party CAN interface hardware vendors. Virtual channels from third-party vendors such as Vector and Kvaser require the installation of the corresponding vendor driver or hardware support package. bat365 virtual channels are shipped with the toolbox and require no additional drivers or support packages.

Simulink blocks to send and receive data using bat365 virtual channels.

Simulink model showing the use of bat365 virtual channels to send and receive CAN data without any hardware.

Work with CDFX Files

Access data in ASAM calibration data format (CDFX) with the cdfx function provided by Vehicle Network Toolbox. Use calibration data from a CDF file as inputs to a Simulink model.

Plot showing the ASAM.C.MAP parameter rising and falling over time.

Using calibration data from a CDFX file as input to a Simulink model, and plotting the simulation output for the ASAM.C.MAP parameter.