About a year ago for my senior design project I needed to setup Java for Ubuntu Linux to communicate with an RS-232 port. It took me some time to figure out how to do this so I thought I would share my solution with anybody that had a similar need. This setup requires a USB-to-Serial cable.
1. Download Java Comm API from http://www.oracle.com/technetwork/java/index-jsp-141752.html
2. Follow the steps in this blog post to setup USB-to-Serial cable for linux
3. Copy libLinuxSerialParallel.so to /usr/lib/
4. Copy javax.comm.properties to [JDK-directory]/jre/lib/
5. Copy comm.jar to [JDK-directory]/lib/
6. Setup classpath by typing the below in a terminal:
> export CLASSPATH=$CLASSPATH:[location of comm.jar]
Example
> export CLASSPATH=$CLASSPATH:/usr/lib/jvm/java-6-openjdk /jre/lib/ext/comm.jar
Notes: Apprantely Linux by default automatically converts LF bytes into two bytes LF and CR whenever sending data through a serial port. Also by default whenever it receives a CR it converts it into an LF byte. This has been a well known issue and there exists a fix. By running the below commands we were able to get rid of this problem. "stty -F /dev/ttyUSB0 -onclr" "stty -F /dev/ttyUSB0 -icrnl" /dev/ttyUSB0 can be whatever file descriptor you are using. - Install JDK 1.6 as earlier versions has had historical problems with the Java Comm API - When testing, launching the Jar file in Netbeans gave no problem, however when trying to launch the jar file in the command line I received the below error:
> java -jar carInterface.jar javax.comm: Error loading javax.comm.properties! ... ...
I was able to fix this by copying the javax.comm.properties file into the same directory as the jar file. Which is weird because according to the javadoc the first place javax.comm looks is the [JDK-directory]/jre/lib directory.
Comments