Skip to content

murilopontes/canbus-decoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

canbus-decoder

Minimal CAN-bus signal decoder/encoder, in the spirit of a .dbc database, without external dependencies.

Given a message definition (arbitration ID, DLC, and packed signals with scale/offset/bit-layout), it decodes raw CAN frames into physical values and encodes physical values back into a raw payload. Supports both Intel (little-endian) and single-byte Motorola (big-endian) signal layouts.

Why

Most real DBC tooling (cantools, vendor tools) is either heavyweight or requires the actual .dbc file. This is a small, dependency-free building block for cases where you just need programmatic signal packing/unpacking — e.g. quick diagnostics scripts, test fixtures, or embedding in a larger tool.

Example

from canbus_decoder import Database, Message, Signal

db = Database()
db.add_message(
    Message(
        can_id=0x100,
        name="EngineStatus",
        dlc=4,
        signals=[
            Signal(name="rpm", start_bit=0, length=16, scale=0.25, unit="rpm"),
            Signal(name="coolant_temp", start_bit=16, length=8, offset=-40, unit="C"),
            Signal(name="throttle", start_bit=24, length=8, scale=0.4, unit="%"),
        ],
    )
)

message, values = db.decode_line("  can0  100   [8]  D0 07 3C 64 00 00 00 00")
print(message.name, values)
# EngineStatus {'rpm': 500.0, 'coolant_temp': 20, 'throttle': 40.0}

Or pipe live candump output through the CLI:

candump vcan0 | python -m canbus_decoder.cli

Development

python -m venv .venv
.venv/bin/pip install -e ".[dev]"
.venv/bin/pytest

Layout

  • signal.py — bit-level packing/unpacking of a single signal (Intel/Motorola)
  • message.py — a set of signals sharing one CAN ID and DLC
  • frame.py — parsing raw candump-formatted lines
  • database.py — a lookup table from CAN ID to message definition
  • cli.py — example CLI wiring the pieces together

License

MIT

About

Minimal CAN-bus signal decoder/encoder (DBC-style bit packing)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages