How can I open a blf file by using python script?

323 Views Asked by At

I want to open a blf file using python scripts and print it. I have opened the file on wireshark. But when I open it using script and print it I do not get any output and there are no errors.

These are a few environment details: os: windows, python 3.11.5, appdirs==1.4.4, attrs==23.1.0, canmatrix==1.0, click==8.1.7, colorama==0.4.6,dpkt==1.9.8, future==0.18.3, lxml==4.9.3, numpy==1.25.2, packaging==23.1, pyshark==0.6, python-can==4.2.2, pywin32==306, scapy==2.5.0, six==1.16.0, termcolor==2.3.0, typing_extensions==4.7.1, wrapt==1.15.0, IDE: Visual Studio code, file:Lines.blf, size: 21.8 MB, contains around 50000 in number

import can

blf_file_path = "C:/Users/sahaanan/Desktop/blf_to_pcap/NewSW_7209_Lines.blf"

try:
    with open(blf_file_path, 'rb') as blf_file:
        log = can.BLFLogReader(blf_file)
        for msg in log:
            print(msg)

    if log.is_empty:
        print("The BLF file is empty.")
except Exception as e:
    print(f"Error reading BLF file: {e}")
1

There are 1 best solutions below

6
Tranbi On

Where did you get can.BLFLogReader from? According to the doc, you should be able to Read BLF files with can.io.BLFReader. Also is_empty doesn't seem to be a valid attribute. You can check if the object count is equal to 0 insted:

import can

blf_file_path = "C:/Users/sahaanan/Desktop/blf_to_pcap/NewSW_7209_Lines.blf"

try:
    with open(blf_file_path, 'rb') as blf_file:
        log = can.io.BLFReader(blf_file)
        for msg in log:
            print(msg)

        if log.object_count == 0:
            print("The BLF file is empty.")
except Exception as e:
    print(f"Error reading BLF file: {e}")