problem reading serial data


i am new to Arduino and i have a problem reading some serial data with the arduino hardware.

The data is send from a pc using the com port at 9600 baud (9600 8N1). Example data:

CA 00 00 03 00 05 7E 04 C9 CB 00 CB 01 CC B2 8D

Interesting for me are the bytes after 7E so i started with the following:

int redPIN = 9;

byte incomingByte;

void setup() {
void loop() {
  if(Serial.available() >0) {
      incomingByte =;
      switch(incomingByte) {
        case 0x7E:

int startByte() {
  byte value;
  value =;
  if(value==0x04) {

int blink()
  analogWrite(redPIN, 255);
  analogWrite(redPIN, 0);

Now it seems that it recognizes the 7E and startByte() will be called but after that i can´t get the additional bytes read like the value (0x04) in the startByte() function. blink() won´t be called.
The blink() function is only for debugging so don´t wonder g

Any ideas where the problem is?

best regards

the inside your function is being called when there is no data in the buffer, therefore returning junk (the function is non-blocking). Even if your sending computer is fast, and the characters are sent back to back, the arduino is so much faster than the serial communication speed that you are inside your function call well before the start bit of that second byte has started down the wire.

you need a test like this:

while (!serial.available())
   { ; }

before the (or any, for that matter).


yes, thats it.

thank you kg4wsv