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

By: Ashish Viewed: 154973 times  Printer Friendly Format    


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"; 
break;
case CommPortIdentifier.PORT_SERIAL:
type = "Serial"; 
break;
default: /// Shouldn't happen
type = "Unknown"; 
break;
}
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



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. By running ListPorts, I am not getting any output
View Tutorial          By: Srinivasa Rao at 2009-04-02 05:17:20

2. Can anyone provide me the Java code to read SMS us
View Tutorial          By: Jaydatta at 2009-04-06 00:21:09

3. Sir,
i am implementing the above code in w

View Tutorial          By: Rahul Singh at 2009-09-01 04:23:25

4. HI, like some guys in this tutorial I get this pro
View Tutorial          By: omar carpio at 2009-09-06 13:17:37

5. Support for serial ports is platform dependent and
View Tutorial          By: Sal at 2009-09-15 15:51:55

6. Sir,
i am implementing the above code in w

View Tutorial          By: Girish padki at 2010-01-15 21:50:50

7. I had just copied the code & tried to execute
View Tutorial          By: dhananjay padalkar at 2010-02-13 19:20:27

8. I changed import javax.comm.*; for this line impo
View Tutorial          By: Julen at 2010-07-18 18:39:25

9. I changed import javax.comm.*; for this line impo
View Tutorial          By: Julen at 2010-07-18 18:39:29

10. Sir, this code not giving any output.
it is

View Tutorial          By: Deepak at 2011-03-19 23:39:49

11. Hi
I tried to use this class , I don't get

View Tutorial          By: Asela at 2012-01-26 19:05:14

12. hey usharadha and all other friends...i hv taken a
View Tutorial          By: abdur at 2012-02-03 15:56:10

13. hi everybody....

please use comm.j

View Tutorial          By: manoj at 2012-09-24 10:41:34

14. Having this error while using comm.jsr for sending
View Tutorial          By: Amit Kumar at 2013-10-15 09:04:58

15. Hi
I tried to use this class , I don't get

View Tutorial          By: arnav at 2015-03-27 06:01:22

16. Hello,
I am not getting any output.

View Tutorial          By: Niket DOshi at 2015-05-01 03:03:49

17. I found javax.comm not really fit for my project a
View Tutorial          By: David at 2015-05-04 18:06:14