lib/rubyserial/posix.rb in rubyserial-0.2.4 vs lib/rubyserial/posix.rb in rubyserial-0.3.0
- old
+ new
@@ -1,6 +1,6 @@
-require 'ffi'
+# Copyright (c) 2014-2016 The Hybrid Group
class Serial
def initialize(address, baude_rate=9600, data_bits=8)
file_opts = RubySerial::Posix::O_RDWR | RubySerial::Posix::O_NOCTTY | RubySerial::Posix::O_NONBLOCK
@fd = RubySerial::Posix.open(address, file_opts)
@@ -81,10 +81,22 @@
buff.get_bytes(0,1).bytes.first
end
end
def gets(sep=$/, limit=nil)
+ if block_given?
+ loop do
+ yield(get_until_sep(sep, limit))
+ end
+ else
+ get_until_sep(sep, limit)
+ end
+ end
+
+ private
+
+ def get_until_sep(sep, limit)
sep = "\n\n" if sep == ''
# This allows the method signature to be (sep) or (limit)
(limit = sep; sep="\n") if sep.is_a? Integer
bytes = []
loop do
@@ -93,11 +105,9 @@
break if (bytes.last(sep.bytes.to_a.size) == sep.bytes.to_a) || ((bytes.size == limit) if limit)
end
bytes.map { |e| e.chr }.join
end
-
- private
def build_config(baude_rate, data_bits)
config = RubySerial::Posix::Termios.new
config[:c_iflag] = RubySerial::Posix::IGNPAR