Use candas to parse and convert blf files in python

The previous article introduced a blf parsing method, which is to use cantools and python-can toolkit, first load the dbc file, and then parse the blf file. In fact, there is a more advanced package that can do this work, that is candas.

In fact, cantools and python-can are also used at the bottom of candas, but they are packaged at a high level and provide richer functions. It can directly convert data into a dataframe, and can also draw. Let's see how to use candas to analyze blf document.

Because candas is developed based on cantools and python-can, it also needs dbc files, but its special library is that it needs to pass a folder parameter, that is to say, we need to pass the folder where the dbc file is located. Go to the parameter list, and the blf file needs to pass the file name, that is, without the extension ".blf". The following is the specific code.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import candas as cd
from asammdf import MDF
database_logpath = r"C:\Users\master01\Downloads\1.28\1-28-(20-21)"
dbc_logpath = r"C:/Users/master01\Downloads/"
db_version = "LR19"
dl = cd.load_dbc(dbc_logpath)

log_data = cd.from_file(dl, database_logpath, verbose=True)
s1 = log_data['FCU_Stk_ClntIn_Pres_kPa']
#print(s1[0][0],s1[1][0],s1[2][0])

all_sig_names = []
for msg in dl.messages:
    for sig in msg.signals:

        all_sig_names.append(sig.name)
df = log_data.to_dataframe(all_sig_names,mode="sampling",frequency=1)
df.set_index('time',inplace=True)
mdf = MDF()
mdf.append(df)
mdf.save("blfconvert.mf4",overwrite=True)

As you can see, the first step is to load the dbc file. Note that you need to pass the folder where dbc is located, and then use candas.from_file(dbc, blffile, verbose=True), and the returned log_data is of catalog type. This class has a to_dataframe method. This method can directly convert data into dataframe, which is very convenient. This method has several parameters. The first is the list of signal names to be converted, and the second is the conversion mode. The default is concat, because blf is the same as mf4, allowing data It is not aligned, so that the original data can be kept as much as possible, but the transferred data is relatively large. Another mode is sampling, followed by a sampling frequency, so that the transferred data is aligned and the frequency is all Consistent, the data is a little smaller than the previous schema. In the end, my code converts the dataframe into mf4, which is more convenient to use canape to view.

This library is actually quite easy to use, but the development tools did not use this library in the end, but used the underlying library cantools and python-can to analyze. The reason is that after using this library, the function is no problem during development, but using pyinstaller After packaging, there is a problem. It always reports an error. After checking the information, it seems that there is a shared library conflict. In the end, it was not resolved, so I had to give up. If it is not packaged, it is still ok. The high-level API is still more convenient, but the parameters do not understand why. To make it like that.

Of course, candas has many other functions, such as drawing various charts, and some functions that I don’t know what to use. If you are interested, you can check the official documents.

Guess you like

Origin blog.csdn.net/zy1620454507/article/details/129856152