Sha256: 229295ebc4301545b9b9d6a3a3ea9beb32a5143f00b1ed8ad60b121d0d22e1f7

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

module RfBeam
  module KLD7
    require 'rubyserial'
    require 'timeout'
    
    class Error < StandardError
    end

    attr_reader :serial_port

    def initialize(path, baude_rate = 0)
      baude_rate = check_speed(baude_rate)
      data_bits = 8
      parity = :even
      stop_bits = 1
      open_serial_port(path, baude_rate, data_bits, parity, stop_bits)
      init_radar if connected?

      yield self if block_given?
    end

    def close
      return unless connected?

      @serial_port.close
      @serial_port = nil
      @serial_port.nil?
    end

    def read(bytes)
      return unless connected?

      @serial_port.read(bytes)
    end

    def write(string)
      return unless connected?

      @serial_port.write(string)
      sleep 0.1
    end

    private

    def open_serial_port(path, baude_rate, data_bits, parity, stop_bits)
      @serial_port = Serial.new(path, baude_rate, data_bits, parity, stop_bits)
    end

    def connected?
      raise Error, 'ConnectionError: No open Serial device connections.' if @serial_port.nil?

      true
    end

    def flush
      return unless connected?

      loop until serial_port.getbyte.nil?
    end

    def check_speed(baude)
      if BAUDE_RATES.value? baude
        baude
      elsif BAUDE_RATES.key? baude
        BAUDE_RATES[baude]
      else
        115_200
      end
    end

    def init_radar
      command = ['INIT', 4, 0]
      @serial_port.write command.pack('a4LL') # 4 ASCII bytes, UINT32, UINT32
      check_response
    end

    def check_response
      sleep RESPONSE_DELAY
      resp = @serial_port.read(9).unpack('a4LC') # 4 ASCII bytes, UINT32, UINT8
      raise Error, 'No valid response from Serial Port' if resp[2].nil?

      response_key = resp[2]
      return response_key.zero? # Everything OK

      error_string = RESP_CODES[response_key].nil? ? 'Response unknown' : RESP_CODES[response_key]
      raise Error, "Radar response Error: #{error_string}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rfbeam-0.4.3 lib/rfbeam/kld7/serial_connection.rb
rfbeam-0.4.2 lib/rfbeam/kld7/serial_connection.rb
rfbeam-0.4.1 lib/rfbeam/kld7/serial_connection.rb