DBC Guide
CanMessenger::DBC parses DBC text and maps signal values to raw CAN bytes.
Load a DBC File
dbc = CanMessenger::DBC.load('example.dbc')
You can also parse inline content:
dbc = CanMessenger::DBC.new(File.read('example.dbc'))
Supported DBC Elements
Current parser support is intentionally narrow and focused:
BO_message definitions.SG_signal definitions in the commonstart|length@endian+/- (factor,offset)pattern.- Ignores standard metadata directives such as
VERSION,NS_,BS_,BU_,CM_,VAL_,BA_,BA_DEF_,BA_DEF_DEF_, andBO_TX_BU_. - Accepts
SIG_VALTYPE_declarations for integer type0. Types1(IEEE float) and2(IEEE double) are unsupported and raiseArgumentErrorinstead of being interpreted as integer signals.
Malformed BO_/SG_ lines and unknown non-empty directives, including unsupported BA_* variants, now raise ArgumentError with line context.
Signal byte order must be @0 or @1; scaling factors and offsets must be valid numbers, and the factor must be nonzero. Signed decimals and scientific notation are supported.
If your DBC uses advanced constructs (for example multiplexing or uncommon syntax variants), validate with tests before relying on runtime behavior.
Encode by Message Name
frame = dbc.encode_can('Example', Speed: 42)
# => { id: 256, data: [42, ...] }
Validation performed during encoding includes:
- Unknown message name rejection.
- Signed/unsigned range checks.
- Signal bit-bound checks against message DLC.
Decode by CAN ID
decoded = dbc.decode_can(frame[:id], frame[:data])
# => { name: 'Example', signals: { Speed: 42.0 } }
Returns nil if no message with matching CAN ID is found.
Integral factors and offsets preserve integer precision, including signed and unsigned 64-bit signals. This also applies to equivalent decimal or scientific notation such as (1.0,0e0). Decoded values with integral scaling are integers; fractional scaling produces floating-point values. Encoding rounds to the nearest raw integer, with halfway values rounded away from zero.
Big-endian and Little-endian Signals
@1is treated as little-endian.@0is treated as big-endian (Motorola/sawtooth indexing).
Current releases include corrected multi-byte big-endian bit mapping and stricter unsigned overflow checks.