# Sending Strings from Arduino to Processing

**URL:** https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615
**Category:** Electronics (Arduino, etc.)
**Created:** [September 2, 2022, 12:56pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615 "2022-09-02T12:56:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)
#### Post date: [September 2, 2022, 12:56pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/1 "2022-09-02T12:56:32Z")

</div>

I want to get Processing to read Strings from Arduino.  
I send two Strings massages from the arduino and I want to store them in two different variables on Processing.  
I tried to do it, but the two Strings are passed to the first variable and the second variable remains empty. I don’t understand why this is the case. Can someone help?

Regards

**Arduino Code**

```auto
    void setup() {
      Serial.begin(9600);
      delay(1000);
      Serial.println("1.first massage");
      Serial.println("2.socend massage"); 
      delay(100);
    }
    void loop() {
    
    }

```

**Processing Code**

```auto
 import processing.serial.*;
    Serial myPort;
    void setup() {
      myPort=new Serial(this, "COM3", 9600);
    }

    void draw() {
      String s1=myPort.readStringUntil('\n');
      String s2=myPort.readStringUntil('\n');

    // printing variables
      if(s1!=null){
      print("s1:",s1);
      }
      if(s2!=null){
       println("s2:",s2);
      }
    }

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [September 2, 2022, 1:25pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/2 "2022-09-02T13:25:45Z")

</div>

What’s wrong with an array:  
[https://stackoverflow.com/questions/73545324/sending-strings-from-arduino-to-processing](https://stackoverflow.com/questions/73545324/sending-strings-from-arduino-to-processing)

You’re sending two separate strings, each with a line feed terminator. Processing can’t read both of them at once and magically split them into two variables that only you know about. Processing will read each one as they are sent. First pass will fill index[0] of the array. Second pass will fill index[1]. You could send both messages as one long string and then parse them into s1 and s2 by using some terminator after message 1, but that is more complicated than what you are doing. The way it is Processing is only going to report two separate readings because that’s what you told it to do: read up to the first line feed (Arduino ‘ **println** ’ puts a line feed at the end of the string) and then read up to the second line feed. With the array concept what comes in first is s[0] and what comes in second is s[1]. If you must use the s1 and s2 variables then s1 = s[0] and s2 = s[1]. Perhaps the following will help you see what is going on:

```auto
import processing.serial.*;

Serial myPort;
String[] s; // Array to hold two strings.
int counter = 0;

void setup() { 
  myPort = new Serial(this, "COM3", 9600);
  s = new String[2];
  println("===========");
}

void draw() { 
String str = myPort.readStringUntil('\n');
  if(str != null) {
    s[counter] = str;  
    printArray(s);
    println("===== end of read",counter+1 + " =======");
    counter++; 
  } 
  
} 

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 2, 2022, 1:47pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/3 "2022-09-02T13:47:28Z")

</div>

Hello @maro,

There are many topics on this subject in this forum.

Give the search in the forum a try.

`:)`

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [September 2, 2022, 4:19pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/4 "2022-09-02T16:19:16Z")

</div>

If you don’t want to use an array, then you could use something like this:

```auto
import processing.serial.*;

Serial myPort;
int counter = 0;
String s1, s2;

void setup() {
  myPort=new Serial(this, "COM3", 9600);
  println("*********end of setup()*********");
}

void draw() {
  String str = myPort.readStringUntil('\n');
  if (str != null) {
    if (counter == 0) {
      s1 = str;
      print("s1:", s1);
      println("====== end of read",counter+1 + " ===========");
    }
    if (counter == 1) {
      s2 = str;
      print("s2:", s2);
      println("====== end of read",counter+1 + " ===========");
    }
    counter++;
  }
}

```

---

<div class="post-metadata">

### Author: ![maro](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@maro](https://discourse.processing.org/u/maro)
#### Post date: [September 2, 2022, 4:29pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/5 "2022-09-02T16:29:54Z")

</div>

thank you @svan. I posted first here but my account was blocked and the post was hidden until now. So I posted the same question on Stack overflow. It is now clear for me 🙂

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [September 2, 2022, 5:22pm UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/6 "2022-09-02T17:22:44Z")

</div>

Good luck on your project. I understand the double posts now. This is a good forum and I’m sorry that your initial question was not posted right away. Thank you for your participation.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [September 3, 2022, 10:16am UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/7 "2022-09-03T10:16:37Z")

</div>

The Arduino sketch is sending your text followed by Carriage Return (CR) and Line Feed (LF). You can see the exact characters by putting this in place of your draw() code.

```auto
  int inp;
  while (myPort.available() > 0)
  {
    inp = myPort.read();
    print(String.format("%02X ",inp));
  }

```

Output:

```auto
31 2E 66 69 72 73 74 20 6D 61 73 73 61 67 65 0D 0A 32 2E 73 6F 63 65 6E 64 20 6D 61 73 73 61 67 65 0D 0A   

```

The “0D 0A” is CRLF. In Processing that CRLF will be on the end of your s1, s2 vars. This might cause confusion later. I prefer to have only LF with this Ard line.

```auto
Serial.print("1.first massage\n");

```

In Processing, where you print s1, it looks like you are using println because of the CRLF contained in s1. (s2 the same.)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [September 3, 2022, 11:01am UTC](https://discourse.processing.org/t/sending-strings-from-arduino-to-processing/38615/8 "2022-09-03T11:01:13Z")

</div>

> [@RichardDL](#):
>
> The Arduino sketch is sending your text followed by Carriage Return (CR) and Line Feed (LF).

The Arduino reference:

> **[Serial.println() - Arduino Reference](https://www.arduino.cc/reference/en/language/functions/communication/serial/println/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

`:)`
