Embedded Freaks..

Java Serial Port (RxTx) Trail

This page contains links to posts about RXTX/Java Comm that I’ve made in this blog. It seems like easier for me (and you, the readers) to have one big table of contents with small explanation of what’s inside, rather than walk through the search page/google/wordpress tags to find specific issue.

So, here we go.

  1. Enumerating serial port (using RxTX)
    Detecting the serial ports that is available in the system, including how to force the library to detect special serial port
  2. Using Rxtx without installing it to JAVA_HOME
    The RxTx installation instruction asks us to install the library into the system. This caveat enables you to install it into the local directory, avoiding conflicts and headache when multiple RxTx/Java Comm libraries are deployed in the same system.
  3. How to open serial port (using rxtx)
  4. How to close serial port in rxtx
  5. Managing Serial Port Ownership (in rxtx)
    Manages the port ownership, and its functional test. Special issue for multiple processes that want to access single serial port
  6. Writing to serial port (using Rxtx)
  7. Serial port event in rxtx
    Serial port events in rxtx: its types, usage, and functionality test
  8. Reading from serial port (using rxtx)
    Read serial port data, using DATA_AVAILABLE interrupt/event or continuous polling over special thread
  9. RXTX Loopback Test Program(1)
    Test the rxtx read/write functionality using loopback device and single thread.
  10. RXTX Loopback Test Program(2)
    Test the rxtx read/write functionality using loopback device, multiple threads and serial event
  11. RXTX Serial Port Helper
    My helper class to manage the serial port over the application. It contains the code from the previous posts.


  1. Daniel,

    I am not an experienced Java developer, but managed to create a webservice reading weight information (after writing a command to the output stream) from a serial port using RXTX on Windows XP OS. Now I have an issue when the serial port is disconnected from the weighbridge, writing the output stream causes the process to hang. It seems the IOException is not thrown or a different exception handling is needed? Do you have any comment on this?

    Comment by Andre den Hamer — August 12, 2010 @ 11:33 pm

  2. Hello,

    I have some questions for you regarding the porting of RxTx to ARM for use on an Android device.

    I have downloaded the source, rxtx-2.1-7r2, but am unsure as to where to begin. I have only tried using the prebuilt Android toolchain, as well as Codesourcery’s arm-2010q1-188-arm-none-eabi-i686-pc-linux-gnu, but have had no luck.

    I receive the error that “C compiler is unable to create executables”, and Google’ing this has not been of any help.

    Please, if you have any ideas as to how or where I should start, kindly advise.

    Sincerely, all the best,


    Comment by Vincent Pascuzzi — September 3, 2010 @ 11:42 pm

  3. “Notify me of follow-up comments via email”…

    Sorry, forgot to check that box!


    Comment by Vincent Pascuzzi — September 3, 2010 @ 11:46 pm

  4. Hi,

    I have 2 serial ports on my computer and I am using RXTX to read and write to them *seperately*. My code snippet is as follows

    if ( (commPort1 instanceof SerialPort) && (commPort2 instanceof SerialPort) )
    SerialPort serialPort1 = (SerialPort) commPort2;

    SerialPort serialPort2 = (SerialPort) commPort2;

    InputStream in_p1 = serialPort1.getInputStream();
    OutputStream out_p1 = serialPort1.getOutputStream();

    InputStream in_p2 = serialPort2.getInputStream();
    OutputStream out_p2 = serialPort2.getOutputStream();

    (new Thread(new SerialReader(“MOTE-1”, in_p1))).start();
    System.out.println(“Started serial reader 1”);
    (new Thread(new SerialWriter(“MOTE-1”, out_p1, write_to_mote))).start();
    System.out.println(“Started serial writer 1”);

    (new Thread(new SerialReader(“MOTE-2”,in_p2))).start();
    System.out.println(“Started serial reader 2”);
    (new Thread(new SerialWriter(“MOTE-2”, out_p2, write_to_mote2))).start();
    System.out.println(“Started serial writer 2”);

    System.out.println(“Successfully connected to Cricket mote on ports “+ port1Name +” and “+ port2Name);

    The SerialReader and SerialWriter are Runnable classes as defined in the example on the RXTX wiki.

    The problem is that there seems to be an intermingling of the data read and written to the two ports. For example, a test byte-stream coming on port 1, will have some of the characters on the SerialReader of port 2 as well. Similarly, when I use the SerialWriter tp write some bytes to Port 1, I find that some (if not all of them) are being written on the SerialWriter of Port 2.

    I guess it has to do with the thread-safe aspect of RXTX and I understand that it is not thread-safe.

    Is there a possible solution for this ?


    Comment by Muzammil — March 31, 2011 @ 7:06 am

  5. Vince,

    Take a look here http://v-lad.org/projects/gnu.io.android/, I have ported the rxtx library to the Android device. It works for me on the Motorolla Droid haven’t tried any other devices. The link above contains prebuilt native library and a link to the github repository that contains the source.

    Comment by Vlad Korolev — March 31, 2011 @ 4:13 pm

  6. Hi

    Vincent Pascuzzi, did you find something for RxTx on ARM android? I am in the same situation, and the port of vladistan works but it hangs after a few sconds (out of memory i guess).

    Comment by adrien — May 2, 2011 @ 6:24 pm

  7. Hello Vlad, Adrien.

    I have, since September of last year, discontinued the implementation of RxTx for use with Android, and instead have switched to Bluetooth as it is natively supported and appears to work well for my needs.

    However, I do very much appreciate the follow-up replies and may be able to get around to try RxTx again in the future.

    @adrien: Sorry, I have not been able to test the project from Vlad. Hopefully I will get around to it soon.

    All the best,


    Comment by Vincent Pascuzzi — May 10, 2011 @ 9:46 pm

  8. […] Java Serial Port (RxTx) Trail […]

    Pingback by Java Serial Port (RxTx) Trail « Yosua Onesimus Bookmarks — May 23, 2011 @ 10:35 pm

  9. Hi

    very nice RXTX code examples.

    Comment by Abhijit Aitwade — August 19, 2011 @ 3:06 pm

  10. […] Java Serial Port (RxTx) Trail Embedded Freaks.. avrdude: Yikes! Invalid device signature. Arduino playground – Fedora RPM Fedora 16 rxtx 2.2 […]

    Pingback by arduino 1.0 Fedora 16 java 類比讀取 | Y12 Studio — February 15, 2012 @ 10:53 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: