<?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>Silveira Neto &#187; RXTX</title>
	<atom:link href="http://silveiraneto.net/tag/rxtx/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Tue, 07 Sep 2010 13:12:43 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Arduino and Java</title>
		<link>http://silveiraneto.net/2009/03/01/arduino-and-java/</link>
		<comments>http://silveiraneto.net/2009/03/01/arduino-and-java/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 02:31:34 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Comm]]></category>
		<category><![CDATA[Dave Brink]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[Robotics]]></category>
		<category><![CDATA[RXTX]]></category>
		<category><![CDATA[Serial]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2344</guid>
		<description><![CDATA[Arduino is a free popular platform for embedded programming based on a simple I/O board easily programmable. Interfacing it with Java allow us to create sophisticated interfaces and take advantages from the several API available in the Java ecosystem. I&#8217;m following the original Arduino and Java interfacing tutorial by Dave Brink but in a more [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino.png"><img class="size-full wp-image-2356 aligncenter" title="Arduino" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino.png" alt="Arduino" width="559" height="316" /></a></p>
<p><a title="Arduino Project" href="http://arduino.cc">Arduino</a> is a free popular platform for embedded programming based on a simple I/O board easily programmable. Interfacing it with Java allow us to create sophisticated interfaces and take advantages from the several API available in the Java ecosystem.</p>
<p><small>I&#8217;m following the original <a title="Dave Brink" href="http://www.arduino.cc/playground/Interfacing/Java">Arduino and Java interfacing tutorial by Dave Brink</a> but in a more practical approach and with more details.</small></p>
<p><strong>Step 1) Install the Arduino IDE</strong></p>
<p>This is not a completely mandatory step but it will easy a lot our work. Our program will borrow some <a title="Arduino Software" href="http://arduino.cc/en/Main/Software">Arduino IDE</a> libraries and configurations  like which serial port it is using and at which <a title="Boud Rate at Wikipedia" href="http://en.wikipedia.org/wiki/Baud">boud rate</a>. At the moment I wrote this tutorial the version of Arduino IDE was 0013.</p>
<p><strong>Step 2) Prepare your Arduino</strong></p>
<p>Connect your Arduino to the serial port in your computer. Here I&#8217;m connecting my Arduino with my laptop throught a USB.</p>
<pre style="text-align: center;" lang="C"><img src="http://farm4.static.flickr.com/3562/3321316792_993cbff274.jpg" alt="Arduino" /></pre>
<p>Make sure your Arduino IDE is configured and communicating well if your Arduino. Let put on it a little program that sends to us a mensage:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;"><span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">9600</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  Serial.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Is there anybody out there?&quot;</span><span style="color: #009900;">&#41;</span>;
  delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p><strong>Step 3) Install RXTX Library</strong></p>
<p>We will use some libraries to acess the serial port, some of them relies on binary implementations on our system. Our first step is to install the <a title="RXTX" href="http://www.rxtx.org/">RXTX library</a> (Java CommAPI) in your system. In a Debian like Linux you can do that by:</p>
<blockquote><p>sudo apt-get install librxtx-java</p></blockquote>
<p>Or using a graphical package tool like Synaptic:</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_00.png"><img class="size-medium wp-image-2345 aligncenter" title="installing rxtx" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_00-500x296.png" alt="installing rxtx" width="500" height="296" /></a></p>
<p>For others systems like Windows see the <a title="RXTX Install" href="http://rxtx.qbang.org/wiki/index.php/Installation">RXTX installation docs</a>.</p>
<p><strong>Step 4) Start a new NetBeans project</strong></p>
<p>Again<strong>,</strong> this is not a mandatory step but will easy a lot our work. <a href="http://netbeans.org">NetBeans</a> is a free and open source Java IDE that will help us to develop our little application. Create a new project at <em>File → New Project</em> and choose at <em>Java</em> at <em>Categories </em>and <em>Java Application</em> at <em>Projects</em>.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_01.png"><img class="size-medium wp-image-2346 aligncenter" title="netbeans new project" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_01-500x323.png" alt="netbeans new project" width="500" height="323" /></a></p>
<p>Chose a name for your project. I called mine <em>SerialTalker</em>.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_02.png"><img class="size-medium wp-image-2347 aligncenter" title="name your project" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_02-500x314.png" alt="name your project" width="500" height="314" /></a></p>
<p><small>At the moment I wrote this tutorial I was using Netbeans version 6.5 and Java 6 update 10 but should work as well on newer and some older versions</small></p>
<p><strong>Step 5) Adding Libraries and a Working Directory<br />
</strong></p>
<p>On NetBeans the <em>Projects</em> tab, right-click your project and choose <em>Properties</em>.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_03.png"><img class="size-medium wp-image-2348 aligncenter" title="libraries" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_03-500x283.png" alt="libraries" width="500" height="283" /></a></p>
<p>On the <em>Project Properties</em> window select the <em>Libraries</em> on the <em>Categories</em> panel.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_04.png"><img class="size-medium wp-image-2349 aligncenter" title="Netbeans project libraries" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_04-499x340.png" alt="Netbeans project libraries" width="499" height="340" /></a></p>
<p>Click the <em>Add JAR/Folder</em> button.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_05.png"><img class="size-medium wp-image-2350 aligncenter" title="arduino directory" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_05-500x307.png" alt="arduino directory" width="500" height="307" /></a></p>
<p>Find where you placed your Arduino IDE installation. Inside this directory there&#8217;s a lib directory will some JAR files. Select all them and click <em>Ok</em>.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_06.png"><img class="size-medium wp-image-2351" title="jars libraries" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_06-500x307.png" alt="jars libraries" width="500" height="307" /></a></p>
<p>As we want to borrow the Arduino IDE configuration the program needs to know where is they configuration files.  There&#8217;s a simple way to do that.</p>
<p>Still in the  <em>Project Properties</em> window select <em>Run</em> at <em>Categories</em> panel. At <em>Working Directory</em> click in the <em>Browse</em> button and select the directory of your Arduino IDE. Mine is at <em>/home/silveira/arduino-0013</em>.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_08.png"><img class="size-medium wp-image-2353 aligncenter" title="Working directory" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_08-499x340.png" alt="Working directory" width="499" height="340" /></a></p>
<p>You can close now the <em>Project Properties</em> window. At this moment in autocomplete for these libraries are enable in your code.</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_07.png"><img class="size-medium wp-image-2352 aligncenter" title="netbeans autocomplete" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_07-500x283.png" alt="netbeans autocomplete" width="500" height="283" /></a></p>
<p><strong>Step 6) Codding and running</strong></p>
<p>Here is the code you can replace at Main.java in your project:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c c" style="font-family:monospace;">package serialtalk;
&nbsp;
import gnu.<span style="color: #202020;">io</span>.<span style="color: #202020;">CommPortIdentifier</span>;
import gnu.<span style="color: #202020;">io</span>.<span style="color: #202020;">SerialPort</span>;
import java.<span style="color: #202020;">io</span>.<span style="color: #202020;">InputStream</span>;
import java.<span style="color: #202020;">io</span>.<span style="color: #202020;">OutputStream</span>;
import processing.<span style="color: #202020;">app</span>.<span style="color: #202020;">Preferences</span>;
&nbsp;
public class Main <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">static</span> InputStream input;
    <span style="color: #993333;">static</span> OutputStream output;
&nbsp;
    public <span style="color: #993333;">static</span> <span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> throws Exception<span style="color: #009900;">&#123;</span>
        Preferences.<span style="color: #202020;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        System.<span style="color: #202020;">out</span>.<span style="color: #202020;">println</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Using port: &quot;</span> <span style="color: #339933;">+</span> Preferences.<span style="color: #202020;">get</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;serial.port&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
        CommPortIdentifier portId <span style="color: #339933;">=</span> CommPortIdentifier.<span style="color: #202020;">getPortIdentifier</span><span style="color: #009900;">&#40;</span>
                Preferences.<span style="color: #202020;">get</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;serial.port&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
        SerialPort port <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SerialPort<span style="color: #009900;">&#41;</span>portId.<span style="color: #202020;">open</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;serial talk&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4000</span><span style="color: #009900;">&#41;</span>;
        input <span style="color: #339933;">=</span> port.<span style="color: #202020;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        output <span style="color: #339933;">=</span> port.<span style="color: #202020;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        port.<span style="color: #202020;">setSerialPortParams</span><span style="color: #009900;">&#40;</span>Preferences.<span style="color: #202020;">getInteger</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;serial.debug_rate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                SerialPort.<span style="color: #202020;">DATABITS_8</span><span style="color: #339933;">,</span>
                SerialPort.<span style="color: #202020;">STOPBITS_1</span><span style="color: #339933;">,</span>
                SerialPort.<span style="color: #202020;">PARITY_NONE</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>input.<span style="color: #202020;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                System.<span style="color: #202020;">out</span>.<span style="color: #202020;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>input.<span style="color: #202020;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
</div>
</div>
<p>Now just compile and run (with your Arduino attached in your serial port and running the program of step 2).</p>
<p style="text-align: center;"><a href="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_09.png"><img class="size-medium wp-image-2354 aligncenter" title="voillá" src="http://silveiraneto.net/wp-content/uploads/2009/03/arduino_java_netbeans_09-500x283.png" alt="voillá" width="500" height="283" /></a></p>
<p>There is. Now you can make your Java programs to talk with your Arduino using a IDE like NetBeans to create rich interfaces.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/03/01/arduino-and-java/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>
