Help with Serial.read

I am going to be receiving commands from a PC. One of the commands will come in as one byte as 0xCA. I am currently simulating PC commands using the Serial monitor and I can't seem to get it to work to read in 0xCA.

Here is my code right now:

      if (Serial.read() == 0xCA) {
        printDistance();
      }

Have you tried to capture the read in a byte first and do the compare thereafter? See http://www.arduino.cc/en/Serial/Read for some sample code how to test what is read by the arduino.

Q: How do you send the 0xCA to the Arduino? Is this from a application as it is not a standard keyboard character.

Yes I've tried that sample code and it's not helpful for my use. I am getting 0xCA from a separate application (that I have not written btw), not the keyboard.

Your problem is that your not performing serial commands in the correct order. You must wait for serial data to be avalible before you read it. Check out this reference and it's example code for proper method to read a byte:

http://arduino.cc/en/Serial/Available

Lefty

Sorry, I am using Serial.available. Should have put up a bigger code block.

  if (Serial.available() > 0) {
            // read the incoming byte:
      int newbyte = Serial.read();
      if (Serial.read() == 0xCA) {
        Serial.println(newbyte, DEC);
        Serial.println(newbyte, BIN);
        Serial.println(newbyte, HEX);
        Serial.println(newbyte, BYTE);
        printDistance_all();
      }            
  }

I added some extra lines to see what I was getting. It will only read keyboard chars as bytes, I need it to read in 0xCA. I can't seem to find any documentation on when it receives a byte in hex form, only keyboard chars.

int newbyte = Serial.read();
      if (Serial.read() == 0xCA) {

Did you mean:

int newbyte = Serial.read();
      if (newbyte == 0xCA) {

?

Dude, you have two Serial.read(), this means that you will consume two bytes of input from the wire. So, if you expect to receive only one byte of data having two Serial.read() will inevitably lead to problems.

I wrote a detailed blog post on Arduino + Serial communication here: http://www.varesano.net/blog/fabio/serial-communication-arduino-and-processing-simple-examples-and-arduino-based-gamepad-int

The hex data probably is received as a four character string (with maybe a charrage return or such at the end. The below test code is a simple string capture setup that might be of use. You will need to develop a method to convert the two hex characters into the desired form, such as decimal. There may be easier ways to do the conversion.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
      Serial.begin(9600);
        Serial.println("serial test 0021"); // so I can keep track of what is loaded
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();
        readString += c;}
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      
      readString="";
      } 
   }