Real Time Sampling & Graph
in
Core Library Questions
•
2 years ago
hi again!
now that my starting-troubles are cleared, I have a more complex problem, and I hope you can help me...
I´m trying to draw a linegraph with data that comes trough a TCP socket stream.
the data is coming continuously at a speed of 100 samples per second. As the data comes trough TCP socket, the data is beeing "packaged", so I get a bounch of data about once per second (about 100 samples, but not exactly 100...)
I tried to write the data into an ArrayList (because the amount of bytes may vary), but this throws an error, so that I had to use a byteArray
I write the data like this:
every 1000 mills I call the funcition ReadData(), so the buffer is beeing filled again.
my problem is, I´m loosing data.
I found the BufferedInputReader, and I think it may be helpfull in my case, but I cannot find how to use it with TCP client.
I post the whole code of the sketch, may be you can give me some hints what I can do better...
At the moment the input is a rectangle at 1,1 Hz with 50% dutycycle, for the case anyone of you might try to run the sketch....
now that my starting-troubles are cleared, I have a more complex problem, and I hope you can help me...
I´m trying to draw a linegraph with data that comes trough a TCP socket stream.
the data is coming continuously at a speed of 100 samples per second. As the data comes trough TCP socket, the data is beeing "packaged", so I get a bounch of data about once per second (about 100 samples, but not exactly 100...)
I tried to write the data into an ArrayList (because the amount of bytes may vary), but this throws an error, so that I had to use a byteArray
I write the data like this:
- public void readData(){
if (myClient.available() > 0) {
// Read in the bytes
byteCount = myClient.readBytes(byteBuffer);
myClient.clear();
println (byteBuffer);
println ("bytes:" +byteCount);
}
- for( i = 0; i<numbers.length;i++){
vertex(i,200-numbers[i]);
if (byteCount > 0 ) {
{
if ((byteBuffer[i]==36 ) && (byteBuffer[i+1]==18))
{
j=unhex(hex(byteBuffer[i+2])+(hex(byteBuffer[i+3])));
numbers[numbers.length-1]=200-j/5;
}
}
}
}
every 1000 mills I call the funcition ReadData(), so the buffer is beeing filled again.
my problem is, I´m loosing data.
I found the BufferedInputReader, and I think it may be helpfull in my case, but I cannot find how to use it with TCP client.
I post the whole code of the sketch, may be you can give me some hints what I can do better...
At the moment the input is a rectangle at 1,1 Hz with 50% dutycycle, for the case anyone of you might try to run the sketch....
- import processing.net.*;
Client myClient;
byte[] byteBuffer = new byte[503];
int[] numbers = new int[499];
int f =0;
int i=0;
int t=0;
int j=0;
int byteCount=0;
// Declare a global time tracking variable
long timeSinceLastEvent;
long DELAY_BETWEEN_EVENTS = 1000; // 1s
void setup(){
size(600,500);
frameRate(15);
myClient = new Client(this, "alano.dyndns.org", 9760);
myClient.write("S");
}
void draw(){
background(255);
//GraphPaper
for( i = 0 ;i<=width/10;i=i+1){
stroke(200);
line((-frameCount%10)+i*10,0,(-frameCount%10)+i*10,height);
line(0,i*10,width,i*10);
}
noFill();
stroke(0);
beginShape();
for( i = 0; i<numbers.length;i++){
vertex(i,200-numbers[i]);
if (byteCount > 0 ) {
{
if ((byteBuffer[i]==36 ) && (byteBuffer[i+1]==18))
{
j=unhex(hex(byteBuffer[i+2])+(hex(byteBuffer[i+3])));
numbers[numbers.length-1]=200-j/5;
}
}
}
}
endShape();
for(t = 1; t<numbers.length;t++){
numbers[t-1] = numbers[t];
}
if (millis() - timeSinceLastEvent > DELAY_BETWEEN_EVENTS)
{
readData();
timeSinceLastEvent = millis();
}
}
//////////////////////////////
public void readData(){
if (myClient.available() > 0) {
// Read in the bytes
byteCount = myClient.readBytes(byteBuffer);
myClient.clear();
println (byteBuffer);
println ("bytes:" +byteCount);
}
}
1