pub struct Message { /* private fields */ }Expand description
Represents a CAN message in a DBC file.
A Message contains:
- A unique ID (CAN identifier)
- A name
- A DLC (Data Length Code) specifying the message size in bytes
- A sender node (ECU that transmits this message)
- A collection of signals
§Examples
use dbc_rs::Dbc;
let dbc_content = r#"VERSION "1.0"
BU_: ECM
BO_ 256 EngineData : 8 ECM
SG_ RPM : 0|16@0+ (0.25,0) [0|8000] "rpm" *
"#;
let dbc = Dbc::parse(dbc_content)?;
let message = dbc.messages().at(0).unwrap();
println!("Message: {} (ID: {})", message.name(), message.id());Implementations§
Source§impl Message
impl Message
Sourcepub fn decode_into(&self, data: &[u8], out: &mut [f64]) -> usize
pub fn decode_into(&self, data: &[u8], out: &mut [f64]) -> usize
Decode all signals into the output buffer (physical values).
This is a zero-allocation decode path for high-speed CAN processing. Signals are decoded in order and written to the output buffer.
§Arguments
data- Raw CAN payload bytesout- Output buffer for physical values (must be at leastsignals().len()long)
§Returns
Number of signals decoded, or 0 if payload is too short.
§Example
let msg = dbc.messages().find_by_id(256).unwrap();
let mut values = [0.0f64; 64];
let count = msg.decode_into(&payload, &mut values);
for i in 0..count {
let signal = msg.signals().at(i).unwrap();
println!("{}: {}", signal.name(), values[i]);
}Sourcepub fn decode_raw_into(&self, data: &[u8], out: &mut [i64]) -> usize
pub fn decode_raw_into(&self, data: &[u8], out: &mut [i64]) -> usize
Decode all signals into the output buffer (raw integer values).
Returns raw values before factor/offset conversion. Useful for encoding or debugging.
§Arguments
data- Raw CAN payload bytesout- Output buffer for raw values (must be at leastsignals().len()long)
§Returns
Number of signals decoded, or 0 if payload is too short.
Source§impl Message
impl Message
Sourcepub fn id(&self) -> u32
pub fn id(&self) -> u32
Returns the CAN message ID.
This returns the raw CAN ID as it would appear on the bus (11-bit or 29-bit).
For extended (29-bit) IDs, the internal flag bit is stripped.
Use is_extended() to check if this is an extended ID.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"\n\nBU_: ECM\n\nBO_ 256 EngineData : 8 ECM"#)?;
let message = dbc.messages().at(0).unwrap();
assert_eq!(message.id(), 256);Sourcepub fn is_extended(&self) -> bool
pub fn is_extended(&self) -> bool
Returns true if this message uses an extended (29-bit) CAN ID.
Standard CAN uses 11-bit identifiers (0-2047), while extended CAN uses 29-bit identifiers (0-536870911).
§Examples
use dbc_rs::Dbc;
// Standard 11-bit ID
let dbc = Dbc::parse(r#"VERSION "1.0"\n\nBU_: ECM\n\nBO_ 256 EngineData : 8 ECM"#)?;
let message = dbc.messages().at(0).unwrap();
assert!(!message.is_extended());
// Extended 29-bit ID (with flag bit set: 0x80000000 | 0x18DAF115)
let dbc = Dbc::parse(r#"VERSION "1.0"\n\nBU_: ECM\n\nBO_ 2564485397 OBD2 : 8 ECM"#)?;
let message = dbc.messages().at(0).unwrap();
assert!(message.is_extended());
assert_eq!(message.id(), 0x18DAF115);Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the message name.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"\n\nBU_: ECM\n\nBO_ 256 EngineData : 8 ECM"#)?;
let message = dbc.messages().at(0).unwrap();
assert_eq!(message.name(), "EngineData");Sourcepub fn dlc(&self) -> u8
pub fn dlc(&self) -> u8
Returns the Data Length Code (DLC) in bytes.
DLC specifies the size of the message payload. For classic CAN, this is 1-8 bytes. For CAN FD, this can be up to 64 bytes.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"\n\nBU_: ECM\n\nBO_ 256 EngineData : 8 ECM"#)?;
let message = dbc.messages().at(0).unwrap();
assert_eq!(message.dlc(), 8);Sourcepub fn sender(&self) -> &str
pub fn sender(&self) -> &str
Get the sender node name for this message.
The sender is the node that transmits this message on the CAN bus.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"
BU_: ECM TCM
BO_ 256 Engine : 8 ECM
SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" *
"#)?;
let message = dbc.messages().iter().next().unwrap();
assert_eq!(message.sender(), "ECM");Sourcepub fn signals(&self) -> &Signals
pub fn signals(&self) -> &Signals
Returns a reference to the signals collection for this message.
The Signals collection provides methods to iterate, search, and access signals by index.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"
BU_: ECM
BO_ 256 Engine : 8 ECM
SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm" ECM
SG_ Torque : 16|16@1+ (0.1,0) [0|500] "Nm" ECM
"#)?;
let message = dbc.messages().find("Engine").unwrap();
let signals = message.signals();
assert_eq!(signals.len(), 2);
assert!(signals.find("RPM").is_some());Sourcepub fn min_bytes_required(&self) -> u8
pub fn min_bytes_required(&self) -> u8
Returns the minimum number of bytes required to decode all signals in this message.
This calculates the actual byte coverage of all signals, which may be less than the declared DLC. Use this when validating frame payloads for decoding - the payload must have at least this many bytes to decode all signals successfully.
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse(r#"VERSION "1.0"
BU_: ECM
BO_ 256 Engine : 8 ECM
SG_ Temp : 0|8@1+ (1,0) [0|255] "" ECM
SG_ Pressure : 8|8@1+ (1,0) [0|255] "" ECM
"#)?;
let message = dbc.messages().find("Engine").unwrap();
assert_eq!(message.dlc(), 8); // Declared DLC is 8
assert_eq!(message.min_bytes_required(), 2); // But signals only need 2 bytes