Help with Serial parsing for GUI.

i have created a GUI in VS to control LED's using buttons. This is programmed in C#.
I need help with terminating the bytes as I am experiencing delays when pressing the buttons.
Also I have a slider to fade the LED's on and off and this is disrupting the buttons due to the data not being parsed.

Am I sending a new line correctly, and also I am not sure where to put the if(Serial.read() == 'a'), whether it should be in the loop or in the processData function.

const unsigned int MAX_INPUT = 50;
/*
 New line is a terminating character
 When received, the buffer is sent off to another function for processing
 The buffer is then emptied
 */

void setup()
{
  Serial.begin(9600);

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
}

void processData(const char * data)
{
  if (Serial.read() == 'a')
  {
    Serial.print("LED's are On - Red.");

    analogWrite(9, 255);
    analogWrite(10, 0);
    analogWrite(11, 0);

    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
  }
}

void loop()
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
  {
    int inByte = Serial.read ();

    switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      processData(input_line);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch
  } //end of data
}

and the Visual Studio code is:

      private void redButton_Click(object sender, EventArgs e)
        {
            serialPort1.Write("a, \n");
        }
private void redTrackbar_Scroll(object sender, EventArgs e)
        {
            redTrackbar.Minimum = 0;
            redTrackbar.Maximum = 255;
            redTrackbar.TickFrequency = 50;

            byte pwm;
            pwm = Convert.ToByte(redTrackbar.Value);
            data[0] = pwm;
            serialPort1.Write(data,0,1);
            textBox7.Text = pwm.ToString();
        }

The first thing "processData" does is read a character.
How do you know there's something there to read?

The question is why ProcessData() does a read at all. You pass it data that, in theory, is what it is supposed to process.

Ah, I have taken out a bit of code..

Serial.print(data);

As you've (hopefully) learned, printing anonymous data is stupid.

Serial.print("The data is: [");
Serial.print(data);
Serial.println("]");

will tell you a LOT more.

I think I am basically saying
if ('a \n' is == 'a')

Clearly not, since ONE Serial.read() can not return 'a \n';

Why are you sending a space and a carriage return, anyway?

Ah, I have taken out a bit of code..

So, post the code you're currently working on

So, what you're saying is, the code that reads characters that may not have arrived yet, is still in there?

Now I am having trouble handling the data which has been passed. As stated above, I need a condition which tells me which bit of code to run depending on the data sent. Is the if statement the best option? I can't get it to work by doing this.

Yes, an if statement is the ONLY option.

Post the code as it is now, with the revised print statements, and show the output that you get (in the PC application). You are reading serial data there, right?

If not, set the PC app aside for now. Make the Arduino do what it is supposed to do based on input from the Serial Monitor application where you can see what you send/it gets.

This is my code from the PC, which sends the data and the newline character.

Why are you sending a new line? It contributes NOTHING.

Everytime I click my redLED button on the PC, the PC serial monitor shows an a every time the button is clicked.

If the Serial Monitor application has the COM port open, your application is not using the com port, and is not talking to the Arduino. If you are referring to some other application showing an a, then you need to be clear about what that application is.

Is the Arduino using the data that is passed to ProcessData() now, or is it still ignoring that, and reading from the serial port and using that data?

Now that I have stopped sending a \n, my visual studio serial monitor is not showing anything now when I press my redLED button.

Now that I've changed my code, nothing happens, but I'm not going to show you all of my PC code or my Arduino code. I still want you to help me debug it.

Is that a reasonable summary of your position?

if(Serial.read() == 'a')

Here, you're reading data that may not have arrived yet.
I think I may have mentioned this.
If it appears to work, it's only because of the debug print immediately before it.

ZeuSou7x:
what can I do to wait for the data to arrive.

You can do what you're already doing in "loop ()"

Why are you reading the serial port in the processData function at all? I'd expect that routine to be making decisions based on the data string it was passed; printing it and then ignoring it seems odd.

Change this:

  if(Serial.read() == 'a')

to

  if(strcmp(data, "a") == 0)

It is only connecting to my Arduino if I send a \n along with 'a', do you have any idea what may be causing this?

Because you only call ProcessData() when a \n is received. Since you are using one letter to mean anything, that is useless. Read a letter, process it.

Or, send the \n and leave the Arduino code alone.

Your choice.