Challenge

Project 1

Aim: Write a program that logs CAN data from one or more CAN buses where CAN messages (from any bus) should be sorted into logs of even ARB ID messages and odd ARB ID messages. Even logs should be stored in a Linux Directory called “Even Logs” and odd logs should be stored in a Linux Directory called “Odd Logs”. Individual log files should only store up to 1000 messages. The program should run indefinitely until manually stopped.

First, add virtual can network vcan0.

Terminal 1:

$sudo ip link add dev vcan0 type vcan

$sudo ip link set vcan0 up

$sudo ip link

open a bash script named "Project1.sh" for writing our program

$mousepad Project1.sh

Even Arbitration Id will be those which end with 0, 2, 4, 6, 8, A, C, and E while Odd Arbitration Id will be those which end with 1, 3, 5, 7, 9, B, D, and F.

#!/bin/bash
#Storing 1000 data messages with Even Arbitration ID in the directory "Even Logs".
mkdir "Even Logs" "Odd Logs"
var="1"
count="000"
while [ $var -gt 0 ]
do
cd "Even Logs"
candump -L -n 1000 vcan0,'0':00F,'2':00F,'4':00F,'6':00F,'8':00F,'A':00F,'C':00F,'E':00F
>"Even_ArbID_$count".log
#Storing 1000 data messages with Odd Arbitration ID in the directory "Odd Logs".
cd ..
cd "Odd Logs"
candump -L -n 1000 vcan0,'1':00F,'3':00F,'5':00F,'7':00F,'9':00F,'B':00F,'D':00F,'F':00F
>"Odd_ArbID_$count".log
((count++))
((var++))
cd ..
done

Save the program.

Terminal 1: Run the program after coming into the specific directory where the program is saved and modifying its rights.

$sudo chmod +x Project1.sh

$./Project1.sh

Terminal 2: Generate random data for logging via cangen

$cangen vcan0 -g 10 -v

The program will run indefinitely until we stop it manually (as the variable 'var' value will always be greater than 0 since we are incrementing it on each iteration).

Logs having Even as well as Odd Arb IDs are stored in their respective directories.

Each log file contains up to 1000 messages.

Odd_ArbID_3.log contains 210 messages as we stopped our program here manually.

Even_ArbID_000.log contains data messages which have even arbitration IDs i.e IDs ending with 0, 2, 4, 6, 8, A, C, and E. Similarly Even_ArbID_1.log and Even_ArbID_2.log contain only even arbitration IDs data messages.

Odd_ArbID_000.log contains data messages which have odd arbitration IDs i.e. IDs ending with 1, 3, 5, 7, 9, B, D, and F. Similarly Odd_ArbID_1.log and Odd_ArbID_2.log contain only odd arbitration IDs data messages.

Result:

  • CAN message from vcan0 were logged into logs of even ARB ID messages and odd ARB ID messages.
  • The even logs were stored in a Linux Directory called “Even Logs” and odd logs were stored in a Linux Directory called “Odd Logs”.
  • The Individual log files should store only 1000 messages.
  • The program runs indefinitely until manually stopped.
  • All files formed or created are attached.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.