0

我在我的 com 端口连接了一个设备,我正在尝试获取它的值,但我被困在第一步。

我无法获取现有的 com 端口。在下面的代码中,枚举似乎是空的,因为程序根本没有进入 while 循环。谁能帮忙

public class connectnow implements Runnable, SerialPortEventListener {

    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    byte[] readBuffer;

    public static void main(String[] args) {

        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("portList... " + portList);

        while (portList.hasMoreElements()) {
            System.out.println("yes");
        }
    }
4

1 回答 1

0

这似乎对我有用。

我必须从http://mfizz.com/oss/rxtx-for-java安装 x64 版本的 RXTX 。该软件包是 gnu.io(这就是您看到导入的原因)。你可能需要做一些不同的事情。

请注意,getPortIdentifiers() 返回需要一点时间。给它时间。

import gnu.io.*;
import java.util.Enumeration;
import java.io.InputStream;

public class connectnow implements Runnable, SerialPortEventListener {

    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    byte[] readBuffer;

    public static void main(String[] args) {

        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("portList=" + portList);

        while (portList.hasMoreElements()) {
            System.out.println("yes");
            CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
            System.out.println("portId=" + portId);
        }
    }

    public void run() {
    }

    public void serialEvent(SerialPortEvent ev) {
    }
}
于 2014-06-17T15:53:00.330 回答