如何使用Java获取我的电脑中可用串口的列表?
我只是运行一些代码来获取我的cmputer中的可用端口列表,当我有3个免费的COM端口时,它返回了我的错误。 我该如何解决这个问题?
我的代码:
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
目录中。
上述每个组件“应该”在这里可用。
我正在使用ubuntu而我的电脑没有任何串口/ pararel端口。
在这种情况下,您需要模拟此端口。
我的答案:
在ubuntu上使用java进行串口识别