Pages: 1 [2]   Go Down
Author Topic: communication with processing messed up  (Read 1623 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

print("something\n\r") (or print("something\r\n") (I can never remember the correct order)) and println("something") are equivalent.
Logged

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

I used  port.bufferUntil('\n')
But still no luck. How hard can it be?

Serial monitor:
Quote
1
2
3
4

6
8
0
1
2
1151671
120

Quote

5



7



9

1

Code:
void setup() {
  Serial.begin(9600);
}

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


Code:
import processing.serial.*;

Serial port;
String inString;

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

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

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

« Last Edit: October 24, 2011, 07:39:00 am by clankill3r » Logged

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

Code:
  port.write(frameCount);
This is writing the frame count out as a string - NOT A SINGLE CHARACTER.

Code:
  if (Serial.available()) {
    Serial.println(Serial.read());
  }
This is reading ONE CHARACTER and pretending that it is the COMPLETE frame count. It, obviously, is NOT.

Put this in the Processing application, instead.
Code:
port.write("[");
port.write(frameCount);
port.write(']");

Try this on the Arduino:
Code:
bool started = false;
bool ended = false;

char inData[20];
byte index;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
   while(Serial.available() > 0)
   {
      char inChar = Serial.read();
      if(inChar == '[')
      {
        started = true;
        ended = false;
        index = 0;
        inData[index] = '\0';
      }

      else if(inChar == ']')
      {
         ended = true;
         break;
      }

      else
      {
         if(index < 19)
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }
      }
   }

   if(started && ended)
   {
      Serial.print("Received: <");
      Serial.print(inData);
      Serial.println(">");
   }
}
Logged

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

Code:
  port.write(frameCount);
This is writing the frame count out as a string - NOT A SINGLE CHARACTER.
The frameCount keeps incrementing so after a while it get's higher then 255/256 which is the maximum character value if i'm correct.

Quote
Code:
  if (Serial.available()) {
    Serial.println(Serial.read());
  }
This is reading ONE CHARACTER and pretending that it is the COMPLETE frame count. It, obviously, is NOT.
i must have looked over that, thanks, good to know

I tryed the code you posted, thanks for the contribution!!
What does this line do?
inData[index] = '\0';

(that line is also in the          if(index < 19) part, where it is not needed i believe).

Unfortunatly it's still not working:
My serial monitor output is:
Quote
Recived
Receivd: <>
Receied: <>
Recived <>
Red: <>
Receied: Received <>
Receive: <>
Rceived: >
ece <>
Receive: <>
R
Received: >
ved: <>
Recived>
Rceivd: >
ved: <>

and the processing console shows:
Quote
R<>
eive: <

RecReceiveived: < <>

Rece: <>

ived: <>

Reed: <   >

>

Received>

Rved: < >

Receied: <
>

What do i need for bufferUntil (or nothing)?

port.bufferUntil('\n');

Code:
import processing.serial.*;

Serial port;
String inString;

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

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

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

Your boolean started was never set back to false so it could be removed.
I also removed the other boolean and ended up with this:
(afcorse still no frameCount cause i continued on something that didn't work).

Code:
char inData[20];
byte index;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == '[')
    {
      index = 0;
      inData[index] = '\0';
    }

    else if(inChar == ']')
    {
      Serial.print("Received: <");
      Serial.print(inData);
      Serial.println(">");
      break;
    }
    else
    {
      if(index < 19)
      {
        inData[index++] = inChar;
      }
    }
  }


}

Logged

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

Quote
The frameCount keeps incrementing so after a while it get's higher then 255/256 which is the maximum character value if i'm correct.
Yeah. So? Big difference between a string (an array of characters) and a character. When frameCount is 2,348,448,998,723, Processing will still be able to send it as a string.

Quote
What does this line do?
inData[index] = '\0';
It adds a NULL terminator. When index is 0, it effectively empties the array.

Quote
(that line is also in the          if(index < 19) part, where it is not needed i believe).
Yes, it is.

I'm beginning to think that your problem is that you have two applications trying to read the serial data sent by the Arduino, and they are interfering with each other. I do not know how you are able to do that, but you really need to stop doing it.
Logged

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

if i test this in processing:
Code:
void setup() {
  char t = 'a';
  t = '\0';

  println(t);
}

then it prints nothing, if i comment the t = '\0'; line then it prints 'a' to the console.

So in this part of your code:
Code:
         if(index < 19)
         {
            inData[index++] = inChar;
            inData[index] = '\0';
         }

Shouldn't this line:             inData[index] = '\0';
not be one line higher? (and then ++ switched afcorse).

And about the interfacing serials, i stripped the processing code to this:
Code:
import processing.serial.*;

Serial port;

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

void draw() {
  port.write("[");
  port.write(frameCount);
  port.write("]");
}

my serial monitor output is this:
Quote
Received: <>
Receivived: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>
Received: <>

Logged

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

Quote
then it prints nothing
When you set t to NULL, it makes it "nothing", so when nothing is printed, that is the expected behavior.

Setting a character to NULL, and setting an element in a character array to NULL have much different results.

Quote
Shouldn't this line:             inData[index] = '\0';
not be one line higher? (and then ++ switched afcorse).
No, it shouldn't.

The elements in an array are like boxes in a post office. There is one letter in each box. If you open the boxes in order, and read each letter, until you encounter an empty box, you make something of the letters you have read.

The "empty box" is the NULL character in a char array.

What that code is doing is putting a letter in a box, and removing the letter from the next box, saying "stop reading when you get here".

Quote
i stripped the processing code to this
Quote
my serial monitor output is this:
I'm not sure how many more times I need to say this. There can only be one application on each end of the serial port. The Arduino is on one end. If the serial monitor is on the other end, Processing can not be there, too. So, the Arduino gets no data.

If the Processing application is there, then the Serial Monitor should not be trying to intercept the output.

You can make Processing read the responses from the Arduino. That is the ONLY way that you can debug the communications process.

Until this sinks in, I can't help you any more.
Logged

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

ok got it smiley
And i used a pot meter for now. I will give it another attempt later.
Logged

Pages: 1 [2]   Go Up
Jump to: