javax.comm - getPortIdentifiers(); works only once..... need help
807589Nov 30 2004 — edited Sep 11 2008Hi, I have a program which reads from a virtual comm port on windowsXP. At the beginning, the program lists all the serial ports on the system and asks the user to choose one. I have a "refresh" button - which should re-list the ports by calling CommPortIdentifier.getPortIdentifiers(); again. I need the refresh button because the virtual comm port only appears if my external device is plugged in.
Here's my "refresh" method:
CommPortIdentifier portId;
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(portId.getName());
}
}
The problem is that the first time I call this, it lists all the ports including the virtual one. However, if the virtual port then disappears (device unplugged) and I call refresh again - the list still shows the virtual port. If I close my java program and reopen it, the call to getPortIdentifiers() now reflects the actual state of the system - no virtual port. How can I get getPortIdentifiers() to return an up-to-date list on successive calls without having to close and reopen my app.?
Thanks for your advice
-Ben
Ps. It seems to me like CommPortIdentifiers class does something at load time that it doesn't ever redo. Is there some way to cause a class to get reloaded into the JVM?