代码之家  ›  专栏  ›  技术社区  ›  csg techguy18985

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

  •  0
  • csg techguy18985  · 技术社区  · 6 年前

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

    angle,distance.mindistance
    

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

    处理时运行的代码

    1. 查找的索引 "," "."
    2. 在位置之间分配 "0" "," 到变量 angle
    3. 在索引之间赋值 "," 和索引 "." distance
    4. 在索引之间赋值 并将数据的结尾改为变量 mindistance
    5. 在GUI上做一些进一步的处理和可视化。

    问题是 心灵距离 始终指定为0,表示步骤5有问题:

    void serialEvent (Serial myPort) {}

    但是 距离 正确显示。

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

    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
    }
    

    ---编辑---

    我发现 心灵距离 iMinDistance = int(mindistance); , iMinDistance 变为0。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Kevin Workman    6 年前

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

        2
  •  1
  •   csg techguy18985    6 年前

    我发现 mindistance 指定了正确的值 iMinDistance = int(mindistance); , iMinDistance 变为0。

    心灵距离 不是可转换为int的,即后面可能跟有空格或新行。因此需要分配的索引比数据末尾少一个 心灵距离

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

    具有

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