我正在開發一個項目,以使用Java與串行端口進行通信。我是否需要將設備連接到串行端口才能測試以下代碼?用於Linux平臺的串行端口訪問代碼
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);
}
任何解決方案,使此代碼工作。目前我如下得到一個錯誤。(沒有任何設備連接到串行端口。
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.comm.SunrayInfo.isSessionActive()Z
at com.sun.comm.SunrayInfo.isSessionActive(Native Method)
at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155)
at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100)
at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138)
at PortTest.main(PortTest.java:9)
Java Result: 1
我已經配置了JRE通訊。我已經按照this博客做到這一點。
http://www.agaveblue.org/howtos/Comm_How-To.shtml我正在關注這篇文章來配置系統。 – Joe
我對串口連接有點困惑。目前沒有設備連接到我的串行端口。這是因爲它。我們可以在linux中創建一個虛擬串行端口嗎?我正在使用Fedora OS。 – Joe
這是可怕的過時,但只要你下載最新的Linux版本,你至少有正確的文件。你得到的錯誤意味着java找不到'.so'(本地庫)文件。你可以用'System.out.println(System.getProperty(「java.library.path」))來檢查你的庫路徑;''.so'文件需要在其中之一。 –