Send string from C# to Arduino

Hi everyone, i'm doing a project about dc motor control by arduino anh c# winform.I try to send some strings to arduino to control direction and set some parameters. This is how i do it:

Code c#:

private void button3_Click(object sender, EventArgs e)
        {
                      
            if (serialPort1.IsOpen)           
                serialPort1.WriteLine("forward");     
               
        }

private void button4_Click(object sender, EventArgs e)
        {
          
            if (serialPort1.IsOpen)
           
                serialPort1.WriteLine("backward");    

        }

private void button5_Click(object sender, EventArgs e)
        {
           
            if (serialPort1.IsOpen)

                 serialPort1.WriteLine("stop");
               
        }

   private void textBox2_TextChanged(object sender, EventArgs e)
        {
           
            if (serialPort1.IsOpen)
                serialPort1.WriteLine("vs_kp"+textBox2.Text);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            
            if (serialPort1.IsOpen)
                serialPort1.WriteLine("vs_ki" + textBox3.Text);
        }

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setspeed = textBox1.Text;
                if (serialPort1.IsOpen)
                {

                    serialPort1.WriteLine("vs_set_speed"+setspeed);

                }
            }
        }

Arduino code:

const byte pin_fwd = 4; //for H-bridge: run motor forward
const byte pin_bwd = 5; //for H-bridge: run motor backward
boolean stringComplete = false;

void setup() {
  
  pinMode(pin_fwd,OUTPUT);
  pinMode(pin_bwd,OUTPUT);
 Serial.begin(9600);
while (!Serial) 
  {};
}
void loop() {
  serialEvent();
  if (stringComplete) {
    // clear the string when COM receiving is completed
    mySt = "";  //note: in code below, mySt will not become blank, mySt is blank until '\n' is received
    stringComplete = false;
  }

  if (mySt.substring(0,7) == "forward"){
    digitalWrite(pin_fwd,1);      //run motor run forward
    digitalWrite(pin_bwd,0);

  }

   if (mySt.substring(0,8) == "backward"){
    digitalWrite(pin_fwd,0);      //run motor run bacward
    digitalWrite(pin_bwd,1);
    
  }
  
  if (mySt.substring(0,4) == "stop"){
    digitalWrite(pin_fwd,0);
    digitalWrite(pin_bwd,0);      //stop motor
    
  }
    //get string after set_speed
  
  if (mySt.substring(0,5) == "vs_kp"){
    kp = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_kp
  }
  if (mySt.substring(0,5) == "vs_ki"){
    ki = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_ki
  }

  }
  if (mySt.substring(0,12) == "vs_set_speed"){
    set_speed = mySt.substring(12,mySt.length()).toFloat();  //get string after set_speed  
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    if (inChar != '\n') {
      mySt += inChar;
    }
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
      break;
    }
    
  }
}

It works on the Arduino UNO, but not on the Leonardo.

I tried to make it simple by control only 2 direction (forward,backward) by 2 button in c# and 2 'if' in arduino code.When i press the button, the led still blink to show that the serial data is sent, but it still didn't work, none signal was sent to the pin out (4 and 5)

Can anyone tell me what i'm doing wrong here,it will be helpful for me. Thanks

From the SerialEvent reference:

Notes and Warnings

serialEvent() doesn’t work on the Leonardo, Micro, or Yún.

Maybe try the methods in the serial input basics tutorial.

The posted code will not compile.

Thanks groundFungus for the share, i will try it.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking 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. It is what I use for Arduino to Arduino and Arduino to PC communication.

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(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R