Tuesday, February 17, 2009

Java Serial Port Communication(javacomm) in 4 steps

These steps apply to Windows environment only:
1.) download javacomm20-win32.zip
2.) extract comm.jar to ..\jre\lib\ext
3.) extract javax.comm.properties to ..\jre\lib folder
4.) extract win32comm.dll to c:\windows


import java.util.Enumeration;
import javax.comm.CommPortIdentifier;

public class JavaCommTest {
public static void main(String[] ap) {
Enumeration pList = CommPortIdentifier.getPortIdentifiers();

while (pList.hasMoreElements()) {
CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();
System.out.print("Port " + cpi.getName() + " ");
if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("is a Serial Port: " + cpi);
} else if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println("is a Parallel Port: " + cpi);
} else {
System.out.println("is an Unknown Port: " + cpi);
}
}
}
}



Here are the outputs of the above snippet if missing:

win32comm.dll - Error loading win32com: java.lang.UnsatisfiedLinkError: no win32com in java.library.path

javax.comm.properties - pList would return false at hasMoreElements()

No comments:

Post a Comment