mrLimpio
mrLimpio

Reputation: 75

Convert .blf to mdf or mf4 in Python

I want to convert .blf files to mdf or mf4 format. The .blf is recorded over multiple channels multiple .dbc are to be used to decode the signals

So far i found different modules to work with CAN data (asammdf, python-can, mdfreader,canmatrix) and partial code examples on SO and Google but I still can't figure out how to make it work.

Upvotes: 0

Views: 1612

Answers (1)

flonair22
flonair22

Reputation: 43

You can use the CANdas library to read the blf using your multiple dbc files. With CANdas, you can convert your data into a pandas dataframe and add it to a mdf file using asammdf. This code worked for me:

import candas as cd
import asammdf

mdf = asammdf.MDF()

database_logpath = "your_blf_filename_here"
dbc = cd.load_dbc(".")
log_data = cd.from_file(dbc, database_logpath)
# The first three 'keys' are not actually signals so I removed them
channel_names = list(log_data.keys())[3:]
blf_dataframe = log_data.to_dataframe(channel_names)
blf_dataframe = blf_dataframe.set_index("time")
blf_dataframe.sort_index(inplace=True)
for channel_name in blf_dataframe:
    try:
        new_signal = asammdf.Signal(
            samples=blf_dataframe[channel_name],
            timestamps=blf_dataframe.index,
            name=channel_name,
        )

        mdf.append(new_signal, common_timebase=True)
    except:
        pass  # There are some weird signals that throw errors when converting to a signal. Skip those


mdf.save("new_mdf.mf4")

I am not sure why but this seems to create a time signal for each signal that is added. I ended up with a couple of those in my final file. If those bother you, you can remove them with asammdf (or find the reason why they are being created in the first place).

For further information, see the CANdas and asammdf documentation.

Upvotes: 0

Related Questions