1

好的,我想在 java 中使用串行通信,使用javax.comm. 我想要做的是通过串行通信读取一行并将其保存在一个数组中。

例如,每次我按下外部设备上的按钮时,它都会发送一行随机数字,例如"12345",如果我再次按下它,它应该发送另一行数字。我希望我的 java 程序读取该行,将其保存到一个数组中,然后准备接收另一行数字并将其保存在另一个数组中以备后用。

如果有任何提示或至少有人可以帮助我与串行连接会很棒。谢谢你。到目前为止,我有以下内容,可以找到端口。

import java.util.Enumeration;
import javax.comm.CommPortIdentifier;

public class PortAccess {
    CommPortIdentifier commPortIdentifier;
    Enumeration enumeration;

    public void fnListPorts() {
        try {
            enumeration = CommPortIdentifier.getPortIdentifiers();
        } catch (Exception e) {
            e.printStackTrace();
        }

        while (enumeration.hasMoreElements()) {
            commPortIdentifier = (CommPortIdentifier) enumeration.nextElement();
            System.out.println(commPortIdentifier.getName());
        }
    }

    public static void main(String[] args) {
        PortAccess portAccess = new PortAccess();
        portAccess.fnListPorts();
    }

}
4

0 回答 0