what is wrong? parsing serial data with split()
in
Programming Questions
•
2 years ago
I don't understand why split() is always setting the last data value read in to be zero. Can anyone advise?
EDIT: I do see the last value if I split to strings, instead of converting with int(). So the last string in the line contains a newline, and int() sees a non-numeric value and sets it to zero?
Here is the relevant section of my code:
EDIT: I do see the last value if I split to strings, instead of converting with int(). So the last string in the line contains a newline, and int() sees a non-numeric value and sets it to zero?
Here is the relevant section of my code:
- import processing.serial.*; // serial library
Serial myPort; // The serial port
String inBuffer = null; // one line of data from serial port
void draw() {
int[] dat; // array of numbers read in on one line from serial port
if (inBuffer != null) { // wait for new data on the serial port
print("SERIAL:" + inBuffer); // show the line of serial input
dat = int(split(inBuffer, ',')); // parse comma-separated number string into numbers
println("DAT_IN:" + dat[0] + "," + dat[1] + "," + dat[2] + "," + dat[3] + "," + dat[4]);
println();
// [... plus other stuff ...]
}
}
void serialEvent(Serial p) {
inBuffer = p.readString(); // store serial port buffer in global var inBuffer
}
- SERIAL:0,0,1,629,125
DAT_IN:0,0,1,629,0
SERIAL:0,1,5,629,123
DAT_IN:0,1,5,629,0
1