class LXP::Packet::Parser

Given an input string, work out which type of LXP::Packet it should be, and call .parse on the appropriate class.

Attributes

ascii[R]
bdata[R]

Public Class Methods

new(ascii) click to toggle source
# File lib/lxp/packet/parser.rb, line 17
def initialize(ascii)
  @ascii = ascii
  @bdata = ascii.unpack('C*')
end
parse(ascii) click to toggle source
# File lib/lxp/packet/parser.rb, line 13
def self.parse(ascii)
  new(ascii).parse
end

Public Instance Methods

parse() click to toggle source
# File lib/lxp/packet/parser.rb, line 22
def parse
  case bdata[7] # tcp_function
  when TcpFunctions::HEARTBEAT then Heartbeat.new
  when TcpFunctions::TRANSLATED_DATA then parse_translated_data
  else
    raise "unhandled tcp_function #{bdata[7]}"
  end
end
parse_input() click to toggle source

Input packets are 1-of-3; work out which it is from the register

# File lib/lxp/packet/parser.rb, line 44
def parse_input
  case Utils.int(bdata[32, 2]) # register
  when 0  then ReadInput1
  when 40 then ReadInput2
  when 80 then ReadInput3
  end
end
parse_translated_data() click to toggle source
# File lib/lxp/packet/parser.rb, line 31
def parse_translated_data
  kls = case bdata[21] # device_function
        when DeviceFunctions::READ_HOLD then ReadHold
        when DeviceFunctions::WRITE_SINGLE then WriteSingle
        when DeviceFunctions::READ_INPUT then parse_input
        else
          raise "unhandled device_function #{bdata[21]}"
        end

  kls.parse(ascii)
end