Skip to main content

Crate can_decode

Crate can_decode 

Source
Expand description

§can_decode

Decode and encode CAN frames into messages/signals in a fast and easy way.

§Features

  • Parse DBC (CAN Database) files
  • Decode CAN messages into signals with physical values
  • Encode signal values back into raw CAN messages
  • Support for both standard and extended CAN IDs
  • Handle big-endian and little-endian byte ordering
  • Support for signed and unsigned signal values
  • Decode/encode IEEE-754 float signals (SIG_VALTYPE_)
  • Support for DBC enumerations (value descriptions) to map raw values to string labels
  • Apply scaling factors and offsets (and inverse for encoding)

§Decoding Example

use can_decode::Parser;
use std::path::Path;

// Load a DBC file
let parser = Parser::from_dbc_file(Path::new("my_can_database.dbc"))?;

// Decode a CAN message
let msg_id = 0x123;
let data = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0];

if let Some(decoded) = parser.decode_msg(msg_id, &data) {
    println!("Message: {}", decoded.name);
    for (signal_name, signal) in &decoded.signals {
        println!("  {}: {:?} {}", signal_name, signal.value, signal.unit);
    }
}

§Encoding Example

use can_decode::Parser;
use std::path::Path;
use std::collections::HashMap;

let parser = Parser::from_dbc_file(Path::new("my_can_database.dbc"))?;

// Encode a CAN message from signal values
let signal_values = HashMap::from([
    ("EngineSpeed".to_string(), 2500.0),
    ("ThrottlePosition".to_string(), 45.5),
]);

if let Some(data) = parser.encode_msg(0x123, &signal_values) {
    println!("Encoded CAN data: {:02X?}", data);
}

// Or encode by message name
if let Some((msg_id, data)) = parser.encode_msg_by_name("EngineData", &signal_values) {
    println!("Message ID: {:#X}, Data: {:02X?}", msg_id, data);
}

Re-exports§

pub use can_dbc;

Structs§

DecodedMessage
A decoded CAN message containing signal values.
DecodedSignal
A decoded signal with its physical value.
DecodedSignalValue
Represents the decoded value of a CAN signal.
MsgEntry
Internal entry representing a loaded CAN message with its format definitions.
Parser
A CAN message parser that uses DBC file definitions.
SignalMeta
Signal metadata for a signal’s value interpretation and DBC description/comment.

Enums§

FloatFormat
Specifies the IEEE-754 floating-point format for a signal.

Type Aliases§

SignalMap
Type alias for the ordered map of signals returned by decoding.