如何使用java初始化串口?

我知道这个问题已被多次询问,我已经浏览了谷歌的前20个搜索结果,但仍然无法让我的代码工作.

portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("portList... " + portList);
while (portList.hasMoreElements()) {
    portId = (CommPortIdentifier) portList.nextElement();
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        System.out.println("port identified is Serial.. "+ portId.getPortType());
        if (portId.getName().equals("COM2")) {
            System.out.println("port identified is COM2.. "+ portId.getName());  
            SimpleRead reader = new SimpleRead();
        } else {
            System.out.println("unable to open port");
        }
    }else{
        System.out.println("pordId.PortType = "+portId.getPortType());
        System.out.println("CommPortIdentifier.PORT_SERIAL = "+CommPortIdentifier.PORT_SERIAL);
    }
}

我得到的输出是

run:

portList … javax.comm.CommPortEnumerator@18020cc
建立成功(总时间:0秒)

有谁知道什么是错的?为什么没有portId有更多的元素?程序没有进入while循环.

提前感谢阅读这篇文章.

最佳答案 我设法解决了这个问题.

我需要将文件复制到以下位置以便javac.comm工作

1) win32com.dll to directory : jdk1.7.0/bin
2) javax.comm.properties to directory : jdk1.7.0/jre/lib
3) comm.jar to directory : jdk1.7.0/lib

现在的输出是

portList... javax.comm.CommPortEnumerator@9173ef
port identified is Serial.. 1
port identified is CO11.. COM11
In SimpleRead() contructor
Serial Port.. COM11
Input Stream... com.sun.comm.Win32SerialInputStream@95c083
................
port identified is Serial.. 1
unable to open port
pordId.PortType = 2
CommPortIdentifier.PORT_SERIAL = 1
pordId.PortType = 2
CommPortIdentifier.PORT_SERIAL = 1
ending main
In run() function 
点赞