Sha256: aa5619ecb19bcc0b9107fe33ee21db7b4dcfea19f00c0cc7b6b75c75c3c78d72

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

require 'rubygems'
require 'serialport'
require 'timeout'

module Generic

  # Simple generic wrapper around the serialport library
  class Serial

    def initialize(port)
      @read_buffer = ""
 			@device = SerialPort.new(port, 9600, 8, 1, SerialPort::NONE)
    end

    def finalize
      close 
    end

    def read
      # I don't want a timeout here, because the phone gets really busy with receipts
      char = @device.getc
      raise "Could not read data" if char.nil?
      @read_buffer << sprintf("%c", char)
      char        
    end

    def read_until(term)
      term = [term].flatten
      stop = nil
      loop do
        term.each {|t| stop = t and break if @read_buffer.index(t)}
        break if stop
        read
      end
      @read_buffer.slice!(0, @read_buffer.index(stop)+stop.size)
    end

    def write(data)
      begin
        @device.write(data)
      rescue Errno::EIO => e
        raise "Could not write data (IO: #{e.message})"
      rescue Exception => e
        raise "Could not write data (#{e.message})"
      end
    end

    def close
      @device = nil
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jeffrafter-sms-0.8.0 lib/serial/generic.rb
jeffrafter-sms-0.8.1 lib/serial/generic.rb
jeffrafter-sms-0.8.2 lib/serial/generic.rb
jeffrafter-sms-0.8.3 lib/serial/generic.rb