Canmatrix

1.2 · active · verified Mon Apr 13

Canmatrix implements a "Python Can Matrix Object" which describes CAN communication and related objects such as Boardunits, Frames, Signals, and Values. It serves as a versatile tool for reading, writing, and manipulating various CAN database formats including .dbc, .dbf, .kcd, .arxml, .yaml, and .xls(x). The library also provides command-line utilities, `canconvert` for format conversion and `cancompare` for comparing CAN databases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a CAN database (DBC file) into a `CanMatrix` object, then iterate through its frames and signals to access their properties. A dummy DBC file is created for a runnable example.

import os
from canmatrix.formats import dbc

# Create a dummy DBC file for demonstration
dummy_dbc_content = '''
BO_ 100 MSG_1: 8 Vector__XXX
 SG_ Sig1 : 0|8@1+ (1,0) [0|255] "" Vector__XXX
 SG_ Sig2 : 8|16@1+ (0.1,0) [0|6553.5] "" Vector__XXX
'''

dbc_file_path = 'dummy.dbc'
with open(dbc_file_path, 'w') as f:
    f.write(dummy_dbc_content)

# Load the CAN matrix from the DBC file
db = dbc.load(dbc_file_path)

# Access frames and signals
for frame_name in db:
    frame = db.frames[frame_name]
    print(f"Frame: {frame.name} (ID: {frame.arbitration_id.id}, DLC: {frame.size} bytes)")
    for signal_name in frame.signals:
        signal = frame.signals[signal_name]
        print(f"  Signal: {signal.name} (StartBit: {signal.start_bit}, Length: {signal.size} bits)")

# Clean up the dummy file
os.remove(dbc_file_path)

view raw JSON →