clearing serial buffer

hi there forum,

im working on a midi project. and from the incoming data i really don't all the data wich is coming in just the first two bytes. therefore i would like to no if there is any simple piece of code wich i can apply to clear the serial buffer of the rest of the message. im programming on a Attiny2313 so i'm really looking for an optimalisation piece, (since it has a very limmited programming memmory.

bummer that the flush command doesn't work anymore

While (Serial.read() > 0)   /* keep looping */  ;

therefore i would like to no if there is any simple piece of code wich i can apply to clear the serial buffer of the rest of the message.

Serial data arrives slowly. The loop() function might iterate 100,000 times or more between the first two bytes. So, when, exactly, do you want to dump random amounts of unread data. You need to be certain that you do this only after all the bytes that make up a packet arrive, but before the next packet arrives.

while(Serial.available() > 0)
   Serial.read();

will dump whatever is in the buffer (plus whatever arrives while the data is being dumped). Just when will you call that code, though?

If you think that I'm hinting that you actually need to pay attention to the serial data, so you know when a packet ends, you are correct.

Serial data arrives slowly. The loop() function might iterate 100,000 times or more between the first two bytes. So, when, exactly, do you want to dump random amounts of unread data. You need to be certain that you do this only after all the bytes that make up a packet arrive, but before the next packet arrives.

you make a valid point but in midi the messagebytes of the same command will follow without delay.
the application will read every message yet only the first 2 bytes , not the third byte.
if you want to here is my code:

#define note_on 0x90
#define note_off 0x80
#define mask 0xF0


int toon[12]= {1,12,2,11,3,4,10,5,9,6,8,7};
byte commandByte = 0;
byte noteByte = 0;

void setup() {
  for (char i =0; i<=11 ; i++){
    pinMode(toon[i], OUTPUT);
  }
  Serial.begin(31250);
}

void loop() { 
  if (Serial.available()) {
    commandByte = Serial.read();
    noteByte = Serial.read();
    noteByte = noteByte%12;
    if (commandByte & mask == note_on){
      digitalWrite(toon[noteByte],HIGH);
    }
    else if (commandByte & mask == note_off){
      digitalWrite(toon[noteByte],LOW);
    }
    while (Serial.read()>0)
  }
}

don’t pay attention to the toon array too much, these are pin-outs for the application.

the code reads the midi messages note-on and note-off for every midi note on every midi channel (<-- thats why the mask is there) then for each C the same pinout goes high as for each D . . . etc.

you make a valid point but in midi the messagebytes of the same command will follow without delay.

No, they won't. Serial data transmission takes time. If you don't believe that, I can't help you.

the application will read every message yet only the first 2 bytes , not the third byte.

No. That code is crap.

  if (Serial.available()) {
    commandByte = Serial.read();
    noteByte = Serial.read();

If there is at least one byte available, read two bytes. Rubbish.

you make a valid point but in midi the messagebytes of the same command will follow without delay

MIDI bytes will arrive about 1/3120 of a second apart. That's about enough time for the Arduino to execute about 5100 instructions

so this ?

 if (Serial.available()) {
    commandByte = Serial.read();
  while (!serial.available()){}
    noteByte = Serial.read();
  while (!serial.available()){}
    wasteByte = Serial.read();
 ->rest of the stuff

Just to prove to you that you need to rewrite. Load and compile this (use whatever baud rate you want). Then run the Serial Monitor, and type in 123, hit send (or enter), and look at the output.

byte  x;
byte y;

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Serial.available()) {
    x = Serial.read();
    y = Serial.read();
    Serial.print(x);
    Serial.print(" ");
    Serial.println(y);
  }
}

That last code you posted is not good at all. As someone already said, you NEED to know what all is in the packet in order to decide when to keep and when to discard bytes. So you either need to know the value of the last "waste byte", or the count of the bytes in a packet. Oh, and you are not required to assign the result of Serial.read() to a variable.

Check out Nick Gammon's excellent Serial tutorial at http://www.gammon.com.au/forum/?id=11425