Challenge

Project 1

1.

The CAN protocol uses  a message-based communication model where each device in a network has a unique identifier that is used to address messages sent.Arbitration ID identifies messages and indicates the message priority. Frames come in two formats : standard which uses 11 bit Arbitration ID and extended which uses 29 bit Arbitration ID.The Even Arbitration ID message is sorted by using the CAN bus tools are cangen & candump.

Cyb_Sec_Project1 attached shows the complete program.

The explanation is given below.

The program has been implemented using Python and Socket CAN package.It uses the CAN interfaces & interface module to create a CAN interface for each specified bus.In the present example two buses 'can0' and 'can1' are considered.But one can modify the can-buses list to match one's specific set up.The program continuously looks for CAN messages using a 'while' loop with a timeout of 1 second.If a message is received it checks for whether the Arbitration ID is even or odd.This is done by performing a modulo operation on the Arbitration ID.Messages with even Arbitration IDs are logged in the 'Even logs' directory while messages with odd Arbitration IDs are logged in the 'Odd logs' directory.The program keeps track of log counters(even_log_counter as well as odd_log_counter) to create new log files when the current log files reach the maximum size of 1000 messages.Each log file is named using the log counter and the corresponding directory.The program appends the received CAN messages to the log file and checks its size.If the size of the log file exceeds the maximum size,a new log file is created by incrementing the corresponding log counter.The program also prints the received CAN messages and the log file in which it is stored for visual feedback.

CAN_diag shows the CAN network and the message frame.

The program involves basic file operations and handling message counts.Some relevant calculations are discussed below.

A.Maximum Log Size:The maximum number of messages per log file is set to 1000.One can modify this value based on one's own requirement.When the log file exceeds this size,a new log file is created.

B.Log Counters:The program uses separate log counters to keep track of the log file numbers for even and odd Arbitration ID messages.The restective counters are incremented whenever a new log file needs  to be created.Initially both counters are set to zero.

C.Log file naming: Each log file is named in the format 'log_{log_counter}.txt' where {log_counter} represents the value of the log counter for the corresponding Arbitration ID category.

D.Log Directories:The program creates the 'even logs' and 'odd logs' directories using os.make dirs( ).If the directories already exist, the program continues without overwriting them.

E.File Size Check:The program uses os.path.get size( ) to check the size of the log files.If the size exceeds the maximum log size, a new log file is created by incrementing the corresponding log counter.

By executing this program one can continuously log CAN messages from multiple CAN buses,sort them into even and odd Arbitration ID logs and store them in separate directories.The program will run indefinitely until manually stopped by a keyboard interrupt(Control+C).

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.