如何使用indexOf()正确划分传入的数据?

时间:2018-10-25 17:24:30

标签: java processing indexof string-conversion

我在PC上使用Processing 3.3.7创建了图形用户界面。微控制器不断通过COM8以以下形式发布数据:

angle,distance.mindistance

后跟一个新行。我在微控制器上编写了一个简单的代码,该代码循环遍历一系列数据,只是为了验证GUI是否正常工作。

在处理上运行的代码

  1. 读取传入的数据直到换行符为止
  2. 找到",""."的索引
  3. 将位置"0"","的索引之间的任何内容分配给变量angle
  4. 将索引","和索引"."之间的任何内容分配给变量distance
  5. "."的索引和数据结尾之间的任何内容分配给变量mindistance
  6. 做一些进一步的处理并在GUI上可视化。

问题是mindistance始终分配为0,表明第5步存在问题:

请参见void serialEvent (Serial myPort) {}

angledistance正确显示。

我的代码的相关部分如下:

import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;

Serial myPort; // defines Object Serial

String angle="";
String distance="";
String mindistance = "";
String data="";
String noObject;
float pixsDistance, pixsMinDist;
int iAngle, iDistance, iMinDistance;
int index1=0;
int index2=0;
PFont orcFont;
int linefeed = 10; // new line ASCII = 10

void setup() {

  size (1600, 900);
  smooth();
  myPort = new Serial(this, "COM8", 115200); // starts the serial communication
  myPort.bufferUntil(linefeed); //reads the data from the serial port up to the character 'n'. So actually it reads this: angle,distance.mindistance
  orcFont = loadFont("OCRAExtended-30.vlw");
}

void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  // reads the data from the Serial Port up to the character 'n' and puts it into the String variable "data".
  data = myPort.readStringUntil(linefeed);
  data = data.substring(0, data.length()-1);

  index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  index2 = data.indexOf(".");  // https://processing.org/reference/String_indexOf_.html
  angle= data.substring(0, index1); // read the data from position "0" to to the index of "."
  distance= data.substring(index1+1, index2); // read the data between index of "," and index of "."
  mindistance = data.substring(index2+1, data.length()); // read the data from index of "." to the end of the data

  // converts the String variables into Integer
  iAngle = int(angle);
  iDistance = int(distance);
  iMinDistance = int(mindistance);
}

void drawObject() {// limiting the range to 400 cm
    // some more code here
}

---编辑---

我发现mindistance被分配了正确的值(例如40),但是当字符串转换为整数iMinDistance = int(mindistance);时,iMinDistance变为0。

2 个答案:

答案 0 :(得分:3)

除了您已经弄清楚的内容之外,您还可以考虑使用Processing提供的便捷功能来进行字符串解析。

例如,您可以使用splitTokens()函数将原始字符串拆分为单个值。您可以在the reference中了解更多信息,但这是一个基本示例:

String incomingString = "45,10.7";

String[] tokens = splitTokens(incomingString, ",.");
int angle = int(tokens[0]);
int distance = int(tokens[1]);
int minDistance = int(tokens[2]);

println("angle: " + angle);
println("distance: " + distance);
println("minDistance: " + minDistance);

(附带说明:这是我们提到MCVE时所讨论的示例程序。)

您还可以使用trim()函数消除任何多余的空白字符:

String incomingString = "  45  ,   10   .   7   ";

String[] tokens = splitTokens(incomingString, ",.");
int angle = int(trim(tokens[0]));
int distance = int(trim(tokens[1]));
int minDistance = int(trim(tokens[2]));

println("angle: " + angle);
println("distance: " + distance);
println("minDistance: " + minDistance);

一如既往,the reference是您最好的朋友。

答案 1 :(得分:1)

  

我发现mindistance被分配了正确的值   (例如40),但当字符串转换为整数时   iMinDistance = int(mindistance);iMinDistance变为0。

之所以会发生这种情况,是因为分配给mindistance的字符串实际上不能转换为整数,即它后面可能跟有空格或换行符。因此,需要将比数据末尾少一个的索引分配给mindistance

更改

mindistance = data.substring(index2+1, data.length());

mindistance = data.substring(index2+1, data.length()-1);