Serial Port Communications

21 Apr

Serial Port Communications
Once the Java Software Development Kit is working smoothly, we need to add support for accessing the serial ports on our computer. This will be important  for the

rest of this book, so we need to get this ironed out now. Sun has developed a set of
Java low-level classes for reading from and writing to serial ports.
There are three levels of classes in the Java communications API:

• High-level classes that manage access and ownership of communication ports.
• Low-level classes that provide an interface to physical communications ports.
• Driver-level classes that provide an interface between the low-level classes and the underlying operating system. Driver-level classes are part of the implementation but not the Java communications API.

All three levels are provided by SUN for the javax.comm API for Win32 and Solaris. The driver level classes are not provided by  SUN for Linux. They are available from a different source. This makes the Linux installation of javax.comm a little more difficult (just a little).

For Windows
To install the javax.comm serial port classes for Windows 98/95/NT, go to the Sun “Java Communications API” web page5 and download the current release of the Java Communications API. Save the ZIP file in a temporary directory. In the ZIP file is a readme.html and a file named PlatformSpecific.html that gives more detailed installation instructions. The general installation is summarized here but if you have problems or if you are installing a newer version, then you should consult these files for details.

Unzip the javacommxx-win32.zip file into C:\jdk1.3\. It will unzip into a folder
named commapi. You will need to copy three files from this folder to the jdk\bin,
jdk\jre\bin, jdk\jre and jdk\lib folders. Copy win32comm.dll to the jdk\bin and
jdk\jre\bin folders, comm.jar to the jdk\lib folder and javax.comm.properties to the
jdk\lib and jdk\jre\lib folders. From a DOS command prompt:

c:\> cd \jdk1.3
c:\jdk1.3\> copy jdk\commapi\win32com.dll jdk\bin
c:\jdk1.3\> copy jdk\commapi\win32com.dll jdk\jre\bin
c:\jdk1.3\> copy jdk\commapi\comm.jar jdk\lib
c:\jdk1.3\> copy jdk\commapi\javax.comm.properties jdk\lib
c:\jdk1.3\> copy jdk\commapi\javax.comm.properties jdk\jre\lib

Now you will need to create a CLASSPATH environment variable so javac and Java programs can find these classes and drivers.

Windows 95/98
For Windows 95/98 you must edit your c:\autoexec.bat file again. Using Notepad (or some other ASCII editor), open your c:\autoexec.bat file. If you already have a CLASSPATH environment variable set, you will need to add the location of comm.jar to it by adding a line similar to the following, just after the CLASSPATH line you already have.

SET  CLASSPATH=%CLASSPATH%;c:\jdk1.3\lib\comm.jar;.

If you don’t have a CLASSPATH environment variable, add one by inserting a line at
the bottom of the autoexec.bat file like this:

SET  CLASSPATH=c:\jdk1.3\lib\comm.jar;.

Notice that we are also adding the current working directory to the CLASSPATH environment variable as specified by the single dot (“.”). You can add other directo-ries for other classes by separating them with the semicolon (“;”). Note that this is one of the few differences between the Windows version of the JDK and the Linux version, where you will use the colon  (“:”) to separate directories in your CLASSPATH environment variable. Save your autoexec.bat file and exit the editor. Now you will need to reboot your computer for these changes to be effective. Do that now.

Windows 2000, Windows NT, Windows XP
For Windows 2000 and Windows NT you will need to edit your environment vari-ables in the usual way. Use the “System” tool in the “Control Panel.” Select the “Environment” tab and look for CLASSPATH in the user variables and system vari- ables. If it already exists, select it and edit its value in the text box. Add to the value; be sure to separate the multiple paths with a semicolon(“;”).

c:\jdk1.3\lib\comm.jar;.

If it does not exist, click the “Variable” text box and enter the variable name, CLASSPATH, then click in the “value” text box and enter the CLASSPATH value as above.

For Windows XP, use the “System” tool in the “Control Panel.” Select the “Ad-vanced” tab and click on the “Environment” button (under “startup and recovery”). Modify or create the CLASSPATH variable as above.

If it does not exist, click in the “Variable” text box and enter the variable name, CLASSPATH, then click in the “value” text box and enter the CLASSPATH value as above.

Now let’s test your installation of the Java Communications API to verify that everything is installed properly. We will use the same folder named “test” that we used in verifying the JDK was installed. Here is a simple Java program that finds all known ports on your computer and lists them by name. These will be known ports— some may not actually exist. This program doesn’t really do anything all that useful, but when it compiles,  runs and spits out some output (without any errors), then you will know that the Java Communications API was installed properly.

Listing 3:2: PortLister.java

Open a DOS command window.
c:\> notepad PortLister.java

Enter the Java program above into the text editor. Save the file and exit the editor. Then compile and run the program:

c:\> javac PortLister.java
c:\> java PortLister

This is what the output from Windows looks like:

C:\> java PortLister
javax.comm.CommPortIdentifier@25ab41 is COM1, serial port
javax.comm.CommPortIdentifier@e3e60 is COM2, serial port
javax.comm.CommPortIdentifier@2125f0 is COM3, serial port
javax.comm.CommPortIdentifier@41cd1f is COM4, serial port
javax.comm.CommPortIdentifier@1afa3 is LPT1, parallel port
javax.comm.CommPortIdentifier@31f71a is LPT2, parallel port

If you saw any errors when compiling or running the program, then you should carefully check the program you entered to see that you typed it correctly. Also check to see that you installed the javax.comm API properly.

These are some common errors and their solutions:

• Your CLASSPATH environment variable does not contain the folder for the javax.comm libraries (\jdk1.3\lib\comm.jar),  the javax.comm API was not installed properly, or you did not restart your computer since modifying your CLASSPATH.

For Linux
To install the javax.comm serial port classes for Linux, go to the Sun “Java Commu- nications API” web page6 and download the current release of the Java Communications API for Solaris/x86. Save the tar file in a temporary directory. In the tar file is a readme.html and a file named PlatformSpecific.html that provides more detailed installation instructions. The general installation is summarized here, but if you have problems or if you are installing a newer version then you should consult these files for details. Decompress and untar the javacommxx-x86.tar.Z file into /opt/jdk. It will create a directory named commapi.

$ cd /usr/java/jdk1.3/
$ cp $HOME/download/javacomm20-x86.tar.gz .
$ gunzip javacomm20-x86.tar.gz
$ tar -xf javacomm2-x86.tar

Now you will need to create a CLASSPATH environment variable so javac and Java programs can find these classes and drivers. Put the commapi.jar file in the CLASSPATH. Using vi (or some other ASCII editor), edit your $HOME/.cshrc (for C shell, or $HOME/.profile for ksh, sh or bash). If you have been using Java, you might already have a CLASSPATH environment variable set, so you will need to add the location of the javax.comm libraries to it.

For C shell:
set  CLASSPATH=/usr/java/jdk1.3/commapi/comm.jar:.
set  PATH=$PATH:/usr/java/jdk1.3/bin
export CLASSPATH PATH

For ksh, sh and bash shells:
CLASSPATH=/usr/java/jdk1.3/commapi/comm.jar:.
PATH=$PATH:/usr/java/jdk1.3/bin
export CLASSPATH PATH

Save the file and exit the editor. This CLASSPATH environment variable tells the java compiler and run time system where to find the java class libraries when you need them.

Also you need to get rxtx. Remember the figure above for the javax.comm API? Recall that Linux low-level drivers are not part of the package. Sun has implemented drivers for Windows and Solaris operating systems only (so far). You will need to get the low-level serial port drivers from the RXTX Web Site7. Download the latest version (in this case rxtx-1.4-5.tar.gz) and save this in your /opt directory. Decompress and untar the file. Run “make jcl” and then “make install.” Refer to the INSTALL files for detailed installation instructions.

% gunzip rxtx-1.4-5.tar.gz
% tar -xf rxtx-1.4-5.tar
% cd rxtx-1.4-5
% ./configure
% make jcl
% make install

If all went well and there were no errors, you can proceed to testing the installation of the javax.comm libraries. Use the same program for Windows as provided above. This is the type of output you should get for Linux:

% javac PortLister.java
% java PortLister

javax.comm.CommPortIdentifier@9df52312 is /dev/ttyS0, serial port
javax.comm.CommPortIdentifier@9e212312 is /dev/ttyS1, serial port
javax.comm.CommPortIdentifier@9dd52312 is /dev/ttyS2, serial port
javax.comm.CommPortIdentifier@9d4d2312 is /dev/ttyS3, serial port
javax.comm.CommPortIdentifier@9d752312 is /dev/lp0, parallel port
javax.comm.CommPortIdentifier@9da12312 is /dev/lp1, parallel port
javax.comm.CommPortIdentifier@9c852312 is /dev/lp2, parallel port

Note that not all of these ports are actual ports. The program simply verifies that the javax.comm API and RXTX were installed and that they function correctly (no compile or runtime errors). This simple test only prints the names of the ports it finds. It does not test them to see if they are real and if they work.

If you saw any errors when compiling or running the program, then you should carefully check the program you entered to see that you typed it correctly. Also check to see that you installed the javax.comm API properly.

These are some common errors and their solutions:

% javac PortLister.java
PortLister.java:4: package javax.comm does not exist
import javax.comm.*;

• Your CLASSPATH environment variable does not contain the folder for the javax.comm libraries (/usr/java/jdk1.3/lib/comm.jar or /usr/java/jdk1.3/commapi/comm..jar),  the javax.comm API was not installed properly, rxtx was not installed properly, or you did not restart your computer since modifying your CLASSPATH.

37

Random Posts

Comments are closed.