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 › separate index from integer in String(serial port)
Page Index Toggle Pages: 1
separate index from integer in String(serial port) (Read 1977 times)
separate index from integer in String(serial port)
Apr 5th, 2010, 11:52am
 
Hello,

any hint how to separate the integer from the index at the result below

see here what I get from the serial port:

http://gyazo.com/345946cd2968f122af9f6a2cb2c6d4a6.png

and this is my code:

Code:
import processing.serial.*;
Serial port;
byte[] inbyte = new byte[4];
void setup()
{
 port = new Serial(this,Serial.list()[1], 9600);
}

void draw()

{
    delay(300);  
while(port.available()>0)
{
   String r = port.readString();
 println(r);
         delay(500);
         String g = port.readString();
 println(g);
         delay(500);
         String b = port.readString();
 println(b);
         delay(500);
}
}

Re: separate index from integer in String
Reply #1 - Apr 7th, 2010, 1:04am
 
Perhaps split()
Then use int() on the results.

http://processing.org/reference/split_.html
Re: separate index from integer in String
Reply #2 - Apr 7th, 2010, 1:37am
 
Hello,

thanks a lot for the answer..I tried split but when I use split[0] I get a result of zeros..
when I use split[1] I get an error

ArrayIndexOutOfBoundsException: 1

see my code with "split" below..

Code:
import processing.serial.*;
Serial port;
byte[] inbyte = new byte[4];
void setup()
{
 port = new Serial(this,Serial.list()[1], 9600);
}

void draw()

{
    delay(300);  
while(port.available()>0)
{
   String r = port.readString();
 
 String[] list = split(r, ',');
           int str2int = int(list[0]);

           println(str2int);
}
}
Re: separate index from integer in String(serial port)
Reply #3 - Apr 7th, 2010, 2:25am
 
thanks again for the hint - the code works with splitTokens! The only thing is that the delay that I have in my code..works strangely..when I change the delay time my output changes and sometimes if the delay is not long enough I get an error..
If I change the delay time for 100ms I get different output and I have a long array to read so I want to be sure I get the correct data...
It seems though comparing the output with arduino's serial monitor that it works well with a delay of 600.
any hint how I can figure out a "correct" delay time or why is this happening?any hint to improve this code below?



Code:
import processing.serial.*;
Serial port;
byte[] inbyte = new byte[4];
void setup()
{
 port = new Serial(this,Serial.list()[1], 9600);
}

void draw()

{
   
while(port.available()>0)
{
   String r = port.readString();
 
 String[] list = splitTokens(r);
         int str2int = int(list[1]);
         println(str2int);
         delay(600);  
}
}

Re: separate index from integer in String(serial port)
Reply #4 - Apr 10th, 2010, 3:02am
 
Since draw() can (in theory) run at any time, the delay simply makes sure that you have given enough time to receive the entire message.

You could use http://processing.org/reference/libraries/serial/Serial_readChar_.html to build up a string and watch for a specific character, say an end of line (or just add a * or something). That way your sketch will know exactly when to split the message.
Page Index Toggle Pages: 1