Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert .blf (CAN) to .csv in Python

#1
How to Convert .blf (CAN) to .csv in Python

5/5 – (1 vote)

Problem Formulation


? How to convert .blf from a CAN bus to .csv in Python?

? What is BLF? The Binary Logging Format (BLF) is a proprietary
CAN log format from the automative company Vector Informatik GmbH.

? What is CAN? The Controller Area Network (CAN bus) is a message-based protocol standard for microcontrollers in vehicles to communicate without a host computer.

Method 1: Using BLF Reader and CSV Writer


To convert the BLF file 'my_file.blf' to the CSV file 'my_file.csv', you can first iterate over the bus messages using can.BLFReader('my_file.csv') and add the data to a list of lists. Then, you can use the csv.writer() approach to write the list of lists to a CSV file.

Here’s an example that improves upon this SO thread:

import can
import csv log = [] for msg in list(can.BLFReader("my_file.blf")): msg = str(msg) row = [msg[18:26], msg[38:40], msg[40:42], msg[46], msg[62], msg[67:90]] log.append(row) with open("my_file.csv", "w", newline='') as f: writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL) writer.writerows(log)

A more sophisticated version of this code is provided in this Github repository. Here’s a screenshot of the code — notice the more advanced processing of a single message compared to our solution:

Method 2: Using the candas Library


The candas library provides utility functions to work with .blf files and the CAN bus. Among other things, it helps you with the conversion from BLF to CSV as outlined here.

This is the provided example:

import candas as cd db = cd.load_dbc("dbc_folder") # This is the BLF file 'my_file.blf':
log = cd.from_file("my_file") # This prints a signal from the messages in the BLF:
print(log["AVGcellTemperature"])

Method 3: Using Custom Solution from python-can Library


You can use your tailor-made solutions by combining the Readers and Writers provided in the python-can library.

It provides multiple utility functions such as:

  • Listener
  • BufferedReader
  • RedirectReader
  • Logger
  • Printer
  • CSVWriter
  • SqliteWriter
  • ASC
  • Log
  • BLF

Chances are you’ll find what you’re looking for when going over those functions!

Related Video


Still not satisfied? I found the following relevant video when searching for a solution to this problem. I think you’ll find some nice tricks in the video!






https://www.sickgaming.net/blog/2022/07/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,394 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,332 12-08-2022, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,299 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Python Convert Hex to Base64 xSicKxBot 0 1,276 11-30-2022, 09:32 PM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,302 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] Python Convert Image (JPG, PNG) to CSV xSicKxBot 0 1,296 09-10-2022, 12:05 PM
Last Post: xSicKxBot
  [Tut] Python Convert Parquet to CSV xSicKxBot 0 1,183 09-02-2022, 03:20 PM
Last Post: xSicKxBot
  [Tut] Python Convert Markdown Table to CSV xSicKxBot 0 1,222 09-01-2022, 01:21 AM
Last Post: xSicKxBot
  [Tut] How to Convert a Log to a CSV File in Python? xSicKxBot 0 1,290 08-30-2022, 02:11 AM
Last Post: xSicKxBot
  [Tut] Python – How to Convert KML to CSV? xSicKxBot 0 1,201 08-21-2022, 08:08 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:

Forum software by © MyBB Theme © iAndrew 2016