Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Simple Serial send receive question

$
0
0

Im having a very hard time understanding serial communication... hopefully one of you can help me out.

I need to write a function to check communication between processing and my arduino via serial.

I am sending the a stream of character "A" from processing to arduino
when Arduino sees an 'A' then it sends a character 'B' back to processing
when processing sees that 'B' then it sends the character 'C' back to arduino

this is done for this reason:

stream of A's sent 1/4 sec apart to allow arduino time in case its not yet ready to receive
once the arduino is receiving data it gets that A and replies with a B to let processing know its online and receiving

processing reads that B then sends arduino a C to let it know it can now proceed to the void loop() function and start streaming the actual sensor data

BTW im using an arduino micro so like Leonardo it uses Serial and Serial1....Serial1 is used to receive data from elsewhere but doesn't apply to this section of the sketch.  likewise on the processing end there are two serial connections established but only myport is used to communicate to this arduino  so myport2 is not applicable either...the robot() class is used elsewhere also but since it has a delay feature I dropped it into this part of the sketch to only send data every 1/4 second so it doesn't fill up the buffer too fast waiting for arduino to respond....If besides the solution you have a better approach to achieve this please let me know...  I am making this stuff up as I go ad don't really know the most efficient way to accomplish this....thnkx


My programs look like this:

Arduino:

[code]
char ch;
int R;
int L;
int comFlag=0;  //0=waiting for initial request to connect from PC
//1= request received...sending confirmation to PC
//2=confirmation acknowledged by PC ready to proceed into main loop()

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  Serial1.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  //ESTABLISH COMMUNICATION LINK WITH PC
  while(comFlag!=2){
    if (Serial.available()) {// wait for data from pc
      ch=(char)Serial.read();      //if data is available read it
      if(ch=='A' && comFlag==0){    
        Serial.println("B");//Send 'A' character back to pc to acknowledge that it received the data
        comFlag=1;//flag to let us know that we are ready for stage two of connection verification process
      }
      if (ch=='C' && comFlag==1){         //check to see if data  in = 'C'
        comFlag=2;
      }

    }
  }
}

void loop() // run over and over
{
.
.
.
}
 [/code]

 

The processing end looks like this:

[code]
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.InputEvent;
import processing.serial.*;
import guicomponents.*;

Serial myPort;
Serial myPort2;
Robot robby;

char message;

void setup() { 
  size(267, 366);
  try
  {
    robby = new Robot();
  }
  catch (AWTException e)
  {
    println("Robot class not supported by your system!");
    exit();
  } 

  println(Serial.list());
  println("  Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this, Serial.list()[portIndex], 19200);
  String portName = Serial.list()[1];
  myPort2 = new Serial(this, portName, 9600);
  robby.mouseMove(xx, yy);
  String[] files;

  //ESTABLISH SRIAL COMMUNICATION WITH HEADSET

  while (message!='B') {//Write 'A' to serial port until a reply is received
    int message = myPort.read();
    println(message);
    myPort.write('A');
    println("Waiting for connection");
    robby.delay(25);
  }
  myPort.write('C');
  println("CONNECTION ESTABLISHED!!!");
  //robby.delay(1000);
}


void draw(){
.
.
.
}
[/code]
 
 
Its just not working....  if I use the Serial monitor in arduino to test and it seems fine.
nothing happening on the monitor
I type an A
it returns a B
I type a C
Sensor data starts to stream....

But when I try to do it through another sketch (in processing)
I see  on the processing monitor area:

Waiting for connection
-1
Waiting for connection 
-1
Waiting for connection
-1
Waiting for connection
-1
Waiting for connection


So its probably not being received the right way
I don't think its a processing issue but I am pretty sure its just my basic understanding of serial communication that's off...

When I send data through a println("A");
am I sending a character
When I receive using ch=(char)Serial.read() do I get a character to compare (if(ch=='A'))

I might be comparing apples to oranges....

Viewing all articles
Browse latest Browse all 1768

Trending Articles