Pages: [1] 2   Go Down
Author Topic: communication with processing messed up  (Read 1478 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just want to send a delay value with arduino but i thought let's start with sending the frameCount of the processing sketch.

arduino:

Code:
    #define LED 13

    int incoming;

    void setup() {
      Serial.begin(9600);
      pinMode(LED, OUTPUT);
    }

    void loop() {
      if (Serial.available()) {
        incoming = Serial.read();
        Serial.println(incoming);
      }
    }


Processing:


Code:
    import processing.serial.*;

    Serial port;

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

    void draw() {
      port.write(frameCount);
     
    }


This is the output i get in the Serial monitor from arduino (exept the last line):
Apart from it being messed up, also a lot of frames are not coming in...


Quote
    123
    5
    7
    8

    1112
    3
    15

    18

    2122
    3
    2526
    7
    29
    31
    32

    3536
    7
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How many times do you suppose the draw() method in the Processing application is called, while the Arduino reads the serial data that it got? draw() is sending data far faster than the Arduino can read it, so the Arduino's receive buffer fills up. Data that arrives while the buffer is full is placed in the bit bucket. There is no method to retrieve data from the bit bucket.

Your Processing application has not implemented a handler (serialEvent) to be called when serial data arrives, so that data is not processed. Not processing data is far faster than processing it.

The speed at which draw() is called is only marginally affected by serial data processing. You need to add a delay to draw, so that data is sent at a more manageable (by the Arduino) rate.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried with a delay of 1000 in processing but this didn't help.
Also a frameRate(1); didn't help, still bad output (only slower).

Now i tried the followong code, arduino serial monitor has output like this:
Quote
9
69
9989999999
10
19
299
1399499959199
7899999999
2099299
299
329
599

And processing is printing null all the times.


Code:
#define LED 13

int incoming;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    incoming = Serial.read();
    String out = incoming;
    out += 9999;
    Serial.println(out);
  }
}


Code:
import processing.serial.*;

Serial port;
String inString;
int lf = 9999;

void setup() {
  frameRate(1);
 println(Serial.list());
 port = new Serial(this, Serial.list()[1], 9600);
 port.bufferUntil(lf);
}

void draw() {
  port.write(frameCount);
  println(inString); 
}

void serialEvent(Serial port) {
  inString = port.readString(); 
}


p.s. the function delay will be removed in next processing
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
    out += 9999;
You are adding a String and a number. Is that really what you want to do?

Processing can be told to buffer until the carriage return/line feed that the Arduino can send, using Serial.println().

Processing is not putting any separator between the strings that it sends. So, the Arduino has no way of knowing where a number begins and ends.

ItwouldbelikeyoutryingtoreadasentenceItypedwithnospacesbetweenthewords.

Quote
p.s. the function delay will be removed in next processing
I don't see one there, now.

You can do without the delay, if you have Processing send only when the Arduino says that it is ready.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright, here the line feed is a new line, but this also doesn't work...

Quote
void loop() {
  if (Serial.available()) {
    incoming = Serial.read();
    String out = incoming;
    Serial.println(out);
    Serial.println(9999);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    incoming = Serial.read();
    String out = incoming;
    Serial.println(out);
    Serial.println(9999);
Storing the one character read into a String object is a waste of resources.

What is the 9999 being sent for?

For every character read, the Arduino sends 9 back out...

Quote
but this also doesn't work...
What a shame.

If you described what did actually happen, you might get help instead of just (fake) sympathy.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the 9999 was sent for the bufferUntil, i had a high int so it would be higher then the delay i want to send later.
Now i use the char 'h'

Quote
For every character read, the Arduino sends 9 back out...
how's that?

Arduino:
Code:
#define LED 13

int incoming;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    Serial.println(Serial.read());
    Serial.println('h');
  }
}


processing:
Code:
import processing.serial.*;

Serial port;
String inString;
char lf = 'h';

void setup() {
  frameRate(1);
 println(Serial.list());
 port = new Serial(this, Serial.list()[1], 9600);
 port.bufferUntil(lf);
}

void draw() {
  port.write(frameCount);
  println(inString); 
}

void serialEvent(Serial port) {
  inString = port.readString(); 
}


processing output is like:

Quote
h

h



h

5
h

5
h



h

8
h

8
h

90
h

90
h

90
h
1

2

h


h

1
h
6
h
6
h
1h

and arduino output is like:

Quote
1
h
2
3
h
4

6
h7


h
1
1h1h13
14
5

1
7
h
18
19
h
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how's that?
Code:
    incoming = Serial.read();
    String out = incoming;
    Serial.println(out);
    Serial.println(9999);
The first line reads one character.

The 3rd line prints that character plus a carriage return and line feed. 3 characters.
The 4th line prints '9', '9', '9', '9' and a carriage return and line feed. 6 characters.
6 + 3 = 9.

Code:
  if (Serial.available()) {
    Serial.println(Serial.read());
    Serial.println('h');
Now, you are sending the character plus a carriage return and line feed, the 'h' plus a carriage return and line feed, for a total of 6 characters.

Perhaps the Serial.println()s should be Serial.print()s to get rid of the carriage returns and line feeds.

I don't know what frameCount is, or how or where it is initialized or incremented. So, I can't comment on the output you show.

How are you capturing the Arduino output separate from the Processing output? You have one serial connection. Either the Serial Monitor should be on the other end (the Arduino is on one end) OR the Processing application should be on the other end.

What OS are you using?

What, exactly, is the problem we are trying to address?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Then the bufferUntil is useless right?
http://processing.org/reference/libraries/serial/Serial_bufferUntil_.html

Quote
The 3rd line prints that character plus a carriage return and line feed. 3 characters.
A carriage return is like pressing enter/return on the keyboard right? So isn't that a line feed allready?

frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.
It would be like this in arduino (only in processing it's a default thing, you don't have to create the variable frameCount)

Code:
int loopCount = 0;
void loop() {
    loopCount++;
}

Quote
How are you capturing the Arduino output separate from the Processing output? You have one serial connection. Either the Serial Monitor should be on the other end (the Arduino is on one end) OR the Processing application should be on the other end.

The processing pde has a console, if i use println("something"); then it goes to the processing console, i could also create a printWriter to create a textfile for example then i can use output.println("something"); for example and it gets added to the textfile.

Quote
What OS are you using?

mac os x, lion

Quote
What, exactly, is the problem we are trying to address?
I want to set a simple delay with processing. Setting a delay between 0 and 255 is quite easy for that just send a byte, however i need a bigger range. I think something between 0 and 1000ms. Afcorse i could use a byte and multiply it by 4 by the arduino which is more easy (but 4 times less precise) but for the purpose of learning i don't.
So at the moment i just try to send a value with processing, receive it with arduino and then send it back to processing (<< that is my goal).
I used frameCount for now cause it's easy to see if things are going correct since it's produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then the bufferUntil is useless right?
Not at all.

Quote
A carriage return is like pressing enter/return on the keyboard right? So isn't that a line feed allready?
Ever seen a typewriter? There is a lever that you use to return the carriage to the left side of the typewriter, and advance the paper so you can type on the next line. So, the carriage return and line feed are separate activities. The enter key on a PC emulates the carriage return/line feed. How do you suppose it does that? Hint: it was two separate activities.

Quote
frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.
"it just counts" when? Details are crucial. What causes it to increment?

Quote
The processing pde has a console, if i use println("something"); then it goes to the processing console
That explains how you know what Processing received. You seem to know what the Arduino sent, too. How?

Quote
Afcorse i could use a byte and multiply it by 4 by the arduino which is more easy (but 4 times less precise) but for the purpose of learning i don't.
Afcorse.

Quote
I used frameCount for now cause it's easy to see if things are going correct since it's produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.
Then why complicate with that 9999 or h crap. Just use the Serial.println() function on the Arduino and bufferUntil('\n'); in the Processing application.

The Processing application sends a string ("0", "1", ... , "10", "11"). You need to read that string, convert it to a number, and send that number back to Processing (as a string) to validate the communication process.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ever seen a typewriter? There is a lever that you use to return the carriage to the left side of the typewriter, and advance the paper so you can type on the next line. So, the carriage return and line feed are separate activities. The enter key on a PC emulates the carriage return/line feed. How do you suppose it does that? Hint: it was two separate activities.
I understand it with a typewriter, but not with arduino.

Quote
Quote
frameCount prints the current frame number, so when you start a processing sketch it starts with 1 and then it just counts.
"it just counts" when? Details are crucial. What causes it to increment?
from: http://processing.org/reference/frameCount.html
Quote
The system variable frameCount contains the number of frames displayed since the program started. Inside setup() the value is 0 and and after the first iteration of draw it is 1, etc.

So every iteration of draw increments it.

Quote
Quote
The processing pde has a console, if i use println("something"); then it goes to the processing console
That explains how you know what Processing received. You seem to know what the Arduino sent, too. How?
by the serial monitor, or don't you mean that?

Quote
Quote
I used frameCount for now cause it's easy to see if things are going correct since it's produces 1,2,3,4,5,6,7,8,9,10,11 etc. If you look in the output below, you see a bit of it coming back.
Then why complicate with that 9999 or h crap. Just use the Serial.println() function on the Arduino and bufferUntil('\n'); in the Processing application.

bufferUntil('\n'); is a good idea! thanks! wish i could test it right away.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So every iteration of draw increments it.
OK. Facts are good.

Quote
I understand it with a typewriter, but not with arduino.
The Serial.println() function outputs the data specified followed by a carriage return and a line feed. They are separate ASCII characters (10 and 13).

Quote
by the serial monitor
On Windows, you can have the Serial Monitor communicating with the Arduino, OR Processing communicating with the Arduino, but not both. I have no idea what that isn't the same on other OSs.
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is a carriage return there so it knows that the output of the data specified is complete?

And if so, wouldn't it then outputs the data specified followed by a line feed and the the carriage return? (instand of carriage return and then line feed).

cause
Serial.print("something\n");

produces the same as
Serial.println("something");
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
cause
Serial.print("something\n");

produces the same as
Serial.println("something");
Your proof?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 124
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no i assume, in processing those results will be the same:

print("something\n");
println("something");
Logged

Pages: [1] 2   Go Up
Jump to: