1

Here is the code that I am implementing for Enumeration of devices. I am able to detect and display all the Serial devices connected.But suppose I connected another device and then try to recall this function, It is always showing the devices that are connected during first run of the code.

Code Snipet:

public void Listports() {

    Enumeration ports = null;
    ports  = CommPortIdentifier.getPortIdentifiers();
    CommPortIdentifier portId = null;

    {
        while (ports.hasMoreElements()) {

             portId = (CommPortIdentifier) ports.nextElement();

            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){                    

                System.out.println(portId.getName());

            }
        }
    }
}

i.e. For Example, in the first Call of the function , COM1 and COM3 are displayed.

Now suppose, a Serial Device connected is loaded on COM27. So If we re-run the code it is displaying only COM1 and COM3, and no COM27.

Another scenario, Before my first run of the code a Serial device is loade on COM27. Now on first run it shows COM1,COM3,COM27. Now remove the COM27 device and re-run the above piece of code, It still shows COM27 is connected.

Any Help in this regard is greatly Appreciated.

Thanks, Abhi

Abhi4ever
  • 41
  • 7
  • you didnt post any code that actually tests if a port is connected, you just enumate the available ports – Peter Mar 18 '13 at 06:53
  • Can you please explain me in detail, I guess (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) is actually testing if a serial device is connected, Am I missing something?? – Abhi4ever Mar 18 '13 at 07:01

1 Answers1

0

Comm port API

you just get a list op comm ports, you can then for example try if the port is owned aka used by some application with isCurrentlyOwned()

Basicly just iterating a list doesnt tell you anyhting in the list until you further test it.

Peter
  • 5,728
  • 20
  • 23
  • Agreed, But here the problem is List of Available comports itself is not Updating, then comes the part whether the port is free or is being owned by some Application! – Abhi4ever Mar 19 '13 at 05:39