开发者

Serial Port accessing code for linux platform

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;
        c开发者_如何学Pythonase 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜