Skip to content

Commit

Permalink
improve robustness of arduino firmware and component
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoufkirsh committed Jul 26, 2012
1 parent cf115b8 commit 89f2a41
Show file tree
Hide file tree
Showing 5 changed files with 2,061 additions and 2,027 deletions.
30 changes: 27 additions & 3 deletions components/arduino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) << "/LibComponent.rb"
require 'serialport'
require 'timeout'

# arg declaration -- Needed to generate --introspect phase

Expand All @@ -26,16 +27,39 @@ def initialize(component_, port_,baup_,voltage_)
rescue
@component.quit_server(10, "From arduino component: #{port_} did not opened correctly")
end
# try to etablish connection with firmware
begin
Timeout::timeout(3) do # allow a maximum of 1s for response
begin
# Try to send a command and wait for response
# For some reason, arduino need time after etablishing the connection
# repeate until response
Timeout::timeout(0.2) do
write_and_read("255")
end
rescue Timeout::Error
retry
rescue
@component.quit_server(10, "Communication with arduino board failed")
end
end
rescue Timeout::Error
@component.quit_server(10, "Arduino board did not respond in time")
end
end

def write(string_)
@sp.write(string_+ ";")
end

def write_and_read(string_)
write(string_)
val = @sp.gets.split(" ").reverse[0]
return val
# allow a maximum of 1s for response
# if fail, dbus will return the error
Timeout::timeout(1) do
write(string_)
val = @sp.gets.split(" ").reverse[0]
return val
end
end

def read
Expand Down
Loading

0 comments on commit 89f2a41

Please sign in to comment.