Sending Strings from Arduino to Processing

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

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

Processing Code

 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);
      }
    }

What’s wrong with an array:
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:

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++; 
  } 
  
} 
1 Like

Hello @maro,

There are many topics on this subject in this forum.

Give the search in the forum a try.

:)

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

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++;
  }
}

1 Like

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 :slight_smile:

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.

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.

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

Output:

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.

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.)

1 Like

The Arduino reference:

:)