Linux平台串口访问代码

发布于 2024-10-21 05:47:28 字数 1300 浏览 7 评论 0原文

我正在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-10-28 05:47:28

您缺少所需的本机库。您发布的错误行上方的行告诉您这一点。

您需要安装 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.

暖心男生 2024-10-28 05:47:28

经过一番努力后,我让代码运行起来。

我犯的一个错误是在 Fedora 13 中使用 RxTx 2.2 库。它使用 2.2 版本的 libSeriallibParellal 文件以及 2.1 RxTxComm< /代码> jar 文件。当我删除它并使用 RxTx2.1 时,我收到如下错误。

gnu.io.RXTXCommDriver cannot be cast to javax.comm.CommDriver

在检查这个错误时,我发现了我犯的第二个错误以及上述问题的解决方案。我使用 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 of libSerial and libParellal files and 2.1 RxTxComm jar file. When I removed it and used RxTx2.1 I got an error like following.

gnu.io.RXTXCommDriver cannot be cast to javax.comm.CommDriver

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 to gnu.io.*. Now I can run the application without any error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文