C# and arduino communication

I'm working on a project in C#. The program is a control program for an arduino. I've written some code to write to the serial of the arduino and have programmed the arduino to respond with a standard message (an acknoledge if you will) in this case "5". After sending information to the arduino, the arduino responds with a "5" but keeps sending a "5".

Any idea as to why this happens?

port config:

    private void Ports_SelectedIndexChanged(object sender, EventArgs e)
        _serialPort.PortName = ArrayComPortsNames[Ports.SelectedIndex];//Set your board COM
        _serialPort.BaudRate = (int)Baudrate.Items[Baudrate.SelectedIndex];
        _serialPort.Parity = Parity.None;
        _serialPort.DataBits = 8;
        _serialPort.StopBits = StopBits.One;
        if (!_serialPort.IsOpen)
        _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_serialPort);
    private void sp_serialPort(object sender, SerialDataReceivedEventArgs e)
        //Write the serial port data to the console.

C# code:

    private void buttonKeyboard_KeyDown(object sender, KeyEventArgs e)
        switch (e.KeyData)
            case Keys.Z:
            case Keys.Q:
            case Keys.S:
            case Keys.D:


    private void PortWrite(string message)

arduino code:

void setup() {


void loop() {

Serial.available tells you that there's something to read. You're not reading it, so it's perpetually available and so the Arduino keeps sending five.


I wish to believe that the OP would not do that mistake if he had possessed the following meaning of Serial.available() programming language construct.

When a valid data item (8-bit ASCII code or binary code) arrives at the Receiver Register of the UNO’s MCU, the data byte immediately enters into a FIFO type buffer. If we don’t bring out the data byte from the FIFO into a user variable by executing a Serial.read() instruction, it will remain there. Before we perform a Serial.read() operation on the FIFO, we naturally wish to check if the FIFO has actually accumulated any data item. The execution of the
byte n = Serial.available(); instruction assigns an integer value to the variable n, which is equal to the number of ‘data items’ currently present in the FIFO. Therefore, the logical codes would like:

void loop()
   byte n = Serial.available();
   if (n != 0)       //FIFO has one data item provided we have read the previously arrived data item 
       byte x1 = Serial.read();  //there is no more data item in the FIFO
       Serial.println("5");          //send 5 and the non printable 'new line character'

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(','); // comma separator
Serial.println('>'); // end marker