【问题标题】:Serial Port accessing code for linux platformlinux平台串口访问代码
【发布时间】:2011-03-09 03:39:34
【问题描述】:

我正在开发一个使用 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 配置了 comm。我已经关注了this 的博客。

【问题讨论】:

    标签: java linux serial-port virtual-serial-port


    【解决方案1】:

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

    您需要安装 javax.comm 扩展 - http://www.oracle.com/technetwork/java/index-jsp-141752.html

    如果您使用的是 Windows,则 Sun/Oracle 不再支持或提供它。您可能可以在网上找到旧版本或其他人移植它。

    【讨论】:

    • agaveblue.org/howtos/Comm_How-To.shtml我正在关注这个帖子来配置系统。
    • 我对连接到串行端口有点困惑。目前没有设备连接到我的串口。是不是因为这个。我们可以在linux中创建一个虚拟串口吗?我正在使用 Fedora 操作系统。
    • 这已经过时了,但只要您下载了最新的 Linux 版本,您至少拥有正确的文件。您收到的错误意味着 java 找不到 .so (本机库)文件。您可以使用 System.out.println(System.getProperty("java.library.path")); 检查您的库路径 - .so 文件需要在其中之一中。
    【解决方案2】:

    经过一番挣扎,我让代码运行起来了。

    我犯的一个错误是在 Fedora 13 中使用 RxTx 2.2 库。它使用 2.2 版本的 libSeriallibParellal 文件和 2.1 RxTxComm jar 文件。当我将其删除并使用 RxTx2.1 时,出现如下错误。

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

    在检查此错误时,我发现了我犯的第二个错误以及上述问题的解决方案。我正在使用带有 java Comm API 的 RxTx 驱动程序。实际上,Java Comm API 中所需的类文件已经在“gnu.io”包中的 RxTx 库中可用。

    所以我将所有javax.comm.* 包更改为gnu.io.*。现在我可以运行应用程序而不会出现任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多