Serial.read(); stops working after its used multiple times in row.

Hi so I have a problem with Serial.read() in my project. So what I have is Arduino Uno which is talking to really, telling it to turn ON or OFF. In order to control this I have created simple GUI in C# which is sending single char A for ON and B for OFF. But all the time after few sucesfull werial reads (the numbe varies and I suspect that the serial buffer is being filled) it will stop reading the serial inputs (even when I do the inputs manually in Arduino Serial Monitor). So could anyone help me with fihuring this one out? Here is my code.

#define RELAY1  7                        
void setup()

{    


Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       
   
  
}

  
  void loop()

{

  
   if( Serial.available() > 0)
  {
    Serial.flush();
    char c = Serial.read();
    if( isalpha(c))
    {
        menuOne(c);
    }
  }

   
}

   

void menuOne(char c)
{
  switch(c)
      {
        case 'A':
         digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
         Serial.flush();
//         Serial.println("Works: ON");
         break;  
      

      case 'B': 
       digitalWrite(RELAY1,HIGH);           // Turns OFF Relays 1
       Serial.flush();
//      Serial.println("Works: OFF");
   
        }
}

I have tested it with and without the Serial.flush(), but it sea,ms to have zero effect on the problem.

Tell us what role you think the Serial.flush is serving.

Well I thought that it would clear the buffer after I call it. Which is complete understanding of that function from my part. But non the less it does not change the problem I have, cause original code without the flush method in it have the same issue.

const byte RELAY1 = 7;                       
void setup()
{   
  Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       
}

void loop()
{
  if( Serial.available() > 0)
  {
    char c = (char)Serial.read();
    menuOne(c);
  }
}  

void menuOne(char c)
{
  switch(c)
  {
    case 'A':
      digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
    break; 
    case 'B':
      digitalWrite(RELAY1,HIGH);           // Turns OFF Relays 1
    break;    
  }
}

So, without the extraneous stuff? (Uncompiled, untested)

Maybe you've got a noise problem.

I have created simple GUI in C# which is sending single char A for ON and B for OFF.

You probably need to show THAT code, too. It is very unlikely that the Arduino quits listening to the serial port.

It is far more likely that the C# app quits talking.

to AWOL: It doesn't make any difference. An the number of times before it freezes is not even consistent. Out of 5 tests one it was 22 ON/OFF (meaning 44 reads) and then 2 times it was 7 ON/OFF and twice it was just 1 ON/OFF.

to PaulS: I am currently not even testing it with use of my c# code. I am using serial monitor in Arduino IDE to send input manually(which is how i determined that its not the c# to arduino connection problem in the first place). But by all means hers the code.

Here is the bit that tells it to turn ON:

serialPort1.PortName = getCom();
serialPort1.Open();
          

if (serialPort1.IsOpen)
 {
timer1.Start();               
 serialPort1.WriteLine("A");
 serialPort1.Close();
            }

and this one tells it to stop

 serialPort1.PortName = getCom();
 serialPort1.Open();
 serialPort1.WriteLine("B");
 timer1.Stop();
  serialPort1.Close();

Opening and closing the serial port resets the Arduino. That is NOT what you want to be doing. Open the port ONCE. Close the port ONCE.

What is with the crappy random indenting? I use C# every day, and none of my code looks like your crap.