# Unpacking serial data from arduino

**URL:** https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662
**Category:** Electronics (Arduino, etc.)
**Created:** [December 27, 2019, 10:03pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662 "2019-12-27T22:03:58Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 27, 2019, 10:03pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/1 "2019-12-27T22:03:58Z")

</div>

Can someone help me understand what is going on here. I am receiving serial data in the format “angle,sonar distance,ir distance.”. My code works with the first 2 variables. I am trying to add the third. I do not understand the syntax for this code. Do I need an index2 or index1+1? what is the proper method?

```auto
///////////////////////////////////////////////////
  index1 = val.indexOf(","); // find the character ',' and puts it into the variable "index1"
  angle= val.substring(0, index1); // read the val from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  sonar_distance= val.substring(index1+1, val.length()); // read the val from position "index1" to the end of the val pr thats the value of the distance
 //ir_distance= val.substring(index2+1, val.length());
  // converts the String variables into Integer
  iAngle = int(angle);
  isonar_distance = int(sonar_distance); 
}

```

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [December 27, 2019, 10:22pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/2 "2019-12-27T22:22:21Z")

</div>

This is a way to split chunk of data based on the **comma** (,) as separator. You can revamp the code above by using the `split()` function as documented in the [reference](https://processing.org/reference/split_.html).

If you still have issues, make sure you print and you inspect the content of `val` and used debugging tools available with processing to understand what is going on. If the issue persists, provide a snippet on the arduino code as well as your updated Processing code.

Kf

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 27, 2019, 10:30pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/3 "2019-12-27T22:30:09Z")

</div>

```auto
void loop()
{
  if (Serial.available() > 0) { // If data is available to read,
    val = Serial.read(); // read it and store it in val

    buttons(val);
    //delay(scanspeed);
    // Serial.println(scanspeed);
  }
  else {

    for (int i = minbrg; i <= maxbrg; i++)
    {
      val = Serial.read(); // read it and store it in val
      buttons(val);

      servo_1.write(i);
      servo_2.write(j);
      //Serial.println(scanspeed);
      delay(scanspeed);

      //delay(5);
      sonar_distance = calculatesonar_distance();
      Serial.print(i);
      Serial.print(",");
      Serial.print(sonar_distance);
     // Serial.print(",");
     // IR_Range();
     // Serial.println(ir_range);
      Serial.println(".");
    }

    for (int i = maxbrg; i > minbrg; i--)
    {
      val = Serial.read(); // read it and store it in val
      buttons(val);

      //int i = 180;
      servo_1.write(i);
      servo_2.write(j);
      delay(scanspeed);

      // delay(5);
      sonar_distance = calculatesonar_distance();
      Serial.print(i);
      Serial.print(",");
      Serial.print(sonar_distance);
     // Serial.print(",");
     // IR_Range();
     // Serial.println(ir_range);
      Serial.println(".");
    }

    // Serial.println("Hello, world!"); //send back a hello world
    //Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

    delay(scanspeed);
    // delay(10);

  }
}

```

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 27, 2019, 10:38pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/4 "2019-12-27T22:38:05Z")

</div>

so does this" index1 = val.indexOf(",");" find the first comma or all of them?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [December 27, 2019, 10:53pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/5 "2019-12-27T22:53:18Z")

</div>

Yes, the code you provide manipulates the string by extracting the comma position and splitting the string. You can check any the docs for String [indexOf](https://www.w3schools.com/java/ref_string_indexof.asp) to find a full explanation of this function.

`sonar_distance= val.substring(index1+1, val.length());` is actually taking the value after the comma to the end of the string. Sonar distance will contain the last two values (out of three). If you want to use this algorithm, one approach is to detect the comma and split this value into _sonar distance_ and _ir distance_.

I encourage you to use more flexible approaches as it is easier to read, easier to maintain, easier to update and in short, makes your life easier. If you want to do character manipulation, then I find it is easier to take pen and paper and figure out the algorithm you need and then implement the code.

```auto
String angle, sonarDista, irDista;
String[] rawData = val.split(',');

if(rawData.size()==3){ //Expecting three values
  angle = rawData[0];
  sonarDista = rawData[1];
  irDista = rawData[2];
}

```

Kf

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 28, 2019, 1:43am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/6 "2019-12-28T01:43:19Z")

</div>

> [@kfrajer](#):
>
> if(rawData.size()==3){

possibly in case there is a spare last “,” better

```auto
if(rawData.length >= 3){

```

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 28, 2019, 3:25am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/7 "2019-12-28T03:25:14Z")

</div>

The method split(String) in the type String is not applicable for the arguments (char)

where is the argument declared char?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 28, 2019, 3:42am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/8 "2019-12-28T03:42:19Z")

</div>

```auto
void serialEvent(Serial p) { // ___________________ handle serial data
  String data = trim(p.readStringUntil('\n'));
  if (data != null) {

```

```auto
String[] datas = val.split(data, ",") ;

```

or

```auto
int[] datai = int(val.split(data, ",") );

```

```auto
    if ( datai.length >=3 ) {
//...
    }
  }
}

```

* * *

`oh, but "," or ',' should be same but ' , ' does not work`

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 28, 2019, 4:10am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/9 "2019-12-28T04:10:51Z")

</div>

ok, this is cleaner and easier to understand. but I am still getting an error.

> Cannot invoke size() on the array type String

```auto
void serialEvent( Serial port) {

//the '.' is our end delimiter indicating the end of a complete packet

 String val = trim(port.readStringUntil('.'));

//make sure our data isn't empty before continuing

if (val != null) {

String i, sonar_distance, ir_distance;

String[] rawData = val.split(",");

if(rawData.size()>=3){ //Expecting three values//error=Cannot invoke size() on the array type String[]

  i = rawData[0];

  sonar_distance = rawData[1];

  ir_distance = rawData[2];

}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 28, 2019, 4:14am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/10 "2019-12-28T04:14:26Z")

</div>

see my example:

```auto
datai.length >=3

```

* * *

> ArrayList .size()  
> Array .length  
> String .length()

JAVA ? english ? 🤯

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 28, 2019, 4:17am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/11 "2019-12-28T04:17:11Z")

</div>

yup just found

```
if(rawData.length >=3){ //Expecting three values
```

---

<div class="post-metadata">

### Author: ![urthlight](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@urthlight](https://discourse.processing.org/u/urthlight)
#### Post date: [December 28, 2019, 4:17am UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/12 "2019-12-28T04:17:47Z")

</div>

that makes it happy thanks for your help

---

<div class="post-metadata">

### Author: ![f42ter](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f42ter/32/16793_2.png) [@f42ter](https://discourse.processing.org/u/f42ter)
#### Post date: [December 29, 2019, 11:07pm UTC](https://discourse.processing.org/t/unpacking-serial-data-from-arduino/16662/13 "2019-12-29T23:07:07Z")

</div>

Have a look on my simple Sketch.  
Seriallisation is finished and I’ll sent a linefeed.  
Don’t forget to add ReadSerial.pde to your sketch.

> <https://github.com/f41ardu/Arduino_Serial/blob/master/Arduino_Serial.pde>
