How to
list all available ports in a PC using Java
Some theory…
Java "Virtual Machine" doesn't support
communication to support for serial and parallel ports with its default
installation. So, if you want to access the serial (RS232) or parallel port
with JAVA, you need to install a platform/operating system dependent library. The
javax.comm from SUN enables programmers to write Java software that accesses
communications ports in a platform-independent way. But Sun has discontinued
the old Java comm. Package for Windows. The substitute is RXTX. RXTX will
support all type of platforms including Windows, Linux, Mac OS X.
RXTX Installation procedure for Windows
- Download the zip file (rxtx-*-win32.zip) from the link, http://rxtx.qbang.org/wiki/index.php/Download
- Unzip rxtx-*-win32.zip
- Zipped file contains 3 files - rxtxSerial.dll,rxtxParallel.dll and RXTXcomm.jar:
- Copy rxtxSerial.dll and rxtxParallel.dll to JAVA_HOME\bin
- (JAVA_HOME is the folder where JRE is installed on your system; e.g. c:\Program Files\Java\ jre1.6.0_03)
- Copy RXTXcomm.jar to JAVA_HOME\lib\ext
Once installed, the IDE will need to know where to look for these installed
files. Even though the files exist in the JRE directory, each project
needs to know about these files.
The package of all the
classes is gnu.io.
So in source code, instead of
import javax.comm.CommPortIdentifier,
you have to use
import gnu.io.CommPortIdentifier.
My aim
is to list all the available serial ports in my PC through Java.
import
gnu.io.CommPortIdentifier;
import java.util.Enumeration;
import java.util.Vector;
/*
* Get list of ports available on this particular computer
*/
public class ListSerialPorts
{
public static void main(String[] args)
{
int totalElements;
CommPortIdentifier portId;
Enumeration en = CommPortIdentifier.getPortIdentifiers();
Vector listData = new Vector(8);
// Walk through the list of port identifiers and, if it
// is a serial port, add its name to the list.
// is a serial port, add its name to the list.
while (en.hasMoreElements())
{
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
listData.addElement(portId.getName());
}
}
totalElements = listData.size();
//Iterate through the vector
for (int index = 0; index < totalElements; index ++)
{
System.out.println(listData.get(index));
}
}
}
While trying to run the
program in Netbeans 7.0, it throws the error,
java.lang.UnsatisfiedLinkError:
no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread
"main" java.lang.UnsatisfiedLinkError: no rxtxSerial in
java.library.path
at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at
java.lang.Runtime.loadLibrary0(Runtime.java:823)
at
java.lang.System.loadLibrary(System.java:1030)
at
gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)
at
ListSerialPorts.main(ListSerialPorts.java:16)
Java Result: 1
Solution:-
Right Click ProjectName à
Properties à Libraries à
Add Jar à Select RXTXcomm.jar from the path C:\Program
Files\Java\jre1.6.0_03\lib\ext\
Run the project.
It again throws exception,
java.lang.UnsatisfiedLinkError:
no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread
"main" java.lang.UnsatisfiedLinkError: no rxtxSerial in
java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
at
java.lang.Runtime.loadLibrary0(Runtime.java:823)
at
java.lang.System.loadLibrary(System.java:1030)
at
gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)
at ListSerialPorts.main(ListSerialPorts.java:16)
Java Result: 1
Solution:-
Copy rxtxSerial.dll and rxtxParallel.dll
files to the root directory of your project
The output will be displayed as
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
COM1
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
COM1
Source:-
No comments:
Post a Comment
Thanks for visiting my Blog....Ur comments n suggestions are most valuable for me...Thanks