![](/img/trans.png)
[英]How to get the list of all available serial ports on linux using java?
[英]How to get list of available serial ports in my pc using Java?
我只是運行一些代碼來獲取我的 cmputer 上可用端口的列表,當我有 3 個空閑的 com 端口時,它返回了 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());
}
}
}
輸出:錯誤
看來您的 javax.comm API 設置可能不正確。 確保您已完成以下操作:
comm.jar
文件放在jre/lib/ext
目錄中。javax.comm.properties
文件放在jre/lib
目錄中。win32com.dll
放在jre/bin
目錄中。上述每個組件都“應該”在此處可用。
我有一個額外的答案,如果你碰巧做了我天真的做的事。
我只是忘記將我的用戶添加到dialout
組,結果是找不到串口,而我的/dev/ttyUSB0
被正確轉發到我的 Lubuntu 機器的 VirtualBox 實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.