CAN Cheat Sheet

This page contains sample code for setting up ICSim and working with can-utils.

  • ICSim
  • Record CAN Traffic
    • cansniffer: Display CAN traffic in real time
    • candump: Record CAN traffic for later analysis
  • Send CAn Traffic
    • cansend: Send individual CAN messages on a network
    • canplayer: Replay a candump file
    • cangen: Generate random CAN data
  • Analyze Traffic

ICSim Setup

The following commands can be run on a Linux computer that has a graphical user interface.

cd ~
sudo apt update
sudo apt install libsdl2-dev libsdl2-image-dev can-utils
git clone https://github.com/zombieCraig/ICSim.git
git clone https://github.com/linux-can/can-utils
cd can-utils
make
sudo make install
cp lib.o ~/ICSim
cd ~/ICSim
make clean
make

Launching ICSim

cd ~/ICSim
sudo sh setup_vcan.sh   # Create the vcan0 network
                        # If there is no output, the command likely worked fine.
                        # A message, "RTNETLINK answers: File exists" likely means
                        # that the vcan0 network was already created.
                        # Use ifconfig to verify if vcan0 exists
ifconfig                # If the setup_vcan.sh command was successful,
                        # a vcan0 network device should appear
./icsim vcan0 &         # Launches the virtual car
                        # The & at the end lets you run more commands in the terminal after this command.
                        # You may need to press [enter] after seeing the message like, "Using CAN interface vcan0"
./controls vcan0 &      # Launches the game-like controller that sends messages on the vcan0 network

cansniffer

When running cansniffer, be sure to size your terminal so that all CAN messages fit on one screen.

cansniffer -c vcan0 # Show live network traffic, -c shows colors

candump

candump -l vcan0 -f output.txt  # Capture all CAN network traffic to a log file

cansend

cansend vcan0 188#01                 # Sends the blinker signal to the ICSim vehicle

canplayer

canplayer -I candump-2023-07-17.log  # Replay network traffic 

cangen

cangen vcan0                         # Generate random CAN messages on the vcan0 network
cangen vcan0 -I 445                  # Generate random CAN data with the arbitration ID 445 only
cangen vcan0 -I 445 -L 8             # Generate random CAN data using a specific arbitration  ID & content length

Denoising

# Run this while not interacting with the system to capture background traffic.
candump vcan0 -l -f noise.txt
# Run this, perform an action, and then stop the capture.
candump vcan0 -l -f event.txt
# Eliminate timestamps. (sed replaces the start of the file to the first space with '')
sed 's/^[^ ]* //' -i noise.txt
sed 's/^[^ ]* //' -i event.txt
# Remove entries from event.txt that exist in noise.txt
grep -vxFf noise.txt event.txt