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?????
EDITI 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 EDITOk, 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 EDITAlso 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.