Problem with serial.clear() in handshake procedure

Hi everybody!

I recently made a front end for a few maintenance functions embedded in my Teensy (Adruino) project
and I attempted to implement a Serial port autodetection by making a simple serial handshake procedure.

The sketch sends an H and the duino replies a Q and then the party can start.
The problem is that after integrating the handshake detect and response code (first snippet below, not rocket science) in the rest of the firmware, there is always other data (boot messages, etc…) in the buffer before I check for the response character and serial.clear() just does not remove the preceding data in the buffer and my check for the response character fails.

I presume it may have something to do with creating multiple instances of the serial class, but I couldn’t find a way to destroy the ones I’ve checked and deemed useless.

Any suggestions would be appreciated and let me know if more of my code is needed to undestand what is happening.

Arduino side HS

bool connOK = false;

void handshake()
    if(Serial.available() > 0)
      char inChar =;
      if(inChar == 'H'){Serial.print("Q"); connOK = true;}

Processing HS

void setup() {
  cp5 = new ControlP5(this);
  // Find Serial Port that the Logic Board is on 
  // Send "H" to Serial ports, expect to get "Q" back
  long millisStart; 
  int i = 0; 
  int len = Serial.list().length;  //get number of ports available 
  println(Serial.list());   //print list of ports to screen
  println("Serial Port Count = " + len); //print count of ports to screen
  int validPorts = 0;
  for ( i = 0; i < len; i++) 
       println("Testing port " + Serial.list()[i]); 
       logicBoard = new Serial(this, Serial.list()[i], 115200);      // Open 1st port in list 
       millisStart = millis(); 
       while ((millis() - millisStart) < 250) ; //wait a bit

       logicBoard.clear();    // empty buffer
       millisStart = millis();
       logicBoard.write('H'); // send handshake initiator character
       while ((millis() - millisStart) < 250) ; //collect some chars
         if(logicBoard.available() > 0)   //if we have a character 
                println("DATA AVAILABLE");
                char c = logicBoard.readChar(); //get the character 
                if(c == 'Q')    //if we got the handshake response character 
                     println("Serial Port used = " + Serial.list()[i]);
                     break;    //leave for loop 
                } else { logicBoard.stop(); }  //if no 'Q', stop port
      }else{println("NO DATA");}
  if(validPorts != 1)
    errorText = cp5.addTextlabel("sign")
    errorText = cp5.addTextlabel("error")
                    .setText("LOGIC BOARD NOT DETECTED")
  }else{ /* show my fantastic GUI, do great things (already works) */

just a idea for the program flow,

-a- pls print every char c what is received

-b- your if ( c == ‘Q’ ) should work like without the else,
why not do the


at / after the timeout.