Visual C# and Arduino, Arduino freezes after receiving serial data

const int MAX_LEN = 20;

char buffer[MAX_LEN+1]; // allow space for the terminating null
int length = 0;
...

if ( c == '\n' || c == '\r')
{
 // end-of-line received
 // process content of buffer, if any
 if(length > 0)
 {
    handle(buffer);
    length = 0;
 }
}
else
{
 // not end-of-line
 if(length < MAX_LEN)
 {
   buffer[length++] = c;
   buffer[length] = '\0';
 }
}




You'll notice I renamed 'i' to 'length'; it's common to use single letter variables as local loop counters and your global 'i' would clash with any local variables of the same name.

Thank you, I will do that.