This discussion is archived
2 Replies Latest reply: Apr 26, 2007 6:49 AM by 807596 RSS

How to get free Serial COM Port list.

807596 Newbie
Currently Being Moderated
Hi All,

I am trying to get list of available com port list but the program execution is not returning anything.
There is not API in the CommPortIdentifier class that can give what the error is.
I am attaching following bit of code from my program. Can you suggest what the error may be?

Thanks
Somesh


import javax.comm.*;
import java.util.*;

public class COMPort
{
     public COMPort()
     {
          Enumeration enum = CommPortIdentifier.getPortIdentifiers();
          CommPortIdentifier comPortId ;
                              
          while (enum.hasMoreElements())
          {
               comPortId = (CommPortIdentifier)enum.nextElement();
               System.out.println("Port name is : " + comPortId.getName());
               System.out.println();
          }
     }     //constructer function ends here.

     public static void main(String[] args)
     {
          COMPort port = new COMPort();
     }
}
  • 1. Re: How to get free Serial COM Port list.
    807596 Newbie
    Currently Being Moderated
    You may have a security problem
                    /*
                     *  Get an enumeration of all of the comm ports
                     *  on the machine
                     */
                System.out.println("getPortIdentifiers");
                System.setSecurityManager(null);
                ports = CommPortIdentifier.getPortIdentifiers();
                System.out.println("gotPortIdentifiers "+ports);
                System.setSecurityManager(new RMISecurityManager());
                
                if (ports == null || !ports.hasMoreElements())
                {
                    logger.log(Level.SEVERE,"No comm ports found!");
                    return;
                }
                
                while (ports.hasMoreElements())
                {
                            /*
                             *  Get the specific port
                             */
                    
                    portId = (CommPortIdentifier)ports.nextElement();
                    
                            /*
                             *  Is this a serial port?
                             */
                    
                    System.out.println(portId.getName() + " ?");
                    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
                    {
                        //  Is the port in use?
                        if (portId.isCurrentlyOwned())
                        {
                            logger.log(Level.INFO,"Detected "+ portId.getName()+ " in use by "+ portId.getCurrentOwner());
                        }
                        else if(portId.getName().compareTo(apertureComPortName)==0)
                        {
    Now you may not be employing rmi too but it could still be a security problem. Have you run the BlackBox example that comes with the COMM 2.0 API? Also the win32comm.dll and the javax.comm.properties have been copied to JAVA_HOME/jre/bin and JAVA_HOME/jre//lib respectively?
  • 2. Re: How to get free Serial COM Port list.
    807596 Newbie
    Currently Being Moderated
    The initialization of the javax.comm lib is a bit tricky. Maybe my class posted on
    http://forum.java.sun.com/thread.jspa?threadID=702317 could solve your probs.