checking data received

hello,
i am using matlab to send byte by byte data to arduino (for example 12) and i need check its value in order to proceed but nothing happens. so how can i check these values.below is the code used for arduino
thanks

void loop() {
int inchar;

if(Serial.available()>0)
{

inchar = Serial.read();
Serial.print(inchar,HEX);
if(inchar=='12')
{
}
}

How many characters does it take to send 12? If you guessed 2, you are right. How many bytes does it take to send 12? 1 byte. So how is the data sent? And '12', look at your keyboard and find the 12 character. Single quotes for single characters, double quotes for strings.
Robin2's serial input basics will help you understand serial communication.

Well, 12 will come in, from the Serial Monitor for example, as '1', '2' and maybe CR and/or LF if you have that selected at the bottom of the serial monitor.

So you need to do this:

         if(Serial.available()>1 ) // got 2 bytes at least?
         {
          inchar10 = Serial.read() - 0x30; // read and convert to ASCII, see www.asciitable.com
          if (inchar10 >= 0 && inchar10 <= 9){  // got a 0 to 9? Yes, read next number
          inchar1 = Serial.read() - 0x30; // read and convert to ASCII, see www.asciitable.com
          Serial.print(inchar10,HEX);
          Serial.print (" ");
          Serial.print(inchar1,HEX);
          Serial.print (" ");
          data = inchar10 *10 + inchar1;
          Serial.print (data, HEX);
         }
          if(inchar==12)
          {
Serial.println (" got my number!");

            }
         }

Something along those lines.

hello groundFungus,
i checked the thread but it didnot help as it deals with characters while i need to deal with bytes if there is any further suggestions it would be much appreciated.

hello CrossRoads,
the code didnot work, any further suggestions would be much appreciated.

A char is eight bits and signed, a byte is eight bits and unsigned.

If you've only got seven bit data, either should work equally well.

Have a look at Robin2's serial handling basics tutorial thread.

Try using loop instead of if statement.

String data="";
while(Serial.available())
{
  data=data+(char)Serial.read();
}
if(data=="12")
{
  
}

samarhefina:
if(inchar=='12')
{
}

I am not sure if this portion will work. 'inchar' is an integet. And you are trying to compare it with '12'. when you try to compare integer with character, it compares the acii code. but 12 is not a character and doesnt have an ascii code.

So try storing converting each byte to corresponding character and store it to a string. and at the end of the transmission compare the string.

logans:
Try using loop instead of if statement.

String data="";

while(Serial.available())
{
  data=data+(char)Serial.read();
}
if(data=="12")
{
 
}




I am not sure if this portion will work. 'inchar' is an integet. And you are trying to compare it with '12'. when you try to compare integer with character, it compares the acii code. but 12 is not a character and doesnt have an ascii code.

int x = '12';it may surprise you, but that's perfectly legal.

AWOL:
int x = '12';it may surprise you, but that's perfectly legal.

Sorry, i didn't know. Never used c++. Only java...
But i didn't understand what is that value it returns..