<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jochen Toppe&#039;s Blog &#187; Rails</title>
	<atom:link href="http://jtoee.com/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jtoee.com</link>
	<description>Putting the &#62;O&#60; in technology</description>
	<lastBuildDate>Mon, 22 Feb 2010 21:48:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Talking to an Arduino from Ruby/Rails</title>
		<link>http://jtoee.com/2009/12/talking-to-an-arduino-from-rubyrails/</link>
		<comments>http://jtoee.com/2009/12/talking-to-an-arduino-from-rubyrails/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 13:36:26 +0000</pubDate>
		<dc:creator>Jochen</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://jtoee.com/?p=671</guid>
		<description><![CDATA[As I&#8217;m in the process of building a full home-automation suite so I can finally open my garage door with my iPhone (stay tuned, more on that soon), I wanted to quickly post this since it actually wasn&#8217;t really easily found online and I spent a few minutes (ok, more like an hour) fiddling with [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m in the process of building a full home-automation suite so I can finally open my garage door with my iPhone (stay tuned, more on that soon), I wanted to quickly post this since it actually wasn&#8217;t really easily found online and I spent a few minutes (ok, more like an hour) fiddling with this before I got it to work.</p>
<p>The code below takes data (you should pass in numeric 8-bit values, i.e. between 0&#215;0 and 0xFF) and then read them out on the Arduino as usual. As a general note, working in dynamically typed language like Ruby is a major pain in the a** when you&#8217;re trying to fiddle with every bit and work with an 8-bit microcontroller on the other side of the wire. On a positive note,  Ruby has bit manipulation capabilities and it&#8217;s just so easy to build webapps with it&#8230;</p>
<p>The gist of the code below: Put the data into an array, pack it as unsigned characters, and then pump it into the serial port. The code below requires the ruby-serial gem.</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">class</span> SerialGateway

  def initialize()
    puts <span class="str">"Starting serial gateway"</span>
    begin
      # 9600, 8N1 on USB0
      @sp = SerialPort.<span class="kwrd">new</span> <span class="str">"/dev/ttyUSB0"</span>, 9600, 8, 1, SerialPort::NONE
      # release port on shutdown
      at_exit { do_at_exit() }
    rescue =&gt; e
      # sophisticated error handling
      puts <span class="str">"Cannot initialize usb device"</span>
    end
    # only one serial access per time
    @send_mutex = Mutex.<span class="kwrd">new</span>
  end

  #
  # Send a command to the master device.
  #
  def sendCommand(command, targetAddress, data=[])

    <span class="kwrd">if</span>(data.length &gt;4)
      raise <span class="str">"Sorry, maximum command data length is 4."</span>
    end    

    # first one ASCII R (remote, 0x52)
    cmd = [ 0x52, command, targetAddress, data.size()]
    # add data array
    data.each { |d| cmd &lt;&lt; d}
    # pack it into unsigned chars
    cmd = cmd.pack(<span class="str">"C*"</span>)

    @send_mutex.synchronize <span class="kwrd">do</span>
     cmd.each_byte { |c| @sp.putc c }
    end

  end

end</pre>
<p><br class="spacer_" /></p>
<p>Oh, and flipping back and forth between Ruby and C is a little weird :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://jtoee.com/2009/12/talking-to-an-arduino-from-rubyrails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
