如何使用Java在PC中获取可用串行端口的列表?
-
12-10-2019 - |
题
我只是运行一些代码以获取我的cmputer的可用端口列表,当我有3个免费端口时,它将我返回我的false。如何解决这个问题?
我的代码:
public static void main(String[] args) {
//SerialParameters params=new SerialParameters();
// System.out.println(CommPortIdentifier.PORT_SERIAL );
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while(portList.hasMoreElements()){
System.out.println("Has more elements");
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
System.out.println(portId.getName());
}
else{
System.out.println(portId.getName());
}
}
}
输出:false
解决方案
看起来您的Javax.comm API的设置可能不正确。确保您完成了以下操作:
- 放置
comm.jar
文件中的文件jre/lib/ext
目录。 - 放置
javax.comm.properties
文件中的文件jre/lib
目录。 - 放置
win32com.dll
在里面jre/bin
目录。
上述每个组件都应可用 这里.
其他提示
不隶属于 StackOverflow