Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javax.comm进行Java串行通信并将变量保存到数组中_Java_Arrays_Serial Port_Javax.comm - Fatal编程技术网

使用Javax.comm进行Java串行通信并将变量保存到数组中

使用Javax.comm进行Java串行通信并将变量保存到数组中,java,arrays,serial-port,javax.comm,Java,Arrays,Serial Port,Javax.comm,好的,我想在java中使用串行通信,使用javax.comm。我要做的就是通过串行通信读取一行数据,并将其保存在一个数组中 例如,每次我按下外部设备上的一个按钮,它都会发送一行像12345这样的随机数字,如果我再次按下它,它会发送另一行数字。我希望我的java程序读取该行,将其保存到一个数组中,然后准备接收另一行数字,并将其保存到另一个数组中以供以后使用 如果有任何提示或有人至少可以帮助我与串行连接将是伟大的。非常感谢。到目前为止,我有以下内容,可以找到端口 import java.util.E

好的,我想在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();
    }

}