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

How to properly partition the incoming data using indexOf()?

我在我的 PC 上使用 Processing 3.3.7 创建了一个图形用户界面。一个微控制器通过COM8以

的形式不断发布数据
angle,distance.mindistance

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

Processing 上运行的代码

  1. 读取传入数据直到换行符
  2. 找到",""."
  3. 的索引
  4. 将位置 "0""," 的索引之间的任何值分配给变量 angle
  5. "," 的索引和 "." 的索引之间的任何值分配给变量 distance
  6. "." 的索引和数据末尾之间的任何内容分配给变量 mindistance
  7. 在 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。

I found out that the mindistance is assigned with the right value (e.g. 40) but when the string is converted into an integer iMinDistance = int(mindistance);, iMinDistance becomes 0.

发生这种情况是因为分配给 mindistance 的字符串实际上不能转换为 int,即它可能后跟 space 或新行。所以比数据末尾少一个索引需要分配给mindistance.

改变

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

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

除了您已经了解的内容之外,您还可以考虑使用 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 是你最好的朋友。