Programming Tutorials

List all the serial and parallel ports in your system using Java

By: Ashish in Java Tutorials on 2009-03-28  

Are you getting javax.comm.NoSuchPortException error? If yes, then first check if ur COM port is really been detected by the driver. I'll give you one program that will display all the serial and parallel ports in your system.

/* program*/

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"; 
case CommPortIdentifier.PORT_SERIAL:
type = "Serial"; 
default: /// Shouldn't happen
type = "Unknown"; 
System.out.println(port.getName() + ": " + type);

just check it ... this is what the output will be!!( my case)

COM1: Serial
COM2: Serial
COM7: Serial
LPT1: Parallel
LPT2: Parallel

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)