lib/rubyserial/posix.rb in rubyserial-0.1.2 vs lib/rubyserial/posix.rb in rubyserial-0.2.0
- old
+ new
@@ -41,20 +41,24 @@
@open = false
end
end
def write(data)
+ data = data.to_s
n = 0
while data.size > n do
buff = FFI::MemoryPointer.from_string(data[n..-1].to_s)
- i = RubySerial::Posix.write(@fd, buff, buff.size)
+ i = RubySerial::Posix.write(@fd, buff, buff.size-1)
if i == -1
raise RubySerial::Exception, RubySerial::Posix::ERROR_CODES[FFI.errno]
else
n = n+i
end
end
+
+ # return number of bytes written
+ n
end
def read(size)
buff = FFI::MemoryPointer.new :char, size
i = RubySerial::Posix.read(@fd, buff, size)
@@ -76,10 +80,24 @@
else
buff.read_string.unpack('C').first
end
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)
+ end
+
+ bytes.map { |e| e.chr }.join
+ end
+
private
def build_config(baude_rate, data_bits)
config = RubySerial::Posix::Termios.new
@@ -93,6 +111,6 @@
config[:cc_c][RubySerial::Posix::VMIN] = 0
config
end
-end
\ No newline at end of file
+end