Class: SerialCanBus::ISOTP

Inherits:
Object
  • Object
show all
Defined in:
lib/serial_can_bus/iso-tp.rb

Overview

Helpers for ISO-TP (ISO-15765-2) data packet creation/parsing.

  • Examples

build multiple segments from single payload:

segments = SerialCanBus::ISOTP.split(payload)

segments.each do |data|
 response = slcan.transmit_frame(:standard, 0x7ff, data.size, data)
end

Defined Under Namespace

Classes: Consecutive, First, Flow, Single

Class Method Summary collapse

Class Method Details

.split(data) ⇒ Object

Split data in one ore more ISO-TP segments.

arguments:

count  frame count (optional)

return values:


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/serial_can_bus/iso-tp.rb', line 142

def self.split(data)
  packets = []
  dlength = data.size

  if dlength <= 7
    packets << Single.new(:dlength => dlength, :data => data)
  elsif dlength <= 0xfff
    index = 1
    packets << First.new(:dlength => dlength, :data => data[0..5])

    data[6..-1].scan(/.{1,7}/).each do |segment|
      packets << Consecutive.new(:dindex => index % 16, :data => segment)
      index += 1
    end
  else
    raise "invalid length (#{dlength} != 0-4095)"
  end

  packets
end