Communicating with ports using javax.comm package for Windows

Posted: February 26, 2011 in Java

Communicating with ports using javax.comm package for Windows

April 7, 2009 — Pradnya

The Java Communications API from Sun available in javax.comm package
provides access to communications resources.It supports configuring,sending and receiving data for parallel and serial ports.

The first step for using this package is placing the proper files
in respective JAVA_HOME’s directory folders.

1) win32com.dll : keep it in JAVA_HOME’s bin directory.
In my case I placed it here C:\Program Files\Java\jdk1.6.0_05\jre\bin

2)comm.jar : place it in JAVA_HOME’s lib\ext directory.
Mine was in C:\Program Files\Java\jdk1.6.0_05\jre\lib\ext

3)javax.comm.properties : same as above.
So this file goes in C:\Program Files\Java\jdk1.6.0_05\jre\lib\ext

To verify whether the javax.comm.package is installed and operational,
Here is a simple program which prints out the serial and parallel ports.

import javax.comm.*;
import java.util.*;

/** List all the ports available on the local machine. **/
public class ListPorts
{
public static void main (String args[])
{

Enumeration port_list = CommPortIdentifier.getPortIdentifiers();

while (port_list.hasMoreElements())
{
CommPortIdentifier port_id = (CommPortIdentifier)port_list.nextElement();

if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println (“Serial port: ” + port_id.getName());
}
else if (port_id.getPortType() == CommPortIdentifier.PORT_PARALLEL)
{
System.out.println (“Parallel port: ” + port_id.getName());
}
else
System.out.println (“Other port: ” + port_id.getName());
}
} // main
} // class PortList

Now build and run the program.
C:\>javac ListPorts.java

C:\>java ListPorts
Serial port: COM7
Serial port: COM10
Serial port: COM3
Parallel port: LPT1
Parallel port: LPT2

If you get a similar output then the package is installed properly.

Not so good news is that Sun no more provides
support for Java communications API for Windows.Why? Here is the answer.
But not to worry..
You can still get the package from http://www.rxtx.org.
Changes to be then made include in import statements.
So instead of import javax.comm it should now be import gnu.io.*

Check out these links.They helped me.Will help u too.
http://java.sun.com/products/javacomm/
http://www.particle.kth.se/~lindsey/JavaCourse/Book/chapter23.html
http://www.rxtx.org/
http://forums.sun.com/thread.jspa?threadID=698876

 

Reference

http://pradnyanaik.wordpress.com/2009/04/07/communicating-with-ports-using-javaxcomm-package-for-windows/

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s