Linux平台串口访问代码
我正在开发一个使用 Java 与串行端口通信的项目。我需要将设备连接到串行端口来测试以下代码吗?
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 配置了通讯。我已按照 这个博客来做到这一点。
I'm working on a project to communicate to the serial ports using Java. Do I need to have a device connected to serial port to test the following code?
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);
}
Any solution to make this code working. Currently I'm getting an error as follows.(without attaching any device to serial port.
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
I've configured comm with jre. I've followed this blog to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少所需的本机库。您发布的错误行上方的行告诉您这一点。
您需要安装 javax.comm 扩展 - http://www.oracle .com/technetwork/java/index-jsp-141752.html
如果您使用的是 Windows,则 Sun/Oracle 不再支持或提供它。您也许可以在网上找到旧版本或其他人移植它。
You're missing the native libraries required. The line above the error lines you posted is telling you that.
You need to install the javax.comm extention - http://www.oracle.com/technetwork/java/index-jsp-141752.html
If you're using windows, it's no longer supported or available from Sun/Oracle. You may be able to find an older version on the net or someone else porting it.
经过一番努力后,我让代码运行起来。
我犯的一个错误是在 Fedora 13 中使用
RxTx 2.2
库。它使用 2.2 版本的libSerial
和libParellal
文件以及 2.1RxTxComm< /代码> jar 文件。当我删除它并使用
RxTx2.1
时,我收到如下错误。在检查这个错误时,我发现了我犯的第二个错误以及上述问题的解决方案。我使用 RxTx 驱动程序和 java Comm API。实际上,Java Comm API 中所需的类文件已在“
gnu.io
”包中的 RxTx 库中提供。因此,我将所有
javax.comm.*
包更改为gnu.io.*
。现在我可以运行该应用程序而不会出现任何错误。After a bit struggles, I got the code running.
One mistake I made was using
RxTx 2.2
library for Fedora 13. It uses 2.2 version oflibSerial
andlibParellal
files and 2.1RxTxComm
jar file. When I removed it and usedRxTx2.1
I got an error like following.While checking for this error, I found the second mistake I made and solution for the above problem. I was using RxTx Driver with java Comm API. Actually the required class files in Java Comm API is already available in the RxTx library with in "
gnu.io
" package.So I changed all the
javax.comm.*
packages tognu.io.*
. Now I can run the application without any error.