Reading .blf file of vector canalyzer using python

会社技術

Install the library in python

First of all,
you should install ‘can’ and ‘cantools’ by console.
Please input below command.

pip install cantools
pip install python-can

Coding

This is a blf reading code.

import pandas as pd
import cantools
import can

filename=input("please input dbc filename:")
print(filename)
dbc_can=cantools.database.load_file(filename)

file=input("please input blf filename:")
print(file)
blfdata=can.io.blf.BLFReader(file)

BO=input("please input BO Num.:") #red circle at picture
print(BO)

name=input("please input Message Num.:") #red triangle at picture
print(name)

variable=input("please input variable.:") #red squere at picture
print(variable)

#initialized time constant
timestamp_ini=0
#initialized list
outputlist=[]

for msg in blfdata:
    if(msg.arbitration_id==int(BO)):
        temp=dbc_can.get_message_by_name(name).\
            decode(msg.data, decode_choices=False)
        aaa=temp[variable]
        #bbb=temp[]
        #ccc=temp[]
        if(timestamp_ini==0):
            timestamp_ini=msg.timestamp
        else:
            Timer=msg.timestamp-timestamp_ini
            outputlist.append([Timer,aaa])

df=pd.DataFrame(outputlist)
df.columns=(["Time","aaa"])
df.to_csv(file[:-4]+".csv") #.blf -> .csv

Picture is shown below.
This is a dbc file.

Code sample is shown as below

Tips

The below error is often occured.
It is caused by overlapping at blf? or dbc?..
So you should delete columns.
For example, if red squere is overlapping the other column,
you should delete this column.

Ending

Thank you for reading my report.
I hope you will find it useful.