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 & HelpSyntax Questions › problems with strings coming over serial port
Page Index Toggle Pages: 1
problems with strings coming over serial port (Read 533 times)
problems with strings coming over serial port
Feb 13th, 2008, 10:59pm
 
Hi there...

I am learning to get Processing to talk to an Arduino board over the serial connection. However, I have some problems with the data coming in. This data is in the form of an ASCII string from 0 to 1024 followed by a carriage return.

Basically, I am perfectly able to get the incoming data to show up when I treat it as a string. However, when I try to convert the string to an integer so that I can do something useful with it, all I get is a long chain of zeroes.

Here's the code I'm using right now...what could the problem be?
Code:

import processing.serial.*;

Serial port;
int datapoint = 0;

void setup () {
println(Serial.list());
port = new Serial (this, Serial.list()[0], 9600);
}

void draw () {

while (port.available() > 0) {
String n = port.readString();
if (n != null) {
int datapoint = int(n);
println(datapoint);

}

}
}


Basically this code gives me a chain of zeroes. If I swap println(datapoint); for println(n); it will give me the data but not in a form I can use.

Thanks,
CUID
Re: problems with strings coming over serial port
Reply #1 - Feb 13th, 2008, 11:52pm
 
What, exactly gets printed when you do println(n); ?
Re: problems with strings coming over serial port
Reply #2 - Feb 14th, 2008, 4:29pm
 
Here's what is coming over the Arduino serial monitor:
Code:

804

847

850

852

855

852

850

843

835

831


And here's another sample of what I get in the console of the processing IDE  with println(n) (different data coming in, but this is an example of the formatting)

Code:

100

99

99

98

97

96

95


Seems exactly the same. Interesting that there is a space between the data points when I paste it here -- I'm starting to think it's a formatting thing. Do I need to strip carriage returns or something?
Re: problems with strings coming over serial port
Reply #3 - Feb 14th, 2008, 4:52pm
 
Try something like
Code:
println("value##"+n+"##");


So you can see exactly what is in each n. If the first value in n is "newline" then when converted to an int it'll be 0.
Re: problems with strings coming over serial port
Reply #4 - Feb 14th, 2008, 8:32pm
 
Thanks! That was extremely useful. It seems that there's a synchronization problem between the rate the arduino is outputting data and the rate that the program is reading it.

First, I reprogrammed the arduino to output comma-terminated data instead of CR-terminated, since it's easier to see. result is the arduino gives me data like the following:

Code:
846,821,799,779,770,756,749,741,730,727,723,717,714,712,714,711,712,717,719,721,728,731,736,741,742,748,753,755,759,764,766,771,774,781,783,784,
788,787,787,789,788,787,786,780,779,778,776,775,774,776,775,774,773,774,775,776,781,786,786,724,666,607,548,490,430,370,306,242,186,186,186,187,188


using println("value##"+n+"##"); on that incoming data gives the following:

Code:
value##9,9,9,9,10,10,10,10,10,##
value##10,8,8,##
value##8,8,8,7,7,##
value##7,7,7,7,7,7,##
value##7,7,8,7,7,7,7,7,7,7,##
value##8,8,8,9,8,##
value##8,8,9,9,##
value##9,8,9,9,9,9,##
value##9,9,8,8,8,8,7,7,##
value##7,7,8,##
value##8,##


So yeah, the problem is definitely a synch error. :p
I'm going to try taking the string data and splitting it at the commas and putting it in an array instead. For the record, any idea what I might do to get these synched properly? I have a 20-msec delay between updates on the arduino, so a 50hz sampling rate; I set the framerate of the Processing code to 50  as well, but it doesn't help.

Thanks again.
Page Index Toggle Pages: 1