lib/rubyserial/windows.rb in rubyserial-0.2.4 vs lib/rubyserial/windows.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::Win32::GENERIC_READ | RubySerial::Win32::GENERIC_WRITE @fd = RubySerial::Win32.CreateFileA("\\\\.\\#{address}", file_opts, 0, nil, RubySerial::Win32::OPEN_EXISTING, 0, nil) @@ -74,21 +74,17 @@ end count.read_int end def gets(sep=$/, limit=nil) - 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 - current_byte = getbyte - bytes << current_byte unless current_byte.nil? - break if (bytes.last(sep.bytes.to_a.size) == sep.bytes.to_a) || ((bytes.size == limit) if limit) + if block_given? + loop do + yield(get_until_sep(sep, limit)) + end + else + get_until_sep(sep, limit) end - - bytes.map { |e| e.chr }.join end def close err = RubySerial::Win32.CloseHandle(@fd) if err == 0 @@ -98,7 +94,23 @@ end end def closed? !@open + 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 + current_byte = getbyte + bytes << current_byte unless current_byte.nil? + 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 end