Using split with serial data
in
Programming Questions
•
2 years ago
I am using an Arduino to measure two sensor readings and send the data back in a string with the values separated by a semicolon like: 123.4;888
I am trying to divide the string into the two readings using the split function but can never access the second value in the string. The relevant debug code is below with the meat happening in the serialEvent. Printing the raw string and the split string array shows the entire string and data are being received properly but if I want to print (manipulate, compare, any operation) just the second value, I get an array index out of bound error. Appreciate any help!
I am trying to divide the string into the two readings using the split function but can never access the second value in the string. The relevant debug code is below with the meat happening in the serialEvent. Printing the raw string and the split string array shows the entire string and data are being received properly but if I want to print (manipulate, compare, any operation) just the second value, I get an array index out of bound error. Appreciate any help!
- import processing.serial.*;
Serial myPort;
int Width = 100;
int Height = 100;
void setup () {
size(Width, Height);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 19200);
myPort.bufferUntil('\n');
background (255);
}
void draw () {
}
void serialEvent (Serial myPort) {
String tempString = myPort.readStringUntil('\n');
tempString = trim(tempString);
println(tempString); //<-- Prints ###.#;####
String[] MyList = split(tempString, ";");
println(MyList); //<-- Prints [0] "###.#" and [1] "####"
println(MyList[0]); //<-- Prints ###.# correctly
// println(MyList[1]); //<-- If uncommented, fails each time with "Caused by: java.lang.ArrayIndexOutOfBoundsException: 1"
}
1

).