Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0526162e6d
Fetching contributors…

Cannot retrieve contributors at this time

83 lines (71 sloc) 1.747 kb
require 'rubygems'
require 'serialport'
class Serial
attr_reader :buffer, :port
def self.list
Dir.new("/dev").entries.select{|e| e =~ /tty\./}
end
def initialize(opts={})
Dir.chdir("/dev")
usb = Dir.glob("tty.usbserial*")
@baud_rate = opts[:baud_rate] || 9600
@data_bits = 8
@stop_bits = 1
@parity = SerialPort::NONE
@port_str = opts[:port] || "/dev/#{usb}"
puts "using #{usb} baud_rate: #{@baud_rate}"
@port = SerialPort.new(@port_str, @baud_rate, @data_bits, @stop_bits, @parity)
@port.read_timeout = 100
@buffer = "" # ? v. bytes
@buffer_until = convert_to_byte( opts[:buffer_until]) if opts[:buffer_until]
end
def read! &block
raise "buffer_until is not set, this will run forever!" unless @buffer_until
s = @port.getc
@buffer << s.chr if s
if (s == @buffer_until)
if block_given?
yield
else
@buffer
end
else
false
end
end
def buffer_until=(s)
@buffer_until = convert_to_byte(s)
end
def buffer_until
@buffer_until.chr
end
def clear_buffer!
@buffer = "" # ? v. bytes
end
def read_string_until(s)
s = convert_to_byte(s)
result = ""
stop = false
count = 0
@buffer.each_byte do |b|
unless stop
result << b.chr
count = count + 1
stop = true if b == s
end
end
@buffer.slice! 0..(count-1)
result
end
def convert_to_byte(s)
raise ArgumentError, "can only use a string or integer to buffer" unless s.is_a?(String) || s.is_a?(Integer)
if s.is_a? String
s[0]
else
s
end
end
def method_missing(method, *args, &block)
@port.send(method, *args)
end
end
Jump to Line
Something went wrong with that request. Please try again.