Decoding logged CAN .blf file using a .dbc file

1.3k Views Asked by At

I am trying to decode a logged CAN file which is saved in CAN binary logging format (.blf)

I tried to use the following piece of code to decode my .blf file, but seem to get key error

import cantools
import can

blf_file = r'path to blf file'
dbc_file = r'path to dbc file'

# Load the .dbc file 
db = cantools.database.load_file(dbc_file)
decoded_msg = []

# Open the .blf file and create a log reader
with can.BLFReader(blf_file) as can_log:
    for msg in can_log:
        print(msg.arbitration_id)
        print(msg.data)
        decoded_msg.append(
            db.decode_message(msg.arbitration_id, msg.data)
        )

When I run this program, I seem to get the following error

150889227
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 16, in <module>
  File "C:\Users\A450407\PycharmProjects\CanReaderv3\venv\lib\site-packages\cantools\database\can\database.py", line 485, in decode_message
    message = self._frame_id_to_message[frame_id_or_name]
KeyError: 150889227

What could be going wrong, and how can I possibly fix it ?

0

There are 0 best solutions below