user4855: I am trying to write one code that takes data from serial 
ie. 0x4E, 0x20, 0x--, 0x0D.
0x-- may be any hex in --
The user will input these hex one by one and raspi will take all of them in buffer.
Whenever 1st, 2nd and 4th data is matched, the 3rd one will be printed in serial 
ie.
Serial.print(3rd); //Decimal value
else Serial.print() // may be 00

I was trying with this code:
byte buffer[4];
int cnt = 0;

void setup()
{
 Serial.begin(9600);
 Serial.println("Start");
}

void loop(void)
{

 // read max 12 bytes
 if ((cnt <= 4) && Serial.available()) buffer[cnt++] = Serial.read();

 if (cnt == 4)  // all bytes read...
 {
   // assume the arrays are the same
   boolean same = true;

   // the ans array holds the right answer
   byte ans[]={0x4E,0x20,0x01,0x0D};

   for (int i=0; (i<= 4) && same; i++) same = (buffer[i] == ans[i]);

   if (same)
   {
     Serial.println("Yes");
   } else {
     Serial.println("Enter another number");
   }
   Serial.flush();
   cnt = 0;
 }
  //Serial.print("Done");
 // do something  
}

But I was not able to do it. Anyone have a suggestion?
Edit: I have tried this state machine in Arduino :
It accepts those data and according to it, it should bi color LED light. But not working properly. Not lighting up anything
//int currState;
//int reChk, data, confirm;
const int pingPin = 13;
int inPin = 12;

void setup()  
{
  Serial.begin(9600);
    // initialize digital pin 13 as an output.
  pinMode(32, OUTPUT);
  pinMode(34, OUTPUT); 

  digitalWrite(32, LOW);   
  digitalWrite(34, LOW);
  //currState = 0;
  //reChk = 0;
  //confirm = -1;


}

void loop() // run over and over
{
  static int currState = 0, reChk = 0, confirm = -1;
  int data;
  if(reChk == 0)
  {
    //ultrasonic();
    if(Serial.available())
    data = Serial.read();
    else
    currState = 5;
  }

  switch(currState)
  {

    case 0:
    reChk = 0;
    if(data == 78)
      currState = 1;
    else
      currState = 0;
    break;


    case 1:
    reChk = 0;
    if(data == 32)
    currState = 2;
    else
    {
      currState = 0;
      reChk = 1;
    }
    break;


    case 2:
    reChk = 0;
    switch(confirm)
    {
      case -1 :
      confirm = data;
      currState = 3;
      break;
      case 0 :
            digitalWrite(32, LOW);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      case 1 :
            digitalWrite(34, HIGH);   
            digitalWrite(32, LOW);
            delay(10);
      confirm = -1;
      currState = 0;
      break;
      case 2 :
            digitalWrite(32, HIGH);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      default :
      confirm = -1;
      reChk = 1;
      currState = 0;
    }


    case 3:
    reChk = 0;
    if(data == 20)
    {
      reChk = 1;
      currState = 2;
    }
    else
    {
      confirm = -1;
      currState = 0;
    }
    break;

    default:
    reChk = 0;
    currState = 0;
    confirm = -1;
  }
  delay(10);
}

void ultrasonic () //ultrasonic
{
      const int pingPin = 13;
      int inPin = 12;
      //raw duration in milliseconds, cm is the
      //converted amount into a distance
      long duration, cm;

      //initializing the pin states
      pinMode(pingPin, OUTPUT);
      //pinMode(greenLed, OUTPUT);
      //pinMode(redLed, OUTPUT); 

      //sending the signal, starting with LOW for a clean signal
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);

      //setting up the input pin, and receiving the duration in
      //microseconds for the sound to bounce off the object infront
      pinMode(inPin, INPUT);
      duration = pulseIn(inPin, HIGH);

      // convert the time into a distance
      cm = microsecondsToCentimeters(duration);

      if(cm > 50)
      {
      //printing the current readings to ther serial display
      Serial.print("5");
      }
      else
      {
      Serial.print("0");
      }
      //Serial.print("cm");
      //Serial.println();

      //checking if anything is within the safezone, if not, keep
      //green LED on if safezone violated, activate red LED instead
      //if (cm > safeZone)
      //{
      //  digitalWrite(greenLed, HIGH);
      //  digitalWrite(redLed, LOW);
      //}
      //else
      //{
      //  digitalWrite(redLed, HIGH);
      //  digitalWrite(greenLed, LOW);
      //}

delay(1000);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
   return microseconds / 29 / 2;
}


user-1: It's unclear how the users input the hex numbers.
If i assume a simple serially attached terminal the user doesn't enter hex numbers but characters.
As an example when the user types 0, x, 4, E the user has only typed one hex number but already four bytes or characters. Instead the user has to type N, , whatever letter, Enter. (Depending on the OS or terminal you will get "CR", "LF" or "CR+LF" when you press enter)
I'm not familiar with C, but your code seems to have also a coding flaw: The 3rd byte will be checked for equality too. You would need something like:
boolean same = true;

if (buffer[0] == ans[0]) { same = false }
if (buffer[1] == ans[1]) { same = false }
if (buffer[3] == ans[3]) { same = false }

Only three tests not four.

4855: <p>I am trying to write one code that takes data from serial 
ie. <code>0x4E</code>, <code>0x20</code>, <code>0x--</code>, <code>0x0D</code>.</p>

<p><code>0x--</code> may be any hex in <code>--</code></p>

<p>The user will input these hex one by one and raspi will take all of them in buffer.
Whenever 1st, 2nd and 4th data is matched, the 3rd one will be printed in serial 
ie.</p>

<pre><code>Serial.print(3rd); //Decimal value
else Serial.print() // may be 00
</code></pre>

<p>I was trying with this code:</p>

<pre><code>byte buffer[4];
int cnt = 0;

void setup()
{
 Serial.begin(9600);
 Serial.println("Start");
}

void loop(void)
{

 // read max 12 bytes
 if ((cnt &lt;= 4) &amp;&amp; Serial.available()) buffer[cnt++] = Serial.read();

 if (cnt == 4)  // all bytes read...
 {
   // assume the arrays are the same
   boolean same = true;

   // the ans array holds the right answer
   byte ans[]={0x4E,0x20,0x01,0x0D};

   for (int i=0; (i&lt;= 4) &amp;&amp; same; i++) same = (buffer[i] == ans[i]);

   if (same)
   {
     Serial.println("Yes");
   } else {
     Serial.println("Enter another number");
   }
   Serial.flush();
   cnt = 0;
 }
  //Serial.print("Done");
 // do something  
}
</code></pre>

<p>But I was not able to do it. Anyone have a suggestion?</p>

<p><strong>Edit: I have tried this state machine in Arduino :</strong>
It accepts those data and according to it, it should bi color LED light. But not working properly. Not lighting up anything</p>

<pre><code>//int currState;
//int reChk, data, confirm;
const int pingPin = 13;
int inPin = 12;

void setup()  
{
  Serial.begin(9600);
    // initialize digital pin 13 as an output.
  pinMode(32, OUTPUT);
  pinMode(34, OUTPUT); 

  digitalWrite(32, LOW);   
  digitalWrite(34, LOW);
  //currState = 0;
  //reChk = 0;
  //confirm = -1;


}

void loop() // run over and over
{
  static int currState = 0, reChk = 0, confirm = -1;
  int data;
  if(reChk == 0)
  {
    //ultrasonic();
    if(Serial.available())
    data = Serial.read();
    else
    currState = 5;
  }

  switch(currState)
  {

    case 0:
    reChk = 0;
    if(data == 78)
      currState = 1;
    else
      currState = 0;
    break;


    case 1:
    reChk = 0;
    if(data == 32)
    currState = 2;
    else
    {
      currState = 0;
      reChk = 1;
    }
    break;


    case 2:
    reChk = 0;
    switch(confirm)
    {
      case -1 :
      confirm = data;
      currState = 3;
      break;
      case 0 :
            digitalWrite(32, LOW);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      case 1 :
            digitalWrite(34, HIGH);   
            digitalWrite(32, LOW);
            delay(10);
      confirm = -1;
      currState = 0;
      break;
      case 2 :
            digitalWrite(32, HIGH);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      default :
      confirm = -1;
      reChk = 1;
      currState = 0;
    }


    case 3:
    reChk = 0;
    if(data == 20)
    {
      reChk = 1;
      currState = 2;
    }
    else
    {
      confirm = -1;
      currState = 0;
    }
    break;

    default:
    reChk = 0;
    currState = 0;
    confirm = -1;
  }
  delay(10);
}

void ultrasonic () //ultrasonic
{
      const int pingPin = 13;
      int inPin = 12;
      //raw duration in milliseconds, cm is the
      //converted amount into a distance
      long duration, cm;

      //initializing the pin states
      pinMode(pingPin, OUTPUT);
      //pinMode(greenLed, OUTPUT);
      //pinMode(redLed, OUTPUT); 

      //sending the signal, starting with LOW for a clean signal
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);

      //setting up the input pin, and receiving the duration in
      //microseconds for the sound to bounce off the object infront
      pinMode(inPin, INPUT);
      duration = pulseIn(inPin, HIGH);

      // convert the time into a distance
      cm = microsecondsToCentimeters(duration);

      if(cm &gt; 50)
      {
      //printing the current readings to ther serial display
      Serial.print("5");
      }
      else
      {
      Serial.print("0");
      }
      //Serial.print("cm");
      //Serial.println();

      //checking if anything is within the safezone, if not, keep
      //green LED on if safezone violated, activate red LED instead
      //if (cm &gt; safeZone)
      //{
      //  digitalWrite(greenLed, HIGH);
      //  digitalWrite(redLed, LOW);
      //}
      //else
      //{
      //  digitalWrite(redLed, HIGH);
      //  digitalWrite(greenLed, LOW);
      //}

delay(1000);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
   return microseconds / 29 / 2;
}
</code></pre>
 -1: <p>It's unclear how the users input the hex numbers.</p>

<p>If i assume a simple serially attached terminal the user doesn't enter hex numbers but characters.</p>

<p>As an example when the user types <code>0</code>, <code>x</code>, <code>4</code>, <code>E</code> the user has only typed one hex number but already four bytes or characters. Instead the user has to type <code>N</code>, <code></code>, <code>whatever letter</code>, <code>Enter</code>. (Depending on the OS or terminal you will get "CR", "LF" or "CR+LF" when you press enter)</p>

<p>I'm not familiar with C, but your code seems to have also a coding flaw: The 3rd byte will be checked for equality too. You would need something like:</p>

<pre><code>boolean same = true;

if (buffer[0] == ans[0]) { same = false }
if (buffer[1] == ans[1]) { same = false }
if (buffer[3] == ans[3]) { same = false }
</code></pre>

<p>Only three tests not four.</p>
