这是我为设备枚举实现的代码。我能够检测并显示所有连接的串行设备。但是假设我连接了另一个设备,然后尝试调用此功能,它始终显示在第一次运行代码期间连接的设备。
代码片段:
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());
}
}
}
}
即例如,在函数的第一次调用中,显示COM1和COM3。
现在假设,连接的串行设备加载到 COM27 上。因此,如果我们重新运行代码,它只会显示 COM1 和 COM3,而不会显示 COM27。
另一种情况,在我第一次运行代码之前,在 COM27 上加载了一个串行设备。现在第一次运行它显示COM1,COM3,COM27。现在删除COM27设备并重新运行上面的代码,它仍然显示COM27已连接。
非常感谢这方面的任何帮助。
谢谢,阿比