🔓 Comprehensive analysis of OEM manufacturers’ CAN communication message files! Practical application of CAN messages, taking you back to fault conditions!
With the CAN bus | A deeper understanding of the theoretical foundation of the CAN protocol, in the article on the bus series | Unlocking the core of the CAN protocol: a comprehensive analysis of DBC files, we have already learned how to use <span>canmatrix</span> to load DBC files. After all this groundwork, we can finally perform practical applications with real vehicle messages!
1 Message Files
When it comes to CAN messages, everyone is certainly familiar with them. There are many types of CAN message files, with known formats including <span>.asc</span>, <span>.blf</span>, <span>.csv</span>, <span>.mf4</span>, etc. When we use <span>ZlgCAN</span> or <span>CANoe</span> to collect messages, which format should we save them in?<span>.blf</span> is a binary log format that supports various buses and protocols, and it takes up less storage space. <span>.mf4</span> is the <span>ASAM</span> standard, suitable for data exchange with other tools. In fact, there is not much difference between the formats; they are supported by different software, and you can choose the appropriate format based on your needs. However, for users of the <span>Vector</span> family of products, <span>.blf</span> is generally the best choice.
1.1 MDF Message Standardization
ASAM MDF (Measurement Data Format) is a binary file format used to record CAN, CAN FD, and LIN bus data. The MDF format has become an industry standard, ensuring interoperability between various CAN tools. The uniqueness of MDF4 lies in its ability to store not only raw data but also necessary metadata. This includes information required to interpret the raw data, such as converting it to physical values, and providing signal names that comply with ASAM standards as context.
2 Message Software
When using the CAN bus on Linux, most people use SocketCAN[1]. It is supported by the mainline Linux kernel and follows the Linux interface model. It also allows the creation of virtual CAN interfaces, enabling the simulation or replay of CAN messages without any physical hardware. On the <span>Windows</span> platform, there are many options such as <span>canalystii</span>, <span>cantact</span>, <span>vector</span>, etc. The library function we will use in this article is the famous <span>Python</span> library <span>python-can</span>, which supports the aforementioned different backends and provides interfaces, such as supporting Kvaser CANLib SDK, PCAN, IXXAT VCI V3 or V4 SDK, XL Driver Library, etc.
3 Trace Replay Message Application
In fact, when we analyze message faults, we generally do not use a direct hardware connection. First, the on-site direct connection requires complete equipment preparation, and second, some vehicle faults are sporadic and difficult to reproduce. Therefore, in general, we obtain the fault messages for offline analysis. If you often analyze <span>blf</span> messages, you will definitely use the <span>Trace</span> backtracking function in <span>Vector Canoe</span>, which can reproduce the messages transmitted on the CAN bus when the fault occurred.💪 Now let’s implement it!
1 Install Dependencies ⚙️
To parse message files, we depend on the <span>python-can</span> and <span>canmatrix</span> libraries. Run the following two commands in the terminal to install them.
$ pip install python-can
$ pip install canmatrix
2 Load DBC Database 📊
Load the local dedicated dbc database file to interpret CAN messages.
import canmatrix
def load_dbc(dbc_path):
"""load can dbc"""
dbc = canmatrix.formats.loadp(dbc_path)
dbc_matrix = dbc.get("")
return dbc_matrix
3 Load Messages 📄
<span>python-can</span> supports loading various file types, including <span>blf</span>, <span>mf4</span>, <span>asc</span>, <span>csv</span>, <span>sqlite</span>, etc. Choose the corresponding loader based on the file type you need to load. This step requires reading the file into memory, which is usually slower.
import python-can
def load_blf(path):
"""load can blf"""
data = can.BLFReader(path)
# data = can.MF4Reader(path)
# data = can.ASCReader(path)
# data = can.CSVReader(path)
# data = can.SqliteReader(path)
return data
4 Parse Messages 🧐
<span>canmatrix</span> provides two parsing methods: <span>decode</span> and <span>decode_pycan</span>. The <span>decode_pycan</span> method is specifically designed for <span>python-can</span> (essentially still calls the <span>decode</span> method).
def parse_blf():
data = load_blf("blf.blf")
dbc_matrix = load_dbc("dbc.dbc")
for frame in data:
try:
# dbc parses blf
decode_frame = dbc_matrix.decode_pycan(frame)
arbitration_id = frame.arbitration_id
signals = [decode_frame.get(signal) for signal in decode_frame.keys()]
print({'arbitration_id': arbitration_id, 'signals': signals})
except Exception as e:
print(e)
Observe the printed information; <span>arbitration_id</span> is the arbitration ID of the message frame, and <span>signal</span> is a custom <span>DecodeSignal</span> type that contains a <span>raw_value</span> representing the actual value on the bus, and the <span>Signal</span> class describes which bits of which signal it is.
🎉 At this point, you have implemented the functionality to parse fault messages using <span>DBC</span> and replay them! Try it with your messages!
Reference Links
<span>[1]</span> SocketCAN: https://en.wikipedia.org/wiki/SocketCAN
Since you’ve made it this far, why not give a thumbs up and follow to support the author? 🙏 Your support is the motivation for my continued creation!