从Java中的串行端口拆分字符串

时间:2019-06-22 23:02:50

标签: java string split serial-port

我有一个arduino代码,它发送millis()和来自两个电位计的两个读数,它们分别以新的一行发送,如下所示:

 Serial.println(millis());
 Serial.println(val1);
 Serial.println(val2);

我想使用Java将这些字符串中的每个字符串存储在其自己的变量中,因此我可以使用JFreeChart对数据进行图形化处理并看到它的存在,但是我很难将字符串放入单独的变量中。

我尝试更改arduino代码并在Java中使用String split方法,但这没有用。

这是我的代码部分,其中我使用扫描仪读取串行数据:

Thread thread = new Thread() {
    @Override
    public void run() {
        Scanner scan = new Scanner(port.getInputStream());
        while(scan.hasNextLine()) {
            //where the data stuff is put into graph
        }
        scan.close();
    }
};

编辑:

所以我尝试像这样使用split方法:

Thread thread = new Thread() {
    @Override
    public void run() {
        String serialDat;

        Scanner scan = new Scanner(port.getInputStream());
        while(scan.hasNextLine()) {
            serialDat = scan.next();
            String[] dataValues = serialDat.split(",");
            System.out.println(dataValues[0] + " " + dataValues[1] + " " + dataValues[2]);

        }
        scan.close();
    }
};

我让arduino通过Serial发送这样的数据:millis(),sensor1,sensor2

但是当我使用Java代码并连接到端口时,我会得到

java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

1 个答案:

答案 0 :(得分:1)

如果事先知道输入数量,则可以使用字符串数组,例如String []。即使不知道,您也可以使用列表作为这些字符串的输入。

package com.stackoverflow;

import com.google.common.base.Strings;

import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.logging.Logger;

public class StackOverflow_56719783 {

    public static void main(String[] args) throws Exception{
        final Logger logger = Logger.getLogger(StackOverflow_56719783.class.getSimpleName());

        //Use the port.getInputStream() instead of the file. I used file as you had given access to the
        //file in google drive present in the local machine.
        File inputFile = new File("XXXXX/inputfile.txt");
        //The FileInputStream is used in place of port.getInputStream() here.
        Scanner scan = new Scanner(new FileInputStream(inputFile));
        String serialData;
        //Use scan.hasNext() instead of scan.hasNextLine(). Please check the JavaDocs for
        //both these methods for the reason.
        while(scan.hasNext()) {
            serialData = scan.next();
            //Check if the current line is an empty line - Strings class is from google's guava library
            //https://github.com/google/guava/wiki/Release21
            if(Strings.isNullOrEmpty(serialData)) {
                continue;
            }

            //If current string does not contain the delimiter at all, we continue.
            if(!serialData.contains(",")) {
                logger.info("The input line does not contain the de-limiter ,");
                continue;
            }
            //Now we continue to split
            String[] dataValues = serialData.split(",");
            //If should be true that the length of the split array is greater than 0 and equal to 3.
            //Going by your input values, the first value looks like to be always an integer, second value is a double, third value is also a double.
            if(dataValues.length > 0 && dataValues.length == 3) {
                int millis = Integer.parseInt(dataValues[0]);
                double sensor1 = Double.parseDouble(dataValues[1]);
                double sensor2 = Double.parseDouble(dataValues[2]);
                System.out.println(millis + " "+ sensor1 + " "+sensor2);
            }
        }
        scan.close();
    }
}