Java Comm Serial API How-To for Linux

Using RXTX and JCL

Preface

Special thanks to Trent Jarvi and Kevin Hester for putting together RXTX and JCL.  These two gentlemen have done an excellent job of porting the original Sun Java Comm API to the Linux OS.  However, with all due respect, and not to be insulting, their installation documentation leaves much to be desired.  This is the primary reason I have chosen to put this document together.

Getting Started

Before insalling the Comm API on your Linux box, be sure you have a JSDK installed.  You can obtain a JSDK for your version of linux from Blackdown.org or Sun Microsystems.  To install and configure Java in Linux, please follow the instructions provided here.

For the purposes of this paper, it will be assumed that you have installed a JSDK and it is located in:

/usr/java/j2sdk1.4.0/

And java is then located in:

/usr/java/j2sdk1.4.0/bin/

If your setup is different, please adjust accordingly. 

Installing RXTX

First, obtain the RXTX bins package from:

http://www.linux.org.uk/~taj/rxtx-bins.1.tar.gz
                        (In IE, right click and "Save Target As")

Decompress and Untar this package:

/bin/gzip --decompress rxtx-bins.1.tar.gz

/bin/tar xf rxtx-bins.1.tar

At this point, you'll have an rxtx-bins.1 directory.  Next, you'll need to copy the shared objects into your java installation:

cp rxtx-bins.1/1.4/i386-pc-linux/libParallel.so /usr/java/j2sdk1.4.0/jre/lib/i386/

cp rxtx-bins.1/1.4/i386-pc-linux/libSerial.so /usr/java/j2sdk1.4.0/jre/lib/i386/

If you are installing on an architecture other than an x86, you'll need to adjust both the /i386-pc-linux/ and the /i386/ accordingly.

Next, you'll need to install the jcl.jar file:

cp rxtx-bins.1/1.4/jcl.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, the RXTX installation is complete.

Installing Comm

The final step to getting the Java Comm API working under Linux, is to install the Comm API itself.  At this point, you have all of the necessary kernel-level drivers installed.  Because Linux is a form of Unix, the authors of RXTX and JCL have wisely decided to reuse sun's solaris (unix) comm library.  At this point, you must download and install this library:

http://java.sun.com/products/javacomm/  

Make sure you choose the Solaris/SparcTM version.  Next, you must Decompress and Untar this package:

/bin/gzip --decompress javax_comm-2_0_2-solsparc.tar.Z

/bin/tar xf javax_comm-2_0_2-solsparc.tar

Next, you'll need to install the comm.jar file:

cp commapi/comm.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, we are almost finished.  We just need to create the properties file that the Comm API will use to load the drivers (.so files).  To create this file, type the following command:

/bin/echo Driver=gnu.io.RXTXCommDriver > /usr/java/j2sdk1.4.0/jre/lib/javax.comm.properties

Congratulations!  You have installed the Linux Comm API.

Tips

While windows uses COM and LPT designators for port identifiers, Linux is a bit different.  Use the following table to identify your ports:

Port Windows port identifier What you use in Linux
Serial Port 1 COM1 /dev/ttyS0
Serial Port 2 COM2 /dev/ttyS1
Parallel Port 1 LPT1 /dev/lp0

Troubleshooting

Several people have emailed me and reported that they get the following error:

Exception in thread "main" java.lang.VerifyError: (class:
gnu/io/RXTXPort$SerialOutputStream, method: write signature: ([BII)V) Illegal
use of nonvirtual function call
at gnu.io.RXTXPort.<init>(RXTXPort.java)
at gnu.io.RXTXCommDriver.getCommPort(RXTXCommDriver.java)
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:547)


I personally have not experienced this problem, but if you encounter this error, you can work around it by adding -noverify to your command line:

java -classpath your classpath -noverify your class

Also, it has been suggested that recompiling the jcl with the java compiler you have chosen will resolve this issue.  As I have never experienced this verification problem, I have no way to test this.  It should also be noted that the purpose of this paper is to be a quick and easy way to get serial and parallel port access in Java on Linux.  Most of the instructions on this page as well as the VerifyError are irrelevant if you chose to download the RXTX source and do a manual compile and installation.

Other Resources

  1. Trent Jarvi's RXTX Homepage.  (http://www.rxtx.org)
  2. Kevin Hester's JCL Homepage.  (http://www.geeksville.com/~kevinh/linuxcomm.html)
  3. Sun Microsystem's Java Comm FAQ.  (http://java.sun.com/products/javacomm/reference/faqs/index.html)

Reader Comments:

11/07/2009 7:30 AM - Krmelsvq wrote:
comment1

11/07/2009 2:08 AM - Enduylqj wrote:
comment5

11/06/2009 8:45 PM - Vqpztyjo wrote:
comment5

11/06/2009 3:56 PM - Qiawltsy wrote:
comment6

11/06/2009 3:55 PM - Qiawltsy wrote:
comment6

11/06/2009 10:59 AM - Tijmlqau wrote:
comment6

11/06/2009 5:39 AM - Qeztqndh wrote:
comment6

11/06/2009 12:39 AM - Nssxjouw wrote:
comment6

11/05/2009 7:37 PM - Wijayxsj wrote:
comment4

11/05/2009 2:37 PM - Ytsjranw wrote:
comment3

10/31/2009 2:00 PM - SchneiderIS wrote:
Pardon the blond moment. The permissions to write to the "lib" folder were needed. Then the file could be created.

10/31/2009 1:50 PM - SchneiderIS wrote:
In running your instructions I have run into a snag with the very last step for the Sun comm driver. When I try running: sudo /bin/echo Driver=gnu.io.RXTXCommDriver > /usr/lib/jvm/java-6-sun/jre/lib/javax.comm.properties I get the following error: bash: /usr/lib/jvm/java-6-sun/jre/lib/javax.comm.properties: Permission denied Looking in the "lib" folder there is no "javax.comm.properties" and when I search the drive for the file it does not exist. I have the latest download from Sun which is version 3 for comm and my java version is as follows: java version "1.6.0_16" Java(TM) SE Runtime Environment (build 1.6.0_16-b01) Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing) Do you have any suggestions as to what needs to be changed for this final command to register the driver?

10/09/2009 5:38 AM - Gunjan Nigam wrote:
Can u explain the error java.lang.ClassCastException: gnu.io.RXTXCommDriver cannot be cast to gnu.io.CommDriver thrown while loading gnu.io.RXTXCommDriver Experimental: JNI_OnLoad called. gnu.io.NoSuchPortException at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218) at TwoWaySerialComm.connect(TwoWaySerialComm.java:17) at TwoWaySerialComm.main(TwoWaySerialComm.java:104)

09/14/2009 11:47 PM - xJWZqGeiFZB wrote:
dors-andrey-viagra1.txt;2;3

09/14/2009 8:11 PM - WmXoZfMbvDtNTeX wrote:
dors-andrey-viagra1.txt;3;10

07/09/2009 7:21 PM - Rciogeaz wrote:
Jonny was here

06/22/2009 3:07 PM - nataly wrote:
77t7RZ ghUnxCczpf72ndOqi20g

06/18/2009 7:14 AM - jeremia wrote:
Xcyjhl bjdBa51p9Nnfla94Mql

06/15/2009 5:17 PM - Nxsnoror wrote:
Very funny pictures

06/14/2009 3:39 AM - Udugaqhc wrote:
magic story very thanks

06/09/2009 2:16 PM - coolman wrote:
Thanks funny site

06/09/2009 2:16 PM - dogkill wrote:
I'm happy very good site

06/09/2009 2:16 PM - Barbera wrote:
Punk not dead

06/09/2009 2:16 PM - Plank wrote:
It's serious

06/09/2009 2:16 PM - Tilburg wrote:
Best Site good looking

06/07/2009 11:41 PM - sunil wrote:
I am not able to close the serial port... I want release the COM port once the data is received... Below is my code.. import java.io.*; import java.util.*; import gnu.io.*; public class ReadCommPort implements SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; OutputStream outputStream; public SerialPort serialPort; List byteList = new ArrayList(); public static Message message = null; public void readData() { boolean portFound = false; String defaultPort = "COM1"; portList = CommPortIdentifier.getPortIdentifiers(); while ( portList.hasMoreElements() ) { portId = ( CommPortIdentifier )portList.nextElement(); if ( portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) { if ( portId.getName().equals( defaultPort ) ) { System.out.println( "Found port: " + defaultPort ); portFound = true; buildSerialPort(); } } } if ( ! portFound ) { System.out.println( "port " + defaultPort + " not found." ); } } public void buildSerialPort() { try { serialPort = (SerialPort) portId.open( "ReadCommPort", 1 ); inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); serialPort.addEventListener( this ); serialPort.notifyOnDataAvailable(true); serialPort.setSerialPortParams( 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); } catch ( Exception e ) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public void serialEvent( SerialPortEvent event ) { switch ( event.getEventType() ) { case SerialPortEvent.BI: System.out.println( "BI"); break; case SerialPortEvent.OE: System.out.println( "OE"); break; case SerialPortEvent.FE: System.out.println( "FE"); break; case SerialPortEvent.PE: System.out.println( "PE"); break; case SerialPortEvent.CD: System.out.println( "CD"); break; case SerialPortEvent.CTS: System.out.println( "CTS"); break; case SerialPortEvent.DSR: System.out.println( "DSR"); break; case SerialPortEvent.RI: System.out.println( "RI"); break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println( "OUTPUT_BUFFER_EMPTY"); break; case SerialPortEvent.DATA_AVAILABLE : try { int len = inputStream.available(); byte[] readBuffer = new byte[ len ]; // processing data code.. // close the port // release all resources... serialPort.removeEventListener(); try { serialPort.addEventListener( null ); } catch (TooManyListenersException e) { e.printStackTrace(); } inputStream.close(); outputStream.close(); serialPort.close(); } catch ( IOException e ) { e.printStackTrace(); } break; } } public static void main(String[] args) { new ReadCommPort().readData(); } }

05/21/2009 7:08 PM - Rspmolew wrote:
Good crew it's cool :)

05/12/2009 5:43 AM - richard wrote:
LR1f7f dkv7Rq29nVvzm74lApqSw

05/10/2009 8:01 PM - Rzhbkqcf wrote:
Excellent work, Nice Design

05/03/2009 3:43 AM - Bjkbcjen wrote:


05/02/2009 2:36 AM - Jxyuhzey wrote:
It's serious

04/25/2009 1:00 AM - Elmgoacp wrote:
Very interesting tale

04/14/2009 6:05 AM - Vjbpeuzo wrote:
this post is fantastic

04/04/2009 5:47 PM - sammy wrote:
f15BeU vkoo7wvY5Xkfak7bf1Th

02/21/2009 9:18 PM - Tkeacszd wrote:
Thanks funny site

11/19/2008 4:38 AM - ng_agi wrote:
it would be great if you have something similar for windows. there are a couple of words in wiki about rxtx win32 installation under eclipse, but i am still fighting with my installation...

11/18/2008 9:02 AM - MRI wrote:
the www.rxtx.org site wiki, and most of the links no longer lead to active sites. Is there a place to get the source for rxtx. I tried accessing the cvs site and that as well no longer functions. Advice?

08/21/2008 3:10 AM - Rajan wrote:
Well it works on linux. I need it to make it work on MAC OS(10.4). Will try to email you with some details, if you could provide any advise and help, would be appreciated.

08/17/2008 10:59 PM - wamu jobs wrote:
Very Nice Site! Thanx!

08/07/2008 8:52 AM - WW wrote:
Point well taken, I've listened, and I've changed the site's text color :)

08/07/2008 2:54 AM - Claus wrote:
This website is hard to read, due to lightgray text on white background. Please, change this.

Post a Comment:

Your Name:
Your Comment:

Copyright ©2002-2009 Wade Wassenberg
Last Modified: March 24, 2004. 
If you have questions, comments, or if you find any part of this document to be inaccurate or incorrect, please submit your feedback to webmaster@wass.homelinux.net.