Serial Port Communication with Java
Here am showing the simple serial port communication program,
here i will show you how to get list of all available serial and parallel ports available
FIrstly,By default java does not contains javax.comm package so we need to install it into our pc java follow this link to know
How to add javax.conn package to java
after adding this package to the java
just run the below code to get available serial and parallel ports
[sourcecode language="java"]
package in.mjtech.serial;
import java.util.Enumeration;
import javax.comm.*;
public class ListSerial {
public static void main(String[] args) {
// SerialParameters params=new SerialParameters();
// System.out.println(CommPortIdentifier.PORT_SERIAL );
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList
.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println(portId.getName()+ " is parallel port");
} else if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName()+ " is serial port");
}
}
}
}
[/sourcecode]
here i will show you how to get list of all available serial and parallel ports available
FIrstly,By default java does not contains javax.comm package so we need to install it into our pc java follow this link to know
How to add javax.conn package to java
after adding this package to the java
just run the below code to get available serial and parallel ports
[sourcecode language="java"]
package in.mjtech.serial;
import java.util.Enumeration;
import javax.comm.*;
public class ListSerial {
public static void main(String[] args) {
// SerialParameters params=new SerialParameters();
// System.out.println(CommPortIdentifier.PORT_SERIAL );
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
CommPortIdentifier portId = (CommPortIdentifier) portList
.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println(portId.getName()+ " is parallel port");
} else if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName()+ " is serial port");
}
}
}
}
[/sourcecode]
Comments
Post a Comment