0

I'm using Python CAN to look for events in BLF files, specifically J1939 TD events so I can determine in which log a "real world"/vehicle timestamped events occurred.

Something along the lines of:

log = can.BLFReader(filename)
for msg in log:
if (is_Message_FEE6(msg))
    etc

However, if I want to find the first and last TD messages in a log, I have to search the entire log "forwards" in order to find the last TD. Since some of the log files are hundreds of MB, this is somewhat time consuming.

So, is it possible to read the log file in reverse?

e.g.

foundFirst = False
foundLast = False
open log
for msg in log:
    if isMessageFEE6(msg):
       first_TD = ExtractTD(msg)
       FoundFirst = True
       break
for msg in log.reversed():
    if isMessageFEE6(msg):
       last_TD = ExtractTD(msg)
       FoundLast = True
       break

Thanks in advance

  • Instead of `log.reversed()`, try `reversed(log)`. I think the former actually constructs the reversed list (which might take a while), whereas the latter just iterates over the original list in reverse order. – Michael Dyck Feb 15 '22 at 14:40
  • @Michael Dyck. Thanks for the response. I think the issue is that if I do `reversed(log)` there is an error saying that the log is not of the correct type to be reversed. I first have to do `fwd_list = list(log)` then `rev_list = reversed(fwd_list)` and the conversion to a list takes a long time in the first place. The log is a reader stream so is it possible to manipulate this in some way other than converting to a list ? – komandirskie Feb 16 '22 at 12:56
  • Okay, ignore my first comment. It's possible that, because of compression, BLFs can only be read forwards from the start. If that's the case, then if you want the last record, you can't avoid iterating through the whole file, and using `reversed` will just add more time. I think your only option is to find a faster BLF reader. – Michael Dyck Feb 16 '22 at 14:17

0 Answers0