Configure Model for XCP on CAN External Mode
R2026bThe XCP on CAN external mode enables you to monitor signals and tune parameters in a Simulink® model deployed on Raspberry Pi® using a CAN bus connection. With Simulink as the host interface, you can use the Monitor & Tune workflow to interact with the running model directly from Simulink. Use this workflow to prepare signals and parameters, deploy the model to Raspberry Pi hardware, and monitor and tune parameters in real time over CAN.
Prerequisites
Before you begin, make sure
CAN hardware is installed and verified on the host computer. For more information, see Step1: Set Up CAN Hardware on Host Computer for XCP on CAN Communication.
CAN interface is enabled on Raspberry Pi hardware.
Vehicle Network Toolbox™ is installed.
Example Model
This example uses a simple model that generates a sine wave, applies a tunable gain, and outputs the results to a PWM pin on Raspberry Pi. You can monitor the sine wave signal and tune the gain parameter in real time over XCP on CAN.

Open Simulink model and create a blank model.
Add the following blocks and connect them:
Configure the Sine Wave (Simulink) block.
Set amplitude to
1.Set frequency to
2*pi.Set sample time to
0.01seconds.
Configure the Gain (Simulink) block.
Set gain to
0.5.
Configure the PWM block.
Set pin number to
18or any other available GPIO pin.
In the Configuration Parameters dialog box, configure these parameters in the model solver:
Set Type to
Fixed-step.Set Solver to
auto (Automatic solver selction).Set Fixed-step size to
0.01.
Enable Signal Logging
Enable logging for the signals you want to monitor during external mode simulation.
In the Simulink model, select the signal line you want to monitor (for example, the output of the Sine Wave (Simulink) block).
On the Simulation tab of the model, click Log Signals. A logging badge appears on the signal line, indicating the signal will be captured during external mode execution.
Repeat for any additional signals you want to monitor.
Select Hardware
In the Configuration Parameters dialog box, select Hardware Implementation.
Set Hardware board to
Raspberry Pi (64bit).
Configure CAN Baud Rate on Target
In the Configuration Parameters dialog box, navigate to Hardware board settings>Target hardware resources>CAN.
Set the Bus speed (kbps) to match the baud rate configured on the host CAN hardware. The host and target CAN interfaces must use the same baud rate. A baud rate mismatch prevents communication.
Configure External Mode Communication
In the Configuration Parameters dialog box, navigate to Hardware board settings>Target hardware resources>External mode.
Set Communication interface to
XCP on CAN.Set Host interface to
Simulink.Configure the host-side CAN hardware parameters.
CAN Vendor — Select the vendor of the CAN hardware connected to the host computer (Vector, PEAK-System, or Kvaser). This list is populated from the CAN hardware detected by the
canChannelList(Vehicle Network Toolbox) function.CAN Device — Select the CAN device from the list of detected devices for the selected vendor.
CAN Channel — Select the CAN channel on the selected device.
Configure the target-side CAN parameter.
CAN interface name — Enter the SocketCAN interface name on Raspberry Pi.
Configure CAN identifier parameters.
Extended CAN ID — Select if your CAN network uses extended 29-bit identifiers instead of standard 11-bit identifiers.
CAN ID command — CAN identifier for command messages from host to target.
CAN ID response — CAN identifier for response messages from target to host.
Configure polling time.
Set XCP target polling time — Set to
Manuallyif you need to override the automatic polling time.Polling time (in seconds) — Specify a custom polling time.
Click Apply.
Deploy Model and Monitor Signals
On the Hardware tab of the model, click Monitor & Tune. Simulink builds the model, deploys it to Raspberry Pi, and establishes the XCP on CAN connection.
Once the connection is established, open Simulation Data Inspector to view logged signals in real time. Modify tunable parameters directly in the block dialog or MATLAB® Command Window to observe the effect on the deployed model.
To stop the external mode session, on the Hardware tab, click Stop.
Troubleshooting
Connection timeout in external mode
Run
canChannelList(Vehicle Network Toolbox) in the MATLAB Command Window to verify the CAN hardware is detected.If the hardware is not listed, see the Troubleshooting steps.
Close and reopen the Configuration Parameters dialog box after connecting new hardware.
Insufficient logging buffer memory
If you encounter a logging buffer memory overflow error:
Reduce the number of signals marked for logging.
Increase the Logging buffer size (in bytes) value or select Set logging buffer size automatically to allow Simulink to calculate the required buffer size.
Data logging gaps or dropped packets
If model execution consumes most of the available CPU time, the external mode task may not receive sufficient execution time. To run the external mode task in a separate thread, select Run external mode in a background.
See Also
External mode | canChannelList (Vehicle Network Toolbox) | External mode | CAN | CAN
Transmit | CAN
Receive