Class: C0f::CANPacket
- Inherits:
-
Object
- Object
- C0f::CANPacket
- Defined in:
- lib/c0f/can_packet.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#data ⇒ Object
Returns the value of attribute data.
-
#deltas ⇒ Object
Returns the value of attribute deltas.
-
#dlc ⇒ Object
Returns the value of attribute dlc.
-
#id ⇒ Object
Returns the value of attribute id.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#sent ⇒ Object
Returns the value of attribute sent.
-
#sent_history ⇒ Object
Returns the value of attribute sent_history.
Instance Method Summary collapse
-
#avg_delta ⇒ Float
Average delta between sent intevals.
-
#initialize ⇒ CANPacket
constructor
A new instance of CANPacket.
-
#parse(line) ⇒ Boolean
Parses CANDump line into CANPacket.
-
#update(pkt) ⇒ Object
Updates stats when the ID matches for a packet.
Constructor Details
#initialize ⇒ CANPacket
Returns a new instance of CANPacket.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/c0f/can_packet.rb', line 4 def initialize @id = 0 @dlc = 0 @data = Array.new @sent = nil # Statistical items @count = 0 @interval = 0 @sent_history = [] @deltas = [] end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def count @count end |
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def data @data end |
#deltas ⇒ Object
Returns the value of attribute deltas.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def deltas @deltas end |
#dlc ⇒ Object
Returns the value of attribute dlc.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def dlc @dlc end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def id @id end |
#interval ⇒ Object
Returns the value of attribute interval.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def interval @interval end |
#sent ⇒ Object
Returns the value of attribute sent.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def sent @sent end |
#sent_history ⇒ Object
Returns the value of attribute sent_history.
3 4 5 |
# File 'lib/c0f/can_packet.rb', line 3 def sent_history @sent_history end |
Instance Method Details
#avg_delta ⇒ Float
Returns Average delta between sent intevals.
52 53 54 55 |
# File 'lib/c0f/can_packet.rb', line 52 def avg_delta return 0.0 if @deltas.size == 0 @deltas.inject{ |sum, el| sum + el }.to_f / @deltas.size end |
#parse(line) ⇒ Boolean
Parses CANDump line into CANPacket
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/c0f/can_packet.rb', line 19 def parse(line) # (1398128223.815980) can0 13A#0000000000000028 if line=~/\((\d+\.\d+)\) [s]?[l|v]?can\d+ (\w+)#(\w+)/ then @sent = $1.to_f @id = $2 @data = $3.scan(/../).map(&:hex) @dlc = @data.size @count = 1 return true # (1421010803.828887) slcan0 128 [3] A0 00 03 elsif line=~/\((\d+\.\d+)\)\s+[s]?[l|v]?can\d+\s+(\w+)\s+\[(\d+)\] (.+)/ then @sent = $1.to_f @id = $2 @dlc = $3.to_i @data = $4.gsub(' ','') @data = @data.scan(/../).map(&:hex) @count = 1 return true end false end |
#update(pkt) ⇒ Object
Updates stats when the ID matches for a packet
43 44 45 46 47 48 49 |
# File 'lib/c0f/can_packet.rb', line 43 def update(pkt) return if not pkt.id == @id @count += 1 @sent_history.push @sent @deltas.push (pkt.sent - @sent) @sent = pkt.sent end |