Handshaking Problem

I am trying to set up a basic handshaking between Arduino and the Processing environment. I stripped down my code to a few lines showing the problem: Here's the Arduino Code, simply waiting for a single byte to arrive and then answering by sending an acknowledge.

const byte SDP=0x03;
const byte ACK=0x02;

int rxData;
void setup () {
   Serial.begin(9600); // start serial port at 9600 bps:

void loop(){
while (Serial.available() > 0 ) {

On the Processing side I send a simple data byte and expect the SerialEvent procedure to read the Acknowledge and set the ack flag to true. This would indicate to the loop() that the next byte can be send. In essence I then would expect the Arduino and the processing environment to ping-pong. What happens is that I see a single "SDP" being send but I never receive an ACK from Arduino at least the ack flag never gets set to true again. Am I mis understanding the serialEvent functionality?

Here's the code for the Processing side:

import processing.serial.*;

byte ACK=0x2;
byte SDP=0x03;
Serial myPort;                 // The serial port
boolean ack;

void setup() {
  size(256, 256);  // Stage size
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.buffer(0); // Want to evaluate each byte arriving

void draw() {

  if (ack==true) { 
    println("Ack is true, writing next SDP");

void serialEvent(Serial myPort) {
   println("Entering Serial Event ");
 int inByte;
  if (myPort.available() >0) { 
   inByte = myPort.read();
     if (inByte==ACK) {
        println("Received ACK ");
     } else {
       println("Received " +char(inByte));

Any Help would be great!

You probably want the handshaking to work in the other direction.

When the serial port is opened, the Arduino resets. It takes some time for the serial port to be readied. If the Processing application sends data to soon, which it can because it is already ready, the Arduino won't get the data.

Since the Processing application is waiting for the Arduino to send something, and the Arduino is waiting for the Processing application to send something, you are in for a long wait.

Oh, I wasn't aware of this reset behavior. Did I miss some documentation here?

Now, does the Arduino only reset once when I issue the Serial.begin(9600) command and is then online all the time or is there some timeout that would need a re-opening of the connection after some time of inactivity?

The Arduino resets whenever the serial port is opened or closed. It has nothing to do with when, or if, to execute the Serial.begin() command.

There is no timeout.

I changed the handshake sequence as you suggested. Works fine now. Thanks for your help!