Communicating with ports using javax.comm package for Windows

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

Posted in JAVA. Tags: . 6 Comments »

6 Responses to “Communicating with ports using javax.comm package for Windows”

  1. Preetinder Rooprai Says:

    This is the exact info. regarding communication issue with system ports (serial/parallel).

  2. keerthi.k Says:

    thank you very much …..finally i got something that i wanted …

  3. keerthi.k Says:

    you made things looks so easy …..once again thank u…
    i would more appropriate if you send me the code to check
    if the installation is perfect in rxtx

  4. Subrata Says:

    After a long time i reach my goal

  5. Pep Says:

    Hi and thanks a lot for your easy explanation. In my case it seems it works because it doesn’t give me any kind of error but I don’t get any port as and answer. Why can this happen?? I use NetBeans IDE 6.8.
    Thanks in advance

  6. Java Communications « Yosua Onesimus Sanctuary Says:

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


Leave a reply to Preetinder Rooprai Cancel reply