pub struct Signal { /* private fields */ }Expand description
Represents a CAN signal within a message.
A Signal contains:
- A name
- Start bit position and length
- Byte order (big-endian or little-endian)
- Signed/unsigned flag
- Factor and offset for physical value conversion
- Min/max range
- Optional unit string
- Receivers (nodes that receive this signal)
§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" *
"#)?;
let message = dbc.messages().at(0).unwrap();
let signal = message.signals().at(0).unwrap();
println!("Signal: {} (bits: {}-{})", signal.name(), signal.start_bit(), signal.start_bit() + signal.length() - 1);Implementations§
Source§impl Signal
impl Signal
Sourcepub fn encode_raw(&self, physical_value: f64) -> Result<u64>
pub fn encode_raw(&self, physical_value: f64) -> Result<u64>
Encode a physical value to raw bits for this signal.
This is the inverse of decode_raw(). It converts a physical value
(after factor and offset have been applied) back to the raw integer
value that can be inserted into a CAN message payload.
§Arguments
physical_value- The physical value to encode (e.g., 2000.0 for RPM)
§Returns
Ok(raw_bits)- The raw bits ready to be inserted into the payloadErr(Error)- If the value is outside the signal’s min/max range
§Formula
raw_value = (physical_value - offset) / factorSourcepub fn encode_to(&self, physical_value: f64, payload: &mut [u8]) -> Result<()>
pub fn encode_to(&self, physical_value: f64, payload: &mut [u8]) -> Result<()>
Encode a physical value and insert it into a payload buffer.
This is a convenience method that combines encode_raw() with
ByteOrder::insert_bits() to directly write the encoded value
into a CAN message payload.
§Arguments
physical_value- The physical value to encodepayload- The mutable payload buffer to write into
§Returns
Ok(())- Value was successfully encoded and writtenErr(Error)- If encoding failed or signal extends beyond payload
Source§impl Signal
impl Signal
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
Returns the signal name.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.name(), "SIGNAL_NAME");Sourcepub fn start_bit(&self) -> u16
pub fn start_bit(&self) -> u16
Returns the start bit position of the signal in the CAN message payload.
The start bit indicates where the signal begins in the message data. For little-endian signals, this is the LSB position. For big-endian signals, this is the MSB position.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.start_bit(), 16);Sourcepub fn length(&self) -> u16
pub fn length(&self) -> u16
Returns the length of the signal in bits.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.length(), 16);Sourcepub fn byte_order(&self) -> ByteOrder
pub fn byte_order(&self) -> ByteOrder
Returns the byte order (endianness) of the signal.
Returns either ByteOrder::LittleEndian (Intel format, @1+) or ByteOrder::BigEndian (Motorola format, @0+).
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.byte_order(), ByteOrder::LittleEndian);Sourcepub fn is_unsigned(&self) -> bool
pub fn is_unsigned(&self) -> bool
Returns true if the signal is unsigned, false if signed.
In DBC format, + indicates unsigned and - indicates signed.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.is_unsigned(), true);Sourcepub fn factor(&self) -> f64
pub fn factor(&self) -> f64
Returns the scaling factor applied to convert raw signal values to physical values.
The physical value is calculated as: physical_value = raw_value * factor + offset
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.factor(), 0.5);Sourcepub fn offset(&self) -> f64
pub fn offset(&self) -> f64
Returns the offset applied to convert raw signal values to physical values.
The physical value is calculated as: physical_value = raw_value * factor + offset
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.offset(), -40.0);Sourcepub fn min(&self) -> f64
pub fn min(&self) -> f64
Returns the minimum physical value for this signal.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.min(), -40.0);Sourcepub fn max(&self) -> f64
pub fn max(&self) -> f64
Returns the maximum physical value for this signal.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.max(), 85.0);Sourcepub fn unit(&self) -> Option<&str>
pub fn unit(&self) -> Option<&str>
Returns the unit of measurement for this signal, if specified.
Returns None if no unit was defined in the DBC file.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.unit(), Some("km/h"));Sourcepub fn receivers(&self) -> &Receivers
pub fn receivers(&self) -> &Receivers
Returns the receivers (ECU nodes) that subscribe to this signal.
Returns a reference to a Receivers enum which can be either a list of node names or None.
§Examples
let message = dbc.messages().find("MSG_NAME").unwrap();
let signal = message.signals().find("SIGNAL_NAME").unwrap();
assert_eq!(signal.receivers().len(), 2);
assert!(signal.receivers().contains("ECU2"));Sourcepub fn is_multiplexer_switch(&self) -> bool
pub fn is_multiplexer_switch(&self) -> bool
Check if this signal is a multiplexer switch (marked with ‘M’)
Sourcepub fn multiplexer_switch_value(&self) -> Option<u64>
pub fn multiplexer_switch_value(&self) -> Option<u64>
Get the multiplexer switch value if this is a multiplexed signal (marked with ‘m0’, ‘m1’, etc.) Returns None if this is a normal signal (not multiplexed)