diff --git a/lib/dino/board.rb b/lib/dino/board.rb index 450930d8..5736a1da 100644 --- a/lib/dino/board.rb +++ b/lib/dino/board.rb @@ -1,3 +1,5 @@ +require 'timeout' + module Dino class Board attr_reader :digital_hardware, :analog_hardware @@ -105,12 +107,18 @@ def normalize(pin, spaces) end def handshake - 100.times do - reset - sleep 0.045 - return @io.flush_read if @io.gets.to_s.chop.match /ACK/ + 50.times do + begin + reset + Timeout::timeout(0.1) do + return @io.flush_read if @io.gets.to_s.chop.match /ACK/ + end + rescue + nil + end end raise BoardNotFound end + end end \ No newline at end of file diff --git a/lib/dino/tx_rx/base.rb b/lib/dino/tx_rx/base.rb index 3312d48b..1bb866e9 100644 --- a/lib/dino/tx_rx/base.rb +++ b/lib/dino/tx_rx/base.rb @@ -1,3 +1,4 @@ +require 'timeout' require 'observer' module Dino @@ -14,9 +15,6 @@ def read end end - def flush_read - end - def close_read return nil if @thread.nil? Thread.kill(@thread) diff --git a/lib/dino/tx_rx/tcp.rb b/lib/dino/tx_rx/tcp.rb index 5d58bb57..59a11a90 100644 --- a/lib/dino/tx_rx/tcp.rb +++ b/lib/dino/tx_rx/tcp.rb @@ -15,6 +15,12 @@ def gets io.gets end + def flush_read + loop do; Timeout::timeout(0.005) { gets } end + rescue + nil + end + def write(message) io.write(message + "\r\n") end diff --git a/spec/lib/tx_rx/usb_serial_spec.rb b/spec/lib/tx_rx/usb_serial_spec.rb index 8eb13e4b..58ed09cb 100644 --- a/spec/lib/tx_rx/usb_serial_spec.rb +++ b/spec/lib/tx_rx/usb_serial_spec.rb @@ -71,7 +71,7 @@ module Dino IO.should_receive(:select).and_return(true) Thread.should_receive(:new).and_yield subject.should_receive(:loop).and_yield - mock_serial.should_receive(:gets).and_return("foo::bar\n") + mock_serial.should_receive(:gets).and_return("foo:bar\n") subject.should_receive(:changed).and_return(true) subject.should_receive(:notify_observers).with('foo','bar')