Serial port communication in Java
before going to execute this program read how to install comport api for windows In java there is no inbuilt support to access serial or parallel ports, to over come this we are taking support of Java communication API. after downloading the java communication api extract the folder then you can get the following… in Commapi folder we require comm.jar javax.comm.properties win32com.dll after getting these files perform following operations….
- Copy file comm.jar in <JAVA_HOME>jrelibext
- Copy file javax.comm.properties in <JAVA_HOME>jrelib
- Copy file win32com.dll in <JAVA_HOME>lib
now you can able to access the commport by Java programm. The following example displays the available ports(serial and parallel ports) and their type:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keyboardreader;
/**
*
* @author katta vijay
*/
import javax.comm.*;
import java.util.Enumeration;
public class ListPorts {
public static void main(String args[]) {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_PARALLEL:
type = “Parallel”;
break;
case CommPortIdentifier.PORT_SERIAL:
type = “Serial”;
break;
default: /// Shouldn’t happen
type = “Unknown”;
break;
}
System.out.println(port.getName() + “: ” + type);
}
}
}
output is like this :
COM1: Serial
COM2: Serial
LPT1: Parallel
LPT2: Parallel
