Serial speed and syncronization problem

here it goes
you have to quickly send a string like
M55555555555555555555555555555555555
or similar length

at 38400 works perfectly
if I try at 115200 I get errors

int totCols = 5;
int totRows = 7;
//set the string that will be received throughSerial
/*the string should start with an 'M' so that the code knows when it is starting
rows and columns notation
M      1000000
      0100000
      0010000
      0001000
      0000100      
continous string notation:  M10000000100000001000000010000000100
*/
char Matrix_string [5][7]; //putting [totCols][totRows] yelds a compiler error

//use this routine to prevent the fact that Serial.read() doesn't wait for a byte in the buffer
char Serial_getSecureByte() {
    while (Serial.available() == 0) {
      // do nothing
    }
    return Serial.read();
}

void Matrix_serialRead () {
    for (int c=0; c < totCols; c++) {
        for (int r=0; r < totRows; r++) {
                 //read a char from Serial - use function to be sure that the byte is there
              //char serIn = Serial_getSecureByte();
                 //convert it to an int since serIn will be 49(ASCII value of num 1)
                 //method 2 would be subtracting 48 - TODO test the fastest
              //int b = atoi(&serIn);
                 //store it in the appropriate slot in the string
              Matrix_string[c][r] = Serial_getSecureByte();//b;
                 //print feedback
              Serial.print(Matrix_string[c][r]);
                Serial.print(" "); 
        }
    }
    Serial.println();
   //prepare the serial buffer for the next string
   Serial.flush();
}

//output the string 
void Matrix_display() {
  //Serial.println("display: ");
   for (int c=0; c < totCols; c++) {
        for (int r=0; r < totRows; r++) {                 
               Serial.print(Matrix_string[c][r]);
               Serial.print(" ");
        }
   }
   Serial.println();
}


void setup() {
  Serial.begin(115200); //can push it further up!!! why??
}

void loop () {
    //*check for serial data
    while (Serial.available()) {
        byte s = Serial.read();
        if(s == 'M'){;
          //start of the protocol Matrix
          Matrix_serialRead ();
        }
    }
    //continously display it all
    //Matrix_display();    
}

thanks in advance :slight_smile: