Skip to content

Commit

Permalink
Better handshake for TCP/ser2net. Fix Serial spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Feb 11, 2013
1 parent 5da7bfc commit ff746a0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
16 changes: 12 additions & 4 deletions lib/dino/board.rb
@@ -1,3 +1,5 @@
require 'timeout'

module Dino
class Board
attr_reader :digital_hardware, :analog_hardware
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions lib/dino/tx_rx/base.rb
@@ -1,3 +1,4 @@
require 'timeout'
require 'observer'

module Dino
Expand All @@ -14,9 +15,6 @@ def read
end
end

def flush_read
end

def close_read
return nil if @thread.nil?
Thread.kill(@thread)
Expand Down
6 changes: 6 additions & 0 deletions lib/dino/tx_rx/tcp.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tx_rx/usb_serial_spec.rb
Expand Up @@ -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')

Expand Down

0 comments on commit ff746a0

Please sign in to comment.