<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Embedded Freaks.. &#187; java-serial-port</title>
	<atom:link href="http://embeddedfreak.wordpress.com/tag/java-serial-port/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedfreak.wordpress.com</link>
	<description>Daily embedded life tips and trick</description>
	<lastBuildDate>Sat, 14 Nov 2009 00:47:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='embeddedfreak.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/9f1113cc5558fcab720e54b7e5a379eb?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Embedded Freaks.. &#187; java-serial-port</title>
		<link>http://embeddedfreak.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://embeddedfreak.wordpress.com/osd.xml" title="Embedded Freaks.." />
		<item>
		<title>RXTX Serial Port Helper</title>
		<link>http://embeddedfreak.wordpress.com/2008/10/08/rxtx-serial-port-helper/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/10/08/rxtx-serial-port-helper/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 06:06:09 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=184</guid>
		<description><![CDATA[Well, the conclusion of the RXTX posts in this blog may have been written in this code sample. It will help you to establish serial port operations (connect / disconnect / add data available listener / get serial port&#8217;s OutputStream/InputStream).
I put this on the blog, because I keep moving around project to project. Hence, it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=184&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Well, the conclusion of the RXTX posts in this blog may have been written in this code sample. It will help you to establish serial port operations (connect / disconnect / add data available listener / get serial port&#8217;s OutputStream/InputStream).</p>
<p>I put this on the blog, because I keep moving around project to project. Hence, it always take a lot of time to find it from one of the project folders. At least, if I keep it online, google will help me to find it when I need it.</p>
<p><span id="more-184"></span></p>
<pre class="brush: java;">
/*
 * Copyright (c) 2008, Daniel Widyanto
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the &lt;organization&gt; nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Daniel Widyanto ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Daniel Widyanto BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kunilkuda.sms;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.UnsupportedCommOperationException;
import java.util.TooManyListenersException;

import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Enumeration;

/**
 * \brief Additional help to handle serial connect / disconnect, and registering
 *        interrupt handler
 */
public class SerialHelper {

    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    /**
     * \brief List the available serial ports
     *
     * \return Array of string for the available serial port names
     */
    public static String[] listSerialPorts() {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        ArrayList portList = new ArrayList();
        String portArray[] = null;
        while (ports.hasMoreElements()) {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            if (port.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                portList.add(port.getName());
            }
        }
        portArray = (String[]) portList.toArray(new String[0]);
        return portArray;
    }

    /**
     * \brief Connect to the selected serial port with 57600bps-8N1 mode
     */
    public void connect(String portName) throws IOException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort = (SerialPort) portId.open(&quot;Demo application&quot;, 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection.
            // If they won't open, close the port before throwing an
            // exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * \brief Get the serial port input stream
     * \return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * \brief Get the serial port output stream
     * \return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * \brief Sets the serial port parameters to 57600bps-8N1
     */
    protected void setSerialPortParameters() throws IOException {

        final int baudRate = 57600; // 57600bps

        try {
            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException(&quot;Unsupported serial port parameter&quot;);
        }
    }

    /**
     * \brief Register listener for data available event
     *
     * @param dataAvailableListener The data available listener
     */
    public void addDataAvailableListener(SerialPortEventListener dataAvailableListener)
            throws TooManyListenersException {
        // Add the serial port event listener
        serialPort.addEventListener(dataAvailableListener);
        serialPort.notifyOnDataAvailable(true);
    }

    /**
     * \brief Disconnect the serial port
     */
    public void disconnect() {
        if (serialPort != null) {
            try {
                // close the i/o streams.
                outStream.close();
                inStream.close();
            } catch (IOException ex) {
                // don't care
            }
            // Close the port.
            serialPort.close();
            serialPort = null;
        }
    }

    /**
     * \brief Private inner class to test and debug the SerialHelper class
     */
    public static class SerialHelperTester implements SerialPortEventListener {

        public static void main(String[] args) {
            if (args.length &lt; 1) {
                System.out.println(
                        &quot;Usage: java SerialHelperTester &lt;dataToBeSentToSerialPort&gt;&quot;);
                System.exit(1);
            }

            SerialHelper serialHelper = new SerialHelper();

            checkListSerialPorts(serialHelper);
            checkConnect(serialHelper);
            checkAddDataAvailableListener(serialHelper, args[0]);
            checkDisconnect(serialHelper);
        }

        private static void checkListSerialPorts(SerialHelper serialHelper) {
            System.out.println(&quot;Check the listSerialPorts&quot;);
            System.out.println(&quot;-------------------------&quot;);
            String[] serialPorts = SerialHelper.listSerialPorts();
            if (serialPorts != null) {
                for (int i = 0; i &lt; serialPorts.length; i++) {
                    System.out.println(&quot;Port name: &quot; + serialPorts[i]);
                }
            }
            System.out.println();
        }

        private static void checkConnect(SerialHelper serialHelper) {
            // Replace it with the tested serial port
            final String serialPort = &quot;/dev/ttyACM0&quot;;

            System.out.println(&quot;Connect to serial port&quot;);
            System.out.println(&quot;-------------------------&quot;);
            try {
                serialHelper.connect(serialPort);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

            System.out.println();
        }

        private static void checkAddDataAvailableListener(
                SerialHelper serialHelper, String data) {
            System.out.println(&quot;Check data available listener&quot;);
            System.out.println(&quot;-----------------------------&quot;);

            SerialHelperTester tester =
                    new SerialHelperTester(serialHelper.getSerialInputStream(),
                    serialHelper.getSerialOutputStream());

            try {
                serialHelper.addDataAvailableListener(tester);
            } catch (TooManyListenersException ex) {
                System.err.println(ex.getMessage());
            }

            OutputStream outStream = serialHelper.getSerialOutputStream();
            data = data + &quot;\r&quot;;
            try {
                outStream.write(data.getBytes());
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            }

            try {
                // Sleep for 10-secs
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
            }
        }

        private static void checkDisconnect(SerialHelper serialHelper) {
            System.out.println(&quot;Disconnect from serial port&quot;);
            System.out.println(&quot;---------------------------&quot;);
            serialHelper.disconnect();
            System.out.println(&quot;done&quot;);
        }
        /**
         * Buffer to hold the reading
         */
        private byte[] readBuffer = new byte[400];
        /**
         * I/O stream for serial port
         */
        private InputStream inStream;
        private OutputStream outStream;

        public SerialHelperTester(InputStream inStream, OutputStream outStream) {
            this.inStream = inStream;
            this.outStream = outStream;
        }

        private void readSerial() {
            try {
                int availableBytes = inStream.available();
                if (availableBytes &gt; 0) {
                    // Read the serial port
                    inStream.read(readBuffer, 0, availableBytes);

                    // Print it out
                    System.out.println(&quot;Recv :&quot; +
                            new String(readBuffer, 0, availableBytes));
                }
            } catch (IOException e) {
            }
        }

        public void serialEvent(SerialPortEvent events) {
            switch (events.getEventType()) {
                case SerialPortEvent.DATA_AVAILABLE:
                    readSerial();
            }
        }
    }
}
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/184/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/184/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/184/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=184&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/10/08/rxtx-serial-port-helper/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>RXTX Loopback Test Program(2)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/29/rxtx-loopback-program2/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/29/rxtx-loopback-program2/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 09:56:54 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=113</guid>
		<description><![CDATA[This second loopback program is to test RXTX&#8217;s serial event (data available event). Note that you need serial loopback to use this source code.


import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;

public class LoopbackEventTest {

    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=113&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This second loopback program is to test RXTX&#8217;s serial event (data available event). Note that you need serial loopback to use this source code.<br />
<span id="more-113"></span></p>
<pre class="brush: java;">
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;

public class LoopbackEventTest {

    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    public void connect(String portName) throws IOException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort =
                    (SerialPort) portId.open(&quot;Demo application&quot;, 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection.
            // If they won't open, close the port before throwing an
            // exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * Get the serial port input stream
     * @return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * Get the serial port output stream
     * @return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * Register event handler for data available event
     *
     * @param eventHandler Event handler
     */
    public void addDataAvailableEventHandler(
            SerialPortEventListener eventHandler) {
        try {
            // Add the serial port event listener
            serialPort.addEventListener(eventHandler);
            serialPort.notifyOnDataAvailable(true);
        } catch (TooManyListenersException ex) {
            System.err.println(ex.getMessage());
        }
    }

    /**
     * Disconnect the serial port
     */
    public void disconnect() {
        if (serialPort != null) {
            try {
                // close the i/o streams.
                outStream.close();
                inStream.close();
            } catch (IOException ex) {
                // don't care
                }
            // Close the port.
            serialPort.close();
        }
    }

    /**
     * Sets the serial port parameters to 57600bps-8N1
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 57600; // 57600bps

        try {
            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(
                    SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException(&quot;Unsupported serial port parameter&quot;);
        }
    }

    public static class SerialEventHandler implements SerialPortEventListener {

        private InputStream inStream;
        private int readBufferLen;
        private int readBufferOffset;
        private byte[] readBuffer;

        public SerialEventHandler(InputStream inStream, int readBufferLen) {
            this.inStream = inStream;
            this.readBufferLen = readBufferLen;
            readBuffer = new byte[readBufferLen];
        }

        public boolean isBufferFull() {
            return (readBufferOffset == readBufferLen);
        }

        public String getReadBuffer() {
            return new String(readBuffer);
        }

        public void serialEvent(SerialPortEvent event) {
            switch (event.getEventType()) {
                case SerialPortEvent.DATA_AVAILABLE:
                    readSerial();
                    break;
            }
        }

        private void readSerial() {
            try {
                int availableBytes = inStream.available();
                if (availableBytes &gt; 0) {
                    // Read the serial port
                    readBufferOffset +=
                            inStream.read(readBuffer, readBufferOffset,
                            availableBytes);
                }
            } catch (IOException e) {
            }
        }

        public static void main(String[] args) {
            // Timeout = 1s
            final int TIMEOUT_VALUE = 10000;

            LoopbackTest loopbackTest = new LoopbackTest();
            try {
                // Open serial port
                loopbackTest.connect(&quot;/dev/ttyUSB0&quot;);

                // Register the serial event handler
                String testString = &quot;The quick brown fox jumps over &quot; +
                        &quot;the lazy dog&quot;;
                InputStream inStream =
                        loopbackTest.getSerialInputStream();

                SerialEventHandler serialEventHandler =
                        new SerialEventHandler(inStream, testString.length());
                loopbackTest.addDataAvailableEventHandler(serialEventHandler);

                // Send the testing string
                OutputStream outStream =
                        loopbackTest.getSerialOutputStream();
                outStream.write(testString.getBytes());

                // Wait until all the data is received
                long startTime = System.currentTimeMillis();
                long elapsedTime;
                do {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                    }
                    elapsedTime = System.currentTimeMillis() - startTime;
                } while ((elapsedTime &lt; TIMEOUT_VALUE) &amp;&amp;
                        (!serialEventHandler.isBufferFull()));

                // Check the data if not TIMEOUT
                if (elapsedTime &lt; TIMEOUT_VALUE) {
                    if (testString.equals(serialEventHandler.getReadBuffer())) {
                        System.out.println(&quot;All data is received successfully&quot;);
                    } else {
                        System.out.println(&quot;Test failed&quot;);
                        System.out.println(&quot;Sent:&quot; + testString);
                        System.out.println(&quot;Received:&quot; +
                                serialEventHandler.getReadBuffer());
                    }
                } else {
                    System.err.println(&quot;Timeout&quot;);
                }

                System.out.println(&quot;Test done&quot;);
                loopbackTest.disconnect();
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
}
</pre>
<p>If everything is okay, the data that&#8217;s received should be the same as the data that&#8217;s sent (<em>&#8220;The quick brown fox jumps over the lazy dog&#8221;</em>). If it&#8217;s jumbled than it&#8217;s not okay.</p>
<p>For me, using Linux 2.6 SuSE SLED 10, Java 5, and RXTX 2.1.7, everything is normal (the data is not jumbled). Please post comment if it doesn&#8217;t work for you.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/113/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/113/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=113&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/29/rxtx-loopback-program2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>RXTX Loopback Test Program(1)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/28/rxtx-loopback-test-program1/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/28/rxtx-loopback-test-program1/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 03:36:48 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=105</guid>
		<description><![CDATA[One comment that I had in this site said that the RXTX library is not very reliable in the receiving part (using data available event).
I&#8217;m unsure whether it happens because of the RXTX implementation (in the application side) or RXTX library has internal bug. So, here&#8217;s some simple loopback test program to test the RXTX.

Note [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=105&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://embeddedfreak.wordpress.com/2008/08/13/reading-from-serial-port-using-rxtx/#comment-29">One comment that I had in this site</a> said that the RXTX library is not very reliable in the receiving part (using data available event).</p>
<p>I&#8217;m unsure whether it happens because of the RXTX implementation (in the application side) or RXTX library has internal bug. So, here&#8217;s some simple loopback test program to test the RXTX.<br />
<span id="more-105"></span><br />
Note that you will need the RS232 loopback plug (it&#8217;s pin-3 and pin-2 are connected), like the schematic below, to use the program.</p>
<div class="wp-caption aligncenter" style="width: 178px"><img src="http://www.airborn.com.au/serial/rs232b.gif" alt="RS232 Loopback" width="168" height="169" /><p class="wp-caption-text">RS232 Loopback</p></div>
<pre class="brush: java;">
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;

/**
 * RXTX input/output reliability test
 *
 * @author kunil
 */
public class LoopbackTest {

    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    public void connect(String portName) throws IOException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort =
                    (SerialPort) portId.open(&quot;Demo application&quot;, 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection.
            // If they won't open, close the port before throwing an exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * Get the serial port input stream
     * @return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * Get the serial port output stream
     * @return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * Register event handler for data available event
     *
     * @param eventHandler Event handler
     */
    public void addDataAvailableEventHandler(
            SerialPortEventListener eventHandler) {
        try {
            // Add the serial port event listener
            serialPort.addEventListener(eventHandler);
            serialPort.notifyOnDataAvailable(true);
        } catch (TooManyListenersException ex) {
            System.err.println(ex.getMessage());
        }
    }

    /**
     * Disconnect the serial port
     */
    public void disconnect() {
        if (serialPort != null) {
            try {
                // close the i/o streams.
                outStream.close();
                inStream.close();
            } catch (IOException ex) {
                // don't care
                }
            // Close the port.
            serialPort.close();
        }
    }

    /**
     * Sets the serial port parameters to 57600bps-8N1
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 57600; // 57600bps

        try {
            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(
                    SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException(&quot;Unsupported serial port parameter&quot;);
        }
    }

    @SuppressWarnings(&quot;empty-statement&quot;)
    public static void main(String[] args) {
        // Start the loopback testing
        String testString = &quot;The quick brown fox jumps over the lazy dog&quot;;
        // Timeout: 1s
        final int TIMEOUT_VALUE = 1000;

        // Get the serial port
        LoopbackTest loopbackTest = new LoopbackTest();
        try {
            loopbackTest.connect(&quot;/dev/ttyUSB0&quot;);

            InputStream inStream = loopbackTest.getSerialInputStream();
            OutputStream outStream = loopbackTest.getSerialOutputStream();
            for (int i = 0; i &lt; testString.length(); i++) {
                // Write to serial port
                outStream.write(testString.charAt(i));

                // wait until we got the loopback data
                // Dont forget the timeout
                long startTime = System.currentTimeMillis();
                long elapsedTime;
                do {
                    elapsedTime = System.currentTimeMillis() - startTime;
                } while ((elapsedTime &lt; TIMEOUT_VALUE) &amp;&amp;
                        (inStream.available() == 0));

                if (elapsedTime &lt; TIMEOUT_VALUE) {
                    // Compare the result
                    int readChar = inStream.read();
                    if (readChar != testString.charAt(i)) {
                        System.err.println(&quot;Received: &quot; + readChar +
                                &quot; Sent:&quot; + testString.charAt(i));
                    }
                }
                else {
                    // Get out from the loop..
                    System.err.println(&quot;Received nothing from serial port&quot;);
                    break;
                }
            }
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }

        System.out.println(&quot;Test done\r\n&quot;);
        loopbackTest.disconnect();
    }
}
</pre>
<p>The program will simply send 1-character and wait to receive 1-character to be compared.</p>
<p>The program has not tested the main issue yet (problem with data available event), but this basic test should be ok for any platform and any version of Java / RXTX. I use Linux 2.6 SUSE SLED 10, Java 6, and RXTX 2.1.7 when testing this, and it&#8217;s okay for me.</p>
<p>Please post a comment if it doesn&#8217;t work for you.</p>
<h4>Changelog</h4>
<p>20080829 &#8211; Add timeout, for case where you forget the loopback. Fix the exception.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/105/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/105/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=105&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/28/rxtx-loopback-test-program1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>

		<media:content url="http://www.airborn.com.au/serial/rs232b.gif" medium="image">
			<media:title type="html">RS232 Loopback</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading from serial port (using rxtx)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/13/reading-from-serial-port-using-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/13/reading-from-serial-port-using-rxtx/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 09:29:08 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=83</guid>
		<description><![CDATA[Once you&#8217;ve got the serial InputStream, reading would be easy. Here&#8217;s an example of code to read the serial port using InputStream:

/**
 * Buffer to hold the reading
 */
private byte[] readBuffer = new byte[400];

private void readSerial() {
    try {
        int availableBytes = inStream.available();
   [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=83&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Once you&#8217;ve got the serial InputStream, reading would be easy. Here&#8217;s an example of code to read the serial port using InputStream:</p>
<pre class="brush: java;">
/**
 * Buffer to hold the reading
 */
private byte[] readBuffer = new byte[400];

private void readSerial() {
    try {
        int availableBytes = inStream.available();
        if (availableBytes &gt; 0) {
            // Read the serial port
            inStream.read(readBuffer, 0, availableBytes);

            // Print it out
            System.out.println(
                    new String(readBuffer, 0, availableBytes));
        }
    } catch (IOException e) {
    }
}
</pre>
<p>But the real problem comes from the place where you should place the code.<br />
<span id="more-83"></span><br />
You can put inside the <code>SerialPortEvent.DATA_AVAILABLE:</code> event, like this:</p>
<pre class="brush: java;">
private class SerialEventHandler implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        switch (event.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                readSerial();
                break;
        }
    }
}
</pre>
<p>Don&#8217;t forget to register the event handler, before you run the application</p>
<pre class="brush: java;">
/**
 * Set the serial event handler
 */
private void setSerialEventHandler(SerialPort serialPort) {
    try {
        // Add the serial port event listener
        serialPort.addEventListener(new SerialEventHandler());
        serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException ex) {
        System.err.println(ex.getMessage());
    }
}
</pre>
<p>The result is the read procedure is only called when new data is accepted.</p>
<p>The other way to read the serial port data continously is by using other thread to read:</p>
<pre class="brush: java;">
private class ReadThread implements Runnable {
    public void run() {
        while(true) {
            readSerial();
        }
    }
}
</pre>
<p>Here&#8217;s how you start the thread:</p>
<pre class="brush: java;">
public void setSerialListener() {
    new Thread(new ReadThread()).start();
}
</pre>
<h4>Conclusion</h4>
<p>So, which one that is better, put the reading inside different thread or in the event handler ? Well..I don&#8217;t know about it yet. I&#8217;ll post it once I found the answer.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/83/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/83/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=83&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/13/reading-from-serial-port-using-rxtx/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Serial port event in rxtx</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/12/serial-port-event-in-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/12/serial-port-event-in-rxtx/#comments</comments>
		<pubDate>Tue, 12 Aug 2008 06:26:50 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=66</guid>
		<description><![CDATA[So, what do you expect from serial port event ? Most probably is the notification whether you&#8217;ve got new data in the buffer or not. But for some people (like me) who deals with real modem (I use GSM modem for server communication), we need RTS/CTS notification as well. Otherwise, we will flood the modem [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=66&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So, what do you expect from serial port event ? Most probably is the notification whether you&#8217;ve got new data in the buffer or not. But for some people (like me) who deals with real modem (I use GSM modem for server communication), we need RTS/CTS notification as well. Otherwise, we will flood the modem with data, since most PCs are faster than modem.</p>
<p>I&#8217;ve tested some of rxtx serial port events in Linux, and here&#8217;s some report that you might be interested.<br />
<span id="more-66"></span></p>
<h4>The Theory</h4>
<p>To handle serial port&#8217;s event, you&#8217;ll need a class that implements <a href="http://java.sun.com/products/javacomm/reference/api/javax/comm/SerialPortEventListener.html"><code>SerialPortEventListener</code></a>. The signature for the class is like this:</p>
<pre class="brush: java;">
class SerialListener implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        // do your event handler stuff here
    }
}
</pre>
<p>To determine which kind of serial event that you want to handle (ie. new data received in the buffer, CTS raised, parity error, etc), check the <a href="http://java.sun.com/products/javacomm/reference/api/javax/comm/SerialPortEvent.html"><code>SerialPortEvent</code> documentation</a>. Here&#8217;s some example of how to check whether there&#8217;s new data on buffer or not:</p>
<pre class="brush: java;">
public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
        case SerialPortEvent.DATA_AVAILABLE:
            System.out.println(&quot;Data available&quot;);
            break;
        }
    }
}
</pre>
<p>After that, as usual, if you have event handler, you have to register your handler to the library. Like this:</p>
<pre class="brush: java;">
serialPort.addEventListener(new SerialListener());
</pre>
<p>serialPort is the SerialPort&#8217;s object, while SerialListener is a class that implements <code>SerialPortEventListener</code>. Anyway, this event listener is a bit different, since you have to describe what kind of event that you want to handle. Like this (for example):</p>
<pre class="brush: java;">
serialPort.notifyOnDataAvailable(true);
</pre>
<p>If you want to check other event, other than notify when data available in buffer, check out the SerialPort&#8217;s documentation <a href="http://java.sun.com/products/javacomm/reference/api/javax/comm/SerialPort.html">here</a>.</p>
<p>And that&#8217;s the end of the theory..Let&#8217;s see on what I&#8217;ve found during testing.</p>
<h4>The Test Results</h4>
<ul>
<li><span style="text-decoration:line-through;"><strong>There is no new data event notification </strong></span><strong>(my mistake in testing application..sorry)<br />
</strong></li>
</ul>
<p><span style="text-decoration:line-through;">This is true if you use USB-serial converter, under Linux. It has been reported <a href="http://mailman.qbang.org/pipermail/rxtx/2004-December/2687758.html">here</a>, but we cannot change it since it is linux&#8217;s usbserial driver problem. If you use normal serial port (with /dev/ttyS0 or /dev/ttyS1), this event is reported.</span></p>
<ul>
<li><strong>CTS event works like a charm</strong></li>
</ul>
<p>No matter if you use USB-serial or normal serial port, this event works like a charm. Here&#8217;s a code pieces to make it work:</p>
<pre class="brush: java;">
// Enable CTS event
serialPort.notifyOnCTS(true);

..lots of code

// The CTS event handler
switch (event.getEventType()) {
    case SerialPortEvent.CTS:
        System.out.println(&quot;CTS changed&quot;);
        if (serialPort.isCTS()) {
            System.out.println(&quot;CTS asserted&quot;);
        }
        else {
            System.out.println(&quot;CTS de-asserted&quot;);
        }
        break;
    }
}
</pre>
<ul>
<li><strong>DSR event works perfectly</strong></li>
</ul>
<p>I need DSR to check whether the modem is ready or not to operate. Using my testing program, I confirm that it works perfectly, using USB-serial and normal serial port.</p>
<ul>
<li><strong>No OUTPUT_BUFFER_EMPTY event</strong></li>
</ul>
<p>The event is suppose to be fired if the outputstream buffer is empty, but it is system dependable (not available in all of the platform). Not sure about the other platform, but rxtx on Linux obviously doesn&#8217;t have this event. It would not be triggerred. Futhermore, enabling this event will make other events hang.</p>
<ul>
<li><strong>DATA_AVAILABLE event works like level triggered interrupt</strong></li>
</ul>
<p>Simply means that this event will trigger as long as the data from serial port has not been read.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/66/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/66/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=66&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/12/serial-port-event-in-rxtx/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing to serial port (using Rxtx)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/11/writing-to-serial-port-using-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/11/writing-to-serial-port-using-rxtx/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 07:57:52 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=63</guid>
		<description><![CDATA[Writing to serial port is easy once you&#8217;ve got the serial port&#8217;s outputstream. Just do something like this:

String serialMessage = &#34;AT\r\n&#34;;
OutputStream outstream = serialPort.getOutputStream();
outstream.write(serialMessage.getBytes());

Refer to my previous post for opening and getting the output stream for the serial port.
Additional Notes
From wikibooks Serial Java, comes a good advice, do not use Reader/Writer for AT command, since [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=63&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Writing to serial port is easy once you&#8217;ve got the serial port&#8217;s outputstream. Just do something like this:</p>
<pre class="brush: java;">
String serialMessage = &quot;AT\r\n&quot;;
OutputStream outstream = serialPort.getOutputStream();
outstream.write(serialMessage.getBytes());
</pre>
<p>Refer to my previous post for opening and getting the output stream for the serial port.</p>
<p><strong>Additional Notes</strong></p>
<p>From <a title="Serial Java" href="http://en.wikibooks.org/wiki/Serial_Programming:Serial_Java">wikibooks Serial Java</a>, comes a good advice, do not use Reader/Writer for AT command, since Reader/Writer will translate the AT command into the Unicode (your code might work as it is, or might not work if you use different Unicode than latin/ISO-8859-1 as default Unicode in the system).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/63/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/63/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=63&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/11/writing-to-serial-port-using-rxtx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Managing Serial Port Ownership (in rxtx)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/09/managing-serial-port-ownership-in-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/09/managing-serial-port-ownership-in-rxtx/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 10:44:39 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=60</guid>
		<description><![CDATA[Okay..I want to build an application that is nice enough to other application. So, if other application asking my port ownership, my application may give it (or not, depends on the user response).
I may also need to be notified, if I get or lose the port&#8217;s ownership. Therefore, I can do connection retry, before sending [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=60&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Okay..I want to build an application that is nice enough to other application. So, if other application asking my port ownership, my application may give it (or not, depends on the user response).</p>
<p>I may also need to be notified, if I get or lose the port&#8217;s ownership. Therefore, I can do connection retry, before sending the data to serial port that I don&#8217;t own.</p>
<p>Hence, I&#8217;ve done some simple test on rxtx, and the result is quite suprising.<br />
<span id="more-60"></span></p>
<h4>The Theory</h4>
<p>To handle the ownership event, you&#8217;ll need a class that implements CommPortOwnershipListener. Something like this:</p>
<pre class="brush: java;">
class SerialOwnershipHandler implements CommPortOwnershipListener
{
    public void ownershipChange(int type) {
        switch (type) {
            case CommPortOwnershipListener.PORT_OWNED:
                System.out.println(&quot;We got the port&quot;);
                break;
            case CommPortOwnershipListener.PORT_UNOWNED:
                System.out.println(&quot;We've just lost our port ownership&quot;);
                break;
            case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
                System.out.println(&quot;Someone is asking our port's ownership&quot;);
                break;
        }
    }
}
</pre>
<p>After that, we need to register our event handler to CommPortIdentifier class (using CommPortIdentifier&#8217;s addPortOwnershipListener() method).</p>
<p>And, here&#8217;s what the class supposed to do:</p>
<ul>
<li>If the port has just been owned (ie. CommPortIdentifier&#8217;s open() method is called and success), the <code>CommPortOwnershipListener.PORT_OWNED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;We got the port&#8221;</em> to the stdout.</li>
<li>If the port has just been released (ie. SerialPort&#8217;s close() method is called), the <code>CommPortOwnershipListener.PORT_UNOWNED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;We&#8217;ve just lost our port ownership&#8221;</em> to the stdout.</li>
<li>If other application asks our port&#8217;s ownership, the <code>CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;Someone is asking our port&#8217;s ownership&#8221;</em> to the stdout.</li>
</ul>
<h4>The Test Result</h4>
<ul>
<li><span style="color:#00ff00;"><strong>CORRECT</strong></span>: If the port has just been owned (ie. CommPortIdentifier&#8217;s open() method is called and success), the <code>CommPortOwnershipListener.PORT_OWNED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;We got the port&#8221;</em> to the stdout.</li>
<li><span style="color:#00ff00;"><strong>CORRECT</strong></span>: If the port has just been released (ie. SerialPort&#8217;s close() method is called), the <code>CommPortOwnershipListener.PORT_UNOWNED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;We&#8217;ve just lost our port ownership&#8221;</em> to the stdout.</li>
<li><span style="color:#ff0000;"><strong>FALSE</strong></span>: If other application asks our port&#8217;s ownership, the <code>CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED</code> will be returned in <code>ownershipChange()</code>, printing <em>&#8220;Someone is asking our port&#8217;s ownership&#8221;</em> to the stdout.</li>
</ul>
<p>For the last case, where other application is run after rxtx get the ownership, I&#8217;ve tried using GNU screen (<code>screen /dev/ttyUSB0 57600</code>) and other rxtx application demo. I couldn&#8217;t generate the third case at all.</p>
<p>After several trial, I found out that the last case can only be done, if the CommPortIdentifier&#8217;s open() method is called more than once in the <strong>SAME</strong> application (same process ID..even other java program with rxtx cannot generate the last case because it used different process ID).</p>
<p>In case someone interested in the test program that I use, here&#8217;s the source code</p>
<pre class="brush: java;">

public class SerialOwnership {

    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    public void connect(String portName, String id) throws IOException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Add ownership event listener
            portId.addPortOwnershipListener(new SerialOwnershipHandler(id));

            // Get the port's ownership
            serialPort =
                    (SerialPort) portId.open(&quot;Demo application&quot;, 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection. If they won't
            // open, close the port before throwing an exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * Get the serial port input stream
     * @return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * Get the serial port output stream
     * @return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    public void disconnect() {
        if (serialPort != null) {
            try {
                // close the i/o streams.
                outStream.close();
                inStream.close();
            } catch (IOException ex) {
                // don't care
            }
            // Close the port.
            serialPort.close();
        }
    }

    /**
     * Sets the serial port parameters
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 57600; // 57600bps

        try {
            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(
                    SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException(&quot;Unsupported serial port parameter&quot;);
        }
    }

    private class SerialOwnershipHandler implements CommPortOwnershipListener
    {
        String id;

        public SerialOwnershipHandler(String id) {
            this.id = id;
        }

        public void ownershipChange(int type) {
            switch (type) {
                case CommPortOwnershipListener.PORT_OWNED:
                    System.out.println(id +
                            &quot;:We got the port&quot;);
                    break;
                case CommPortOwnershipListener.PORT_UNOWNED:
                    System.out.println(id +
                            &quot;:We've just lost our port ownership&quot;);
                    break;
                case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED:
                    System.out.println(id +
                            &quot;:Someone is asking our port's ownership&quot;);
                    break;
            }
        }
    }

    public static void main(String[] args) {
        SerialOwnership serialOwnershipTest = new SerialOwnership();

        try {
            serialOwnershipTest.connect(&quot;/dev/ttyUSB0&quot;, &quot;1&quot;);
            Thread.sleep(10000);

            // Uncomment this to generate the third case
            //new SerialOwnership().connect(&quot;/dev/ttyUSB0&quot;, &quot;2&quot;);
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        }

        serialOwnershipTest.disconnect();
    }
}
</pre>
<p>This test is run on Linux Suse Enterprise Desktop 10 (kernel 2.6.16), Sun Java 1.6.0, and Rxtx 2.1.7</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/60/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/60/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=60&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/09/managing-serial-port-ownership-in-rxtx/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>How to close serial port in rxtx</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/08/how-to-close-serial-port-in-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/08/how-to-close-serial-port-in-rxtx/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 08:18:21 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=45</guid>
		<description><![CDATA[Well, you&#8217;ve opened the port. But how do you clean up your mess once you&#8217;ve done with the serial port ?
Here&#8217;s some hints to clean up your serial port mess


    public void disconnect() {
        if (serialPort != null) {
       [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=45&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Well, you&#8217;ve opened the port. But how do you clean up your mess once you&#8217;ve done with the serial port ?</p>
<p>Here&#8217;s some hints to clean up your serial port mess<br />
<span id="more-45"></span></p>
<pre class="brush: java;">
    public void disconnect() {
        if (serialPort != null) {
            try {
                // close the i/o streams.
                outStream.close();
                inStream.close();
            } catch (IOException ex) {
                // don't care
            }
            // Close the port.
            serialPort.close();
        }
    }
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=45&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/08/how-to-close-serial-port-in-rxtx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>How to open serial port (using rxtx)</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/08/how-to-open-serial-port-using-rxtx/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/08/how-to-open-serial-port-using-rxtx/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 07:36:22 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=37</guid>
		<description><![CDATA[Ok. So here&#8217;s a brief explanation of how to open serial port in Java using rxtx.
First of all, you need to get the serial port&#8217;s ID

CommPortIdentifier portId =
  CommPortIdentifier.getPortIdentifier(&#34;/dev/ttyUSB0&#34;);

Then, you&#8217;ll need to ask the OS to give the serial port ownership to you.

SerialPort serialPort =
  (SerialPort) portId.open(&#34;Demo application&#34;, 5000);


The &#8220;Demo application&#8221; string is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=37&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ok. So here&#8217;s a brief explanation of how to open serial port in Java using rxtx.</p>
<p>First of all, you need to get the serial port&#8217;s ID</p>
<pre class="brush: java;">
CommPortIdentifier portId =
  CommPortIdentifier.getPortIdentifier(&quot;/dev/ttyUSB0&quot;);
</pre>
<p>Then, you&#8217;ll need to ask the OS to give the serial port ownership to you.</p>
<pre class="brush: java;">
SerialPort serialPort =
  (SerialPort) portId.open(&quot;Demo application&quot;, 5000);
</pre>
<p><span id="more-37"></span><br />
The &#8220;Demo application&#8221; string is the application&#8217;s name. The rxtx will pass this value to OS as the application name who ask the serial port ownership (<span style="text-decoration:line-through;">I think there&#8217;s some Unix command to view which application hold which system resource..I need to dig more on this</span> use <code>lsof /dev/&lt;your serial device&gt;</code> to see which application that holds it. But I should remind you that &#8220;Demo application&#8221; string won&#8217;t come up using <code>lsof</code> or <code>ps -ax</code>).</p>
<p>The 5000 value is the timeout value that is given to the system to release the port (5000 ms = 5 seconds). If the current owner of the serial port doesn&#8217;t release the serial port within 5 seconds, rxtx will throw an <code>IOException</code>.</p>
<p>If you&#8217;re lucky (the current serial port owner willing to give the serial port to you, or nobody owns it currently), you can set the serial port parameters.</p>
<pre class="brush: java;">
int baudRate = 57600; // 57600bps
try {
  // Set serial port to 57600bps-8N1..my favourite
  serialPort.setSerialPortParams(
    baudRate,
    SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException ex) {
  System.err.println(ex.getMessage());
}
</pre>
<p>Don&#8217;t forget to set its flow control</p>
<pre class="brush: java;">
try {
  serialPort.setFlowControlMode(
        SerialPort.FLOWCONTROL_NONE);
  // OR
  // If CTS/RTS is needed
  //serialPort.setFlowControlMode(
  //      SerialPort.FLOWCONTROL_RTSCTS_IN |
  //      SerialPort.FLOWCONTROL_RTSCTS_OUT);
} catch (UnsupportedCommOperationException ex) {
  System.err.println(ex.getMessage());
}
</pre>
<p>Lastly, before you can read or write to serial port. You&#8217;ll need its inputstream and outputstream.</p>
<pre class="brush: java;">
OutputStream outStream = serialPort.getOutputStream();
InputStream inStream = serialPort.getInputStream();
</pre>
<p>So, here&#8217;s the complete example of the method to open the serial port</p>
<pre class="brush: java;">
public class SerialPortHandler {
    private SerialPort serialPort;
    private OutputStream outStream;
    private InputStream inStream;

    public void connect(String portName) throws IOException {
        try {
            // Obtain a CommPortIdentifier object for the port you want to open
            CommPortIdentifier portId =
                    CommPortIdentifier.getPortIdentifier(portName);

            // Get the port's ownership
            serialPort =
                    (SerialPort) portId.open(&quot;Demo application&quot;, 5000);

            // Set the parameters of the connection.
            setSerialPortParameters();

            // Open the input and output streams for the connection. If they won't
            // open, close the port before throwing an exception.
            outStream = serialPort.getOutputStream();
            inStream = serialPort.getInputStream();
        } catch (NoSuchPortException e) {
            throw new IOException(e.getMessage());
        } catch (PortInUseException e) {
            throw new IOException(e.getMessage());
        } catch (IOException e) {
            serialPort.close();
            throw e;
        }
    }

    /**
     * Get the serial port input stream
     * @return The serial port input stream
     */
    public InputStream getSerialInputStream() {
        return inStream;
    }

    /**
     * Get the serial port output stream
     * @return The serial port output stream
     */
    public OutputStream getSerialOutputStream() {
        return outStream;
    }

    /**
     * Sets the serial port parameters
     */
    private void setSerialPortParameters() throws IOException {
        int baudRate = 57600; // 57600bps

        try {
            // Set serial port to 57600bps-8N1..my favourite
            serialPort.setSerialPortParams(
                    baudRate,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(
                    SerialPort.FLOWCONTROL_NONE);
        } catch (UnsupportedCommOperationException ex) {
            throw new IOException(&quot;Unsupported serial port parameter&quot;);
        }
    }
}
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/37/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/37/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=37&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/08/how-to-open-serial-port-using-rxtx/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Rxtx without installing it to JAVA_HOME</title>
		<link>http://embeddedfreak.wordpress.com/2008/08/08/using-rxtx-without-installing-it-to-java_home/</link>
		<comments>http://embeddedfreak.wordpress.com/2008/08/08/using-rxtx-without-installing-it-to-java_home/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 03:24:34 +0000</pubDate>
		<dc:creator>kunilkuda</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[java-serial-port]]></category>
		<category><![CDATA[rxtx]]></category>

		<guid isPermaLink="false">http://embeddedfreak.wordpress.com/?p=24</guid>
		<description><![CDATA[Sometimes, it is very annoying to know that you just cannot install your Rxtx library in the JAVA_HOME (lack of admin privilege, you don&#8217;t want to clutter your JAVA_HOME with trashes, you need easy uninstallation, etc).
So, I&#8217;ve looked around in JNI usage, and I found a way to handle this problem.


Put your Rxtx library (RXTXcomm.jar, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=24&subd=embeddedfreak&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes, it is very annoying to know that you just cannot install your Rxtx library in the JAVA_HOME (lack of admin privilege, you don&#8217;t want to clutter your JAVA_HOME with trashes, you need easy uninstallation, etc).</p>
<p>So, I&#8217;ve looked around in JNI usage, and I found a way to handle this problem.</p>
<p><span id="more-24"></span></p>
<ol>
<li>Put your Rxtx library (RXTXcomm.jar, librxtxParallel.so, librxtxSerial.so) in the local project directory.</li>
<li>Run your project with this argument: -Djava.library.path=&lt;your local Rxtx project directory&gt;</li>
</ol>
<p>If you use Netbeans, you can add the argument in: Project Properties -&gt; Run -&gt; VM Options</p>
<div id="attachment_25" class="wp-caption aligncenter" style="width: 310px"><a href="http://embeddedfreak.files.wordpress.com/2008/08/projectproperties.png"><img class="size-medium wp-image-25" src="http://embeddedfreak.files.wordpress.com/2008/08/projectproperties.png?w=300&#038;h=212" alt="Netbeans Project Properties" width="300" height="212" /></a><p class="wp-caption-text">Netbeans Project Properties</p></div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/embeddedfreak.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/embeddedfreak.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/embeddedfreak.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/embeddedfreak.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/embeddedfreak.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/embeddedfreak.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/embeddedfreak.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/embeddedfreak.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/embeddedfreak.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/embeddedfreak.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/embeddedfreak.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/embeddedfreak.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=embeddedfreak.wordpress.com&blog=1740165&post=24&subd=embeddedfreak&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://embeddedfreak.wordpress.com/2008/08/08/using-rxtx-without-installing-it-to-java_home/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/13b3ee554fe96a750148b9819d734843?s=96&#38;d=&#38;r=G" medium="image">
			<media:title type="html">kunilkuda</media:title>
		</media:content>

		<media:content url="http://embeddedfreak.files.wordpress.com/2008/08/projectproperties.png?w=300" medium="image">
			<media:title type="html">Netbeans Project Properties</media:title>
		</media:content>
	</item>
	</channel>
</rss>