benito_h
benito_h

Reputation: 560

Read header of .blf CAN file

I have a .blf file containing multiple can bus messages, which I can read using python-can like so

import can
can_log = can.BLFReader("./test.blf")
for msg in can_log:
    print(msg)

According to the python-can docs, the header of a standard .blf file has 144 bytes and contains the start- and ending-timestamp of the whole record itself.

I would like to directly read this start and end timestamp, is this possible?

I know I could also read the timestamp from the first message using msg.timestamp, but it slightly differs from the start timestamp that I would like to extract.

Upvotes: 1

Views: 3714

Answers (1)

Benoît
Benoît

Reputation: 416

From the source code of python can :

[...]
class BLFReader(object):
"""
Iterator of CAN messages from a Binary Logging File.

Only CAN messages and error frames are supported. Other object types are
silently ignored.
"""

def __init__(self, filename):
    self.fp = open(filename, "rb")
    data = self.fp.read(FILE_HEADER_STRUCT.size)
    header = FILE_HEADER_STRUCT.unpack(data)
    #print(header)
    assert header[0] == b"LOGG", "Unknown file format"
    self.file_size = header[10]
    self.uncompressed_size = header[11]
    self.object_count = header[12]
    self.start_timestamp = systemtime_to_timestamp(header[14:22])
    self.stop_timestamp = systemtime_to_timestamp(header[22:30])
[...]

You can use start_timestamp and stop_timestamp this way :

can_log.start_timestamp

Upvotes: 1

Related Questions