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.
Page Index Toggle Pages: 1
Data question (Read 985 times)
Data question
Apr 11th, 2009, 8:39am
 
Sketch,
Code:

import processing.serial.*;
Serial myPort;
String inString;
int[] Data = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

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

void draw()
{
 // Just useing console at this time
}

void serialEvent(Serial p)
{
 inString = (myPort.readString());
 println(inString);
 Data = int(split(inString,','));
 println(Data[0]);
 println(Data[1]);
 println(Data[2]);
 println(Data[3]);
 println(Data[4]);
 println(Data[5]);
 println(Data[6]);
 println(Data[7]);
 println(Data[8]);
 println(Data[9]);
 println(Data[10]);
 println(Data[11]);
 println(Data[12]);
 println(Data[13]);
 println(Data[14]);
 println(Data[15]);
 println(Data[16]);
}


Problem.
All Data prints fine except for Data[0] The first time Data is received, it displays the correct number, 234, each data received after that Data[0] contains 0.

Here is the results,
Code:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "COM3"
234,79,79,100,408,195,100,100,613,332,100,100,764,432,100,100,0;
234
79
79
100
408
195
100
100
613
332
100
100
764
432
100
100
0

234,79,79,100,408,195,100,100,612,331,100,100,764,432,100,100,0;
0
79
79
100
408
195
100
100
612
331
100
100
764
432
100
100
0

234,79,79,100,408,195,100,100,613,332,100,100,764,432,100,100,0;
0
79
79
100
408
195
100
100
613
332
100
100
764
432
100
100
0


As you can see, the first time Data[0] is correct, after that it is 0 even though the incomeing string shows the first data to be 234.

Does it have something to do with the ';' at the end?

note: I have also tried simply int[] Data;
Re: Data question
Reply #1 - Apr 11th, 2009, 9:59pm
 
It seems to be something in the communication. Using a fixed String with your example always gives the correct results.

It may have something to do with the buffer. You could use readStringUntil(';') and see if it does the trick.
Re: Data question
Reply #2 - Apr 12th, 2009, 2:27am
 
I see NoahBuddy answered already: that's the problem of double posting, you duplicate efforts of people trying to help.
Anyway, like NB, I found the code working outside of serial context, so your problem is quite puzzling:
Code:
int[] Data;
void setup()
{
String inString = "234,79,79,100,408,195,100,100,613,332,100,100,764,432,100,100,0;";
println(inString);
Data = int(split(inString,','));
ShowData();
Data[0] = 42;
Data = int(split(inString,','));
ShowData();
}

void ShowData()
{
for (int i = 0; i < Data.length; i++)
{
print(Data[i] + " ");
}
println("EOD");
}
Re: Data question
Reply #3 - Apr 12th, 2009, 8:46am
 
Thanks and sorry for the double posting, I know better  Undecided

But am in a hurry to get it figured out. I will play with it more this eveing. I assume then the syntax is ok.
Re: Data question
Reply #4 - Apr 12th, 2009, 10:02am
 
Something new,If I change it from an int to a string the Data[0] reports fine. parplexing.

I'll try the suggestion from NB next, thanks
Re: Data question
Reply #5 - Apr 13th, 2009, 10:59am
 
ok, wrap your brain around this,

I copied this sketch from the referance web page and modified it a bit for my data,
Code:

// Example by Tom Igoe

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
int[] Data;
Serial myPort;  // The serial port

void setup()
{
 // List all the available serial ports
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my  Keyspan adaptor, so I open Serial.list()[0].
 // Open whatever port is the one you're using.
 myPort = new Serial(this, Serial.list()[0], 9600);
 myPort.clear();
 // Throw out the first reading, in case we started reading
 // in the middle of a string from the sender.
 myString = myPort.readStringUntil(lf);
 myString = null;
}

void draw()
{
 while(myPort.available() > 0)
   {
   myString = myPort.readStringUntil(lf);
   if (myString != null)
{
println(myString);
Data = int(split(myString,','));
println(Data[0]);
}
   }
}


IT WORKS!!!
Results
Code:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "COM3"
235,80,80,100,406,194,100,100,609,329,100,100,758,428,100,100,0;

235
235,80,80,100,406,194,100,100,609,329,100,100,758,428,100,100,0;

235
235,80,80,100,406,194,100,100,609,329,100,100,759,429,100,100,0;

235


HOWEVER, This sketch is 99% identical and it DOES NOT WORK.
Code:

import processing.serial.*;
int sc = 59;
String inString = null;
int[] Data;
Serial myPort;

void setup()
{
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[0], 9600);
 myPort.clear();
 inString = myPort.readStringUntil(sc);
 inString = null;
}  

void draw()
{
 while(myPort.available() > 0)
 {
   inString = myPort.readStringUntil(sc);
   if (inString != null)
{
println(inString);
Data = int(split(inString,','));
println(Data[0]);
}
 }
}


Results
Code:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
[0] "COM3"
236,80,80,100,407,194,100,100,610,330,100,100,759,429,100,100,0;
236

236,80,80,100,406,194,100,100,610,330,100,100,759,429,100,100,0;
0

235,80,80,100,406,194,100,100,610,330,100,100,759,429,100,100,0;
0


Now notice that there is an extra line feed in the previous results that these results do not have. Also, with this script I can not end the program by clicking the "X" on the window that opens. I must click the "Stop" button in the Processing window.


Other than the delimiter, I just don't see anything different between the 2 programs. Why does one work and the other does not?????

EDIT
I may have answered my own question. IF I change the delimiter from 59 to 10 in the sketch that does not work, it then works correctly, so why does it not function correctly when useing 59 instead of 10? My assumtion is that the line feed is then replaceing the data in Data[0] for some reason.

If I am reading until a ";" is found then any thing else, such as a line feed after that point should be ignored, correct? Maybe the serial lib needs a looking at. ??
This seems that it will be an issue when useing anything other than a Line Feed as the delimiter.

EDIT EDIT
Ok, if I use clear() (must be in a specific spot) if seems to take care of the issue for the most part, but it still sometimes misses and Data[0] will display as 0.

Code:

   if (inString != null)
{
println(inString);
Data = int(split(inString,','));
println(Data[0]);
myPort.clear();  
   }

Note the location of myPort.clear();  any other location will not work. Once inString is done "gathering" unitl ";" I should be able to clear the buffer, but this doesn't seem to be the case. Clearing the buffer also seems to clear the value in inString.

EDIT EDIT EDIT
Also note that this,
int[] Data;

Does not init the array with all 0's, as some other lang do. I must do this,
int[] Data = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

otherwise I will ocasionally get "Null pointer Exception" errors, but not consistantly. It seems to depend on how fast the data is received.

I have tried int[17] Data; but that doesn't compile and Other lang I use do it as int Data[17]; will init all elements to 0, [0]-[16] but none of these compile.
Re: Data question
Reply #6 - Apr 15th, 2009, 5:11am
 
Back to the drawing board.

I now have Data[0] returning the correct number from the inString by useinf the bufferUntil(10) instead of bufferUntil(59), However, Data[16] is now always returning 0, even if the incoming data is 1.

I don't get it, I must be missing something wrong here.

EDIT
Here is the test sketch
Code:

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
int[] Data;// Data;
Serial myPort;  // The serial port

void setup()
{
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[0], 9600);
 myPort.clear();
 myString = myPort.readStringUntil(lf);
 myString = null;
}

void draw()
{
 while(myPort.available() > 0)
   {
   myString = myPort.readStringUntil(lf);
   if (myString != null)
{
println(myString);
Data = int(split(myString,','));
println(Data[16]);
}
   }
}


Here is the results
Code:

237,81,81,100,405,193,100,100,637,348,100,100,820,470,100,100,1;

0


Re: Data question
Reply #7 - Apr 15th, 2009, 6:13am
 
Jassper wrote on Apr 13th, 2009, 10:59am:
If I am reading until a ";" is found then any thing else, such as a line feed after that point should be ignored, correct
Not the specialist of serial, but I think it is wrong. When you do readStringUntil(), what is not read probably remains in the buffer, and further data is appended. You should do clear on each read. That's why you had to read up to linefeed.

Quote:
Ok, if I use clear() (must be in a specific spot) if seems to take care of the issue for the most part
Ah, that's what I meant.



Quote:
Also note that this,
int[] Data;

Does not init the array with all 0's, as some other lang do. I must do this,
int[] Data = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

otherwise I will ocasionally get "Null pointer Exception" errors, but not consistantly. It seems to depend on how fast the data is received.

I have tried int[17] Data; but that doesn't compile and Other lang I use do it as int Data[17]; will init all elements to 0, [0]-[16] but none of these compile.

Wow, lot of misconceptions...
With int[] Data; line, you get a variable pointing to an array that is not yet defined. So Data is null, indeed.
You don't need to use { 0, 0, ... } though, fortunately. Just do:
int[] Data = new int[17]; and the entries will be set to 0.
But of course, you don't need to do that, since split() result will replace this array anyway.

Oh, and while typing, I understood what is the (last) problem: after split(), the 16th element is "1;", which isn't a correct number, so int() will give 0 for it.

Either you explicitly remove this char after reading the string, or you use read until ";" and just clear the buffer after it.
Re: Data question
Reply #8 - Apr 15th, 2009, 7:09am
 
Hay thanks for the speedy reply  and the good info Smiley

Quote:
You don't need to use { 0, 0, ... } though, fortunately. Just do:
int[] Data = new int[17]; and the entries will be set to 0.
But of course, you don't need to do that, since split() result will replace this array anyway.


If I don't fill the array with 0's, then when I run the sketch it will give me a "Null pointer exception" error if there is no data comeing in when any of the println lines are executed. I read in another thred that I can probably avoid this by first checking for a null.

Quote:
Oh, and while typing, I understood what is the (last) problem: after split(), the 16th element is "1;", which isn't a correct number, so int() will give 0 for it.

Either you explicitly remove this char after reading the string, or you use read until ";" and just clear the buffer after it.

I had a type-o in that last post, I'll go back to correct it but my last sentance sould have been,
However, Data[16] is now always returning 0, even if the incoming data is 1.


So before it was Data[0] that was always 0, now I have that working but Data[16] is always 0 no mater what the incomeing data is.

So you are saying that "1;" will not truncate to 1? Most other languages I work with will ignore any non numerical character and drop it off, so for example a string of "XR45" changed to an int would be 45.

I will try playing with the clear() somemore, but last time all I got was either errors in the sketch, or no data at all.

thanks again
Re: Data question
Reply #9 - Apr 15th, 2009, 9:24am
 
OK, I split it a second time to get rid of the ";" character.
Code:

while(myPort.available() > 0)
{
inString = myPort.readStringUntil(sc);
if (inString != null)
{
String[] sTemp = split(inString,';');
println(sTemp);
Data = int(split(sTemp[0],','));
println(Data[0]);
println(Data[16]);
myPort.clear();
}


Everthing seems to be working now. So much to learn....
Page Index Toggle Pages: 1