We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Problem splitting serial feed in Processing
Page Index Toggle Pages: 1
Problem splitting serial feed in Processing (Read 2480 times)
Problem splitting serial feed in Processing
May 3rd, 2010, 8:17am
 
Hi,

I'm having some problems importing a string of serial data, and converting it an int array inside processing.
I can't figure out what I'm doing wrong here; so I'm hoping someone in here can help me out.

The string looks like this when I get it inside Processing:
Quote:
Ì811578757985

-7,10,4

-12,2,7

-8,3,5

.. and so on..


This is the Arduino code that I use to send it to Processing:
Code:
void loop(){
receiveData(); //receive data and calculate yaw pitch and roll
Serial.print(outputarray[0]);
Serial.print(",");
Serial.print(outputarray[1]);
Serial.print(",");
Serial.println(outputarray[2]);
}


Here's the code I use to read the data, and split it in the sensors[] array inside Processing:
Code:
void serialEvent(Serial myPort) { 
 String myString = myPort.readStringUntil('\n');
 if (myString != null) {
int sensors[] = int(split(myString, ','));
println(sensors[0]);
println(sensors[1]);
}
}


The weird thing here is that println(sensors[0]); works just fine, and only shows the first value in the string, but  sensors[1] and sensors[2] doesn't.
Any ideas as to why this is?

Also, I'm wondering if the weird garbled data at the beginning of my Arduino feed might have something to do with it. The garbled data changes at every run, so it's never the same.
Re: Problem splitting serial feed in Processing
Reply #1 - May 3rd, 2010, 10:42pm
 
What does myString look like just before the split()?

It seems to work fine with a constant string...
Code:
String myString = "-7,10,4";
 
int sensors[] = int(split(myString, ','));
println(sensors[0]);
println(sensors[1]);
println(sensors[2]);

You might try calling clear() within setup() to empty any junk from the serial buffer.
http://processing.org/reference/libraries/serial/Serial_clear_.html
Re: Problem splitting serial feed in Processing
Reply #2 - May 3rd, 2010, 11:51pm
 
Thanks for the reply. Smiley

Here's how it looks before the split:
...

Yeah, it works just fine with a constant string here as well; no problems there I guess.
Re: Problem splitting serial feed in Processing
Reply #3 - May 5th, 2010, 6:36am
 
I figured out the problem; a delay in the linefeed from Arduino caused the array at index [1] and [2] hadn't been filled with data before I tried to access it.
I've now made it wait a full four seconds before actually trying to access the array.
Page Index Toggle Pages: 1