1

I'm desperatly trying to get arduino to divide a string from processing into two sets of variables. In the code below I've decided to just type the important parts but x and y does of course contain the correct values. Any solution would be appreciated. These are my two attempts so far:

Attempt 1 doesn't work at all. 1.Processing:

myPort.write(x + "," + y + "\n");

1.Arduino:

String tempX = Serial.readStringUntil(44);
String tempY = Serial.readStringUntil(10);
String x = tempX.substring(0,tempX.length() -1);
String y = tempY.substring(0,tempY.length() -1);

Attempt 2 where x works correctly but not y. 2.Processing:

String [] dataToSend = new String [2];
dataToSend [0] = x;
dataToSend [1] = y;
String joinedData = join(dataToSend, ":");
myPort.write(joinedData);

2.Arduino:

String x  = Serial.readStringUntil(":");
Serial.read(); //next character is comma, so skip it using this
String y = Serial.readStringUntil('\0');
3
  • What data type are x and y, int or float ? What are the minimum and maximum values they will have ? Commented Apr 23, 2020 at 23:03
  • I have tried int, String and char, none of which made a difference, sadly. Float I haven't tried. Commented Apr 24, 2020 at 15:04
  • What are the min/max values? e.g. (0-255) Commented Apr 24, 2020 at 15:05

1 Answer 1

1

First, don't worry about combining them on the Processing side. Sending two strings one right after the other is the same as sending one long string. It's all being broken into bytes on the Serial line and nobody can tell where one print line stops and the next starts.

myport.write(x);
myport.write(',');
myport.write(y);
myport.write('\n')

will work just as good.

Then on the Arduino side you most likely want to shy away from the String class. Read the data character by character into a char array.

char myArray[howLongTheStringIs];
char x[howLongXIs];
char y[howLongYIs];
int index = 0;

This gets called over and over in loop and picks up serial data as it comes in:

while (Serial.available()){
  char c = Serial.read();
  myArray[index] = c;  // add c to the string
  myArray[++index] = 0;  // null terminate our string
  if(c == '\n'){  // if we are at the end of the string
    handleString();
  }
}

Then you have a function to parse your string there are lots of ways to do that:

If you don't know anything about the strings other than the separator use strtok:

void handleString(){
  char* ptr = strtok(myArray, ":");  // get up to the ":" from the string
  strcpy(x, ptr);  // copy into x
  ptr = strtok(NULL, "\n");  // get from the separator last time up to the next "\n"
  strcpy(y, ptr);  // copy into y
  index = 0         // reset our index and
  myArray[0] = 0;  // and clear the string
}

That's all untested and uncompiled and written in the reply box, so if I made a little typo in there please forgive and correct. But something like this should work. If you already know the exact lengths of the strings (or can send them from the processing code) then the handleString method can be simpler. If you've got something short to do with x and y and don't need them after that then maybe you can just keep pointers to where they are in myArray. It all depends on what the larger picture goal of your code is. But something like this should get the job done.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I will try that definitely. I do know the length of each variable value so I will look into handleString.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.