Class: CanMessenger::Message

Inherits:
Object
  • Object
show all
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.

Examples:

message = Message.new(0x123, 'EngineData', 8)
message.signals << Signal.new('RPM', start_bit: 0, length: 16, ...)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, dlc) ⇒ Message

Initializes a new Message instance.

Parameters:

  • id (Integer)

    The CAN message ID (11-bit standard or 29-bit extended)

  • name (String)

    The symbolic name of the message

  • dlc (Integer)

    Data Length Code - number of bytes in the message (0-8 for classic CAN)



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

#dlcObject (readonly)

Returns the value of attribute dlc.



213
214
215
# File 'lib/can_messenger/dbc.rb', line 213

def dlc
  @dlc
end

#idObject (readonly)

Returns the value of attribute id.



213
214
215
# File 'lib/can_messenger/dbc.rb', line 213

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



213
214
215
# File 'lib/can_messenger/dbc.rb', line 213

def name
  @name
end

#signalsObject (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.

Parameters:

  • data (Array<Integer>)

    The message data bytes to decode

Returns:

  • (Hash<Symbol, Float>)

    Signal names mapped to their decoded 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.

Parameters:

  • values (Hash<Symbol|String, Numeric>)

    Signal names mapped to their values

Returns:

  • (Array<Integer>)

    Array of bytes representing the encoded message



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