Class: CanMessenger::Message
- Inherits:
-
Object
- Object
- CanMessenger::Message
- Defined in:
- lib/can_messenger/dbc.rb
Overview
Represents a CAN message definition from a DBC file.
A Message contains the basic message properties (ID, name, data length) and a collection of Signal objects that define how data is structured within the message payload.
Instance Attribute Summary collapse
-
#dlc ⇒ Object
readonly
Returns the value of attribute dlc.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#signals ⇒ Object
readonly
Returns the value of attribute signals.
Instance Method Summary collapse
-
#decode(data) ⇒ Hash<Symbol, Float>
Decodes message data bytes into individual signal values.
-
#encode(values) ⇒ Array<Integer>
Encodes signal values into the message byte array.
-
#initialize(id, name, dlc) ⇒ Message
constructor
Initializes a new Message instance.
Constructor Details
#initialize(id, name, dlc) ⇒ Message
Initializes a new Message instance.
220 221 222 223 224 225 |
# File 'lib/can_messenger/dbc.rb', line 220 def initialize(id, name, dlc) @id = id @name = name @dlc = dlc @signals = [] end |
Instance Attribute Details
#dlc ⇒ Object (readonly)
Returns the value of attribute dlc.
213 214 215 |
# File 'lib/can_messenger/dbc.rb', line 213 def dlc @dlc end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
213 214 215 |
# File 'lib/can_messenger/dbc.rb', line 213 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
213 214 215 |
# File 'lib/can_messenger/dbc.rb', line 213 def name @name end |
#signals ⇒ Object (readonly)
Returns the value of attribute signals.
213 214 215 |
# File 'lib/can_messenger/dbc.rb', line 213 def signals @signals end |
Instance Method Details
#decode(data) ⇒ Hash<Symbol, Float>
Decodes message data bytes into individual signal values.
Extracts and decodes each signal from the message data bytes, applying the appropriate scaling (factor/offset) to produce the final engineering unit values.
253 254 255 256 257 258 259 |
# File 'lib/can_messenger/dbc.rb', line 253 def decode(data) res = {} @signals.each do |sig| res[sig.name.to_sym] = sig.decode(data) end res end |
#encode(values) ⇒ Array<Integer>
Encodes signal values into the message byte array.
Iterates through all signals in the message and encodes their values into the appropriate bit positions within the message data bytes.
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/can_messenger/dbc.rb', line 234 def encode(values) bytes = Array.new(@dlc, 0) @signals.each do |sig| next unless values.key?(sig.name.to_sym) || values.key?(sig.name.to_s) v = values[sig.name.to_sym] || values[sig.name.to_s] sig.encode(bytes, v) end bytes end |