Challenge

Project 1

Answer 1.

Bash script that logs CAN data from one or more CAN buses, sorts the messages into logs of even and odd ARB IDs, and stores them in respective directories. The script creates new log files when the current log file reaches 1000 messages.

Bash Script

#!/bin/bash

# Directories for storing logs

EVEN_DIR="Even_Logs"

ODD_DIR="Odd_Logs"

# CAN interface(s) (change if needed)

CAN_INTERFACES=("vcan0") # Add more interfaces if needed, e.g., ("vcan0" "vcan1")

# Create directories if they don't exist

mkdir -p "$EVEN_DIR"

mkdir -p "$ODD_DIR"

# Counters for number of messages in the current log file

even_count=0

odd_count=0

# Index for log files

even_log_index=1

odd_log_index=1

# Infinite loop to capture and sort CAN messages

while true; do

# Capture CAN messages from all specified interfaces

candump "${CAN_INTERFACES[@]}" | while read -r line; do

# Extract the ARB ID from the CAN message (assumed format: "<timestamp> vcan0 123#data")

ARB_ID_HEX=$(echo $line | awk '{print $3}' | cut -d '#' -f 1)

# Convert ARB ID to decimal

ARB_ID_DEC=$((16#$ARB_ID_HEX))

# Determine if ARB ID is even or odd

if (( ARB_ID_DEC % 2 == 0 )); then

# EVEN ARB ID

echo $line >> "$EVEN_DIR/log_even_$even_log_index.txt"

even_count=$((even_count + 1))

# Check if 1000 messages have been logged, and if so, start a new file

if (( even_count >= 1000 )); then

even_count=0

even_log_index=$((even_log_index + 1))

fi

else

# ODD ARB ID

echo $line >> "$ODD_DIR/log_odd_$odd_log_index.txt"

odd_count=$((odd_count + 1))

# Check if 1000 messages have been logged, and if so, start a new file

if (( odd_count >= 1000 )); then

odd_count=0

odd_log_index=$((odd_log_index + 1))

fi

fi

done

done

Explanation

  1. Directories: The script creates two directories, "Even_Logs" and "Odd_Logs", to store the log files for even and odd ARB ID messages, respectively.
  2. CAN Interfaces: The CAN_INTERFACES array holds the names of the CAN interfaces (e.g., vcan0). You can add more interfaces if needed.
  3. Message Count and Log Index: The script tracks the number of messages logged (even_count and odd_count) and the index of the current log file (even_log_index and odd_log_index). When 1000 messages are logged, it increments the log index and resets the message count.
  4. Logging CAN Messages: The script uses candump to capture CAN messages from the specified interfaces. It extracts the ARB ID from each message, checks if it is even or odd, and appends the message to the appropriate log file.
  5. Log File Naming: Log files are named log_even_X.txt and log_odd_X.txt, where X is the log file index.
  6. Infinite Loop: The script runs indefinitely until manually stopped (e.g., with Ctrl+C).

Running the Script

  1. Ensure can-utils is installed on your system.
  2. Set up the virtual CAN interface(s) if needed:

sudo modprobe vcan

sudo ip link add dev vcan0 type vcan

sudo ip link set up vcan0

  1. Save the script as can_logger.sh and make it executable:

chmod +x can_logger.sh

  1. Run the script:

./can_logger.sh

This script will log and sort CAN messages based on the ARB ID, creating log files in the respective directories for even and odd ARB IDs. Each log file will store up to 1000 messages before a new log file is created.

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.