#!/bin/bash # The vehicle's VIN is available from message ID $380. # Due to it's length, it is split across three messages. # The first character contains the messge ID [0-2] # The next seven characters holds ASCII character (in hexadecimal) # Each part of the message is sent 0.1 seconds apart. # We need to combine all three parts, convert them to ASCII, # and store them in the variable $VIN # We'll wait up to 0.35 seconds for three message to arrive # (in any order). # We'll create an array called $message declare -a message # We're going to store the VIN number in a variable called $vin vin=$( timeout -s 9 0.35 /usr/bin/candump -L can0,03E0:0fff | \ cut -d# -f2 | while read a do # Extract the message ID number [0-3] id="$( echo $a | cut -c1-2 )" id=$( printf "%d" $id) # Extract the data, convert to ASCII, remove trailing NULL characters. data="$( echo $a | cut -c3- )" data="$( echo $data | fold -w2 | paste -sd ' ' | sed -e 's/\( 00\)*$//g' )" data="$( bindechexascii --h2a $data 2>/dev/null | cut -d: -f2 | cut -c2- )" # Assign the data to an array called $message message[$id]="$data" # Print all three message parts, present or not. echo ${message[@]} | tr -d ' ' # Exit the loop, only keeping the last line, which we # have set (above) to be stored in the variable $vin done | tail -1 ) chars=$( echo -n "$vin" | wc -c ) # If there are no characters, no VIN was received. Print an error. if [ $chars -eq 0 ] then echo "NOTFOUND" exit 2 fi # Check to see how many characters are in our reassembled VIN. # If it is less than 17, exit the script with an error. if [ $chars -lt 17 ] then # INVALID VIN, EXIT WITH RESULT CODE 1 TO SIGNIFY AN ERROR. echo "INVALID($vin)" exit 1 fi # Print the VIN to standard output echo $vin