Canmatrix in simple words

Canmatrix is a Python library for working with CAN database files in code.

It also provides command-line tools for common jobs such as converting and comparing databases.

Canmatrix represents objects such as:

  • ECUs
  • frames
  • signals
  • value tables
  • attributes
  • multiplexed definitions

It is useful when the same database task must run many times or become part of a build, test, or conversion process.

Install canmatrix

Terminal
python -m pip install canmatrix

Check that the command-line tools are available:

Terminal
canconvert --help
cancompare --help

Convert a DBC file

Canmatrix can read and write several database formats. A simple conversion uses the input and output file extensions:

Terminal
canconvert vehicle.dbc vehicle.xlsx

Convert an AUTOSAR system description to DBC:

Terminal
canconvert vehicle.arxml vehicle.dbc

Convert a DBC back to DBC after applying options or normalizing the output:

Terminal
canconvert source.dbc cleaned.dbc

Always review the result. Different file formats do not support exactly the same information, so a conversion may not be lossless.

Compare two CAN databases

Terminal
cancompare old.dbc new.dbc

This is useful in a script or review pipeline when you want a repeatable comparison instead of manually opening both files.

Use canmatrix from Python

The package API can load databases and expose their frames and signals. A small inspection script looks like this:

Python
import canmatrix.formats

databases = canmatrix.formats.loadp("vehicle.dbc")

for bus_name, matrix in databases.items():
    print(f"Bus: {bus_name or 'default'}")
    for frame in matrix.frames:
        print(f"0x{frame.arbitration_id.id:X} {frame.name}")
        for signal in frame.signals:
            print(f"  - {signal.name} ({signal.size} bits)")

The exact objects you use depend on the file type and the canmatrix version. Keep the package version pinned in automation and test the output with representative files.

Useful canmatrix jobs

Extract one ECU

Terminal
canconvert --ecus=ENGINE_ECU vehicle.dbc engine-only.dbc

Extract selected frames

Terminal
canconvert --frames=EngineData,VehicleSpeed vehicle.dbc selected.dbc

Rename a signal

Terminal
canconvert --renameSignal=OldName:NewName source.dbc updated.dbc

Merge another database

Terminal
canconvert --merge=second.dbc source.dbc merged.dbc

Before using these commands on an important database, work on a copy and compare the output.

Canmatrix vs cantools

Both are Python projects used around CAN databases, but their common use cases differ.

NeedCommon first choice
Decode and encode CAN payloads in Pythoncantools
Convert between several database formatscanmatrix
Compare or reshape database filescanmatrix
Build a simple runtime DBC decodercantools

This is not a strict rule. Check the current APIs and supported formats for your project.

Canmatrix vs a DBC viewer

CanmatrixVisual DBC viewer
Python and command-line workflowGraphical workflow
Good for automationGood for human inspection
Easy to repeat in CIEasy to explore unfamiliar files
Can convert and batch-change filesCan show message trees and bit layouts
Output should be validatedChanges can be reviewed visually

The tools can work together. Use canmatrix to automate a conversion, then open the generated DBC in a viewer to check message counts, signal definitions, multiplexing, units, and layouts.

A safe automation pattern

Example
source database
      |
      v
canmatrix conversion
      |
      v
generated DBC
      |
      +--> automated checks
      |
      +--> visual review in a DBC viewer

Do not assume that a successful command means the database is semantically correct. Validate important signals with known raw payloads.

The simple summary

Canmatrix is a Python library and command-line toolkit for CAN database automation. Use it when you need repeatable conversions, comparisons, extraction, merging, or code-based changes. Use a visual DBC viewer when a person needs to understand and review the result.

References