似乎我无法从这段代码中得出结果,它的目的是列出计算机上的所有端口(com,lpt)
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);
}
}
}
我听说Windows阻止应用程序访问端口,但我无法找到解决方法。任何提示和建议将不胜感激
EDIT 下面的链接对我有用 How to get list of available serial ports in my pc using Java?