CAN Communication with MATLAB and Simulink
MATLAB, Simulink, and Vehicle Network Toolbox can be used for CAN/CAN FD traffic, logging, and model-based workflows when the installed MATLAB release supports the selected hardware and driver.
Prerequisites
- A detected CAN or CAN FD interface and its required driver.
- A correctly wired and terminated test bus.
- MATLAB, Simulink when using models, and Vehicle Network Toolbox.
- A MathWorks hardware-support path that lists the selected vendor and device for the installed release.
Hardware support changes between MATLAB releases. Check the current MathWorks documentation and the result returned by your local MATLAB installation rather than assuming support from a device name alone.
Discover Channels
In the MATLAB command window, inspect the available CAN channels:
canChannelListUse the vendor, device, and channel identifiers returned on the test computer. Do not copy identifiers from another system without checking them.
Script Workflow
A classic CAN script typically follows this sequence:
% Replace these placeholders with values returned by canChannelList.
ch = canChannel("VendorName", "DeviceName", 1);
configBusSpeed(ch, 500000);
start(ch);
msg = canMessage(100, false, 8);
msg.Data = uint8([1 2 3 4 5 6 7 8]);
transmit(ch, msg);
rx = receive(ch, 1);
stop(ch);The example uses an arbitrary test identifier and payload. Use only identifiers and timing approved for the test network. For CAN FD, use the CAN FD channel and message functions documented for your MATLAB release, including separate arbitration- and data-phase settings where required.
Simulink Workflow
- Add the CAN configuration block for the detected hardware.
- Build a transmit path with a data source, CAN packing block, and CAN transmit block.
- Build a receive path with CAN receive, parsing or unpacking, and a display or logging sink.
- Keep the device and channel selection consistent across the configuration, transmit, and receive blocks.
- Confirm solver and sample-time settings before running the model.
Refer to the MathWorks Vehicle Network Toolbox documentation for the exact blocks and APIs in the installed release.
