Sha256: 4b947c981a4faa8f28201afe40035c15953d80e9e5e649ed523276404369f513

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 Bytes

Contents

require 'monitor'

module Adapter

  class Generic
    include MonitorMixin
    include RsLogger

    attr_accessor :event

    def initialize(*args, &block)
      Thread.abort_on_exception = true
      unless instance_variables.include?(:@rxd)
        @rxd = true
      end
      @event = block
      super(*args)
      run
    end

    def reading_allowed?
      @rxd
    end

    def defer_reading
      @rxd = false
      !reading_allowed?
    end

    def allow_reading
      @rxd = true
    end

    def rx(int, blocking = false)
      byte = read(int, blocking)
      if byte
        logger.debug "RX [#{byte.length}]: #{byte.inspect}"
      end
      byte
    end

    def tx(bytes)
      int = write(bytes)
      logger.debug "TX [#{int}]: #{bytes.inspect}"
      int
    end

    def restart_rx_thread
      allow_reading
      run
    end

    def run
      Thread.new do
        loop do
          rx(1, false) if reading_allowed?
          sleep(0.005)
        end
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rs_232-2.0.5 features/support/adapter/generic.rb
rs_232-2.0.4 features/support/adapter/generic.rb