Change an arduino variable in for loop from C#

Hi guys,
I am having a project which is control a stepper motor by arduino with C# GUI and I have no idea how to change the "n" variable of my for loop from a textbox in C#. For example, i will type a value of n whenever i want to change to angle of stepper motor.
Here my arduino code:

int i;
int n;
void setup() {
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(9,HIGH);
  delay(10);
}

int received = 0;
void loop() {
  if (Serial.available() == 1)
  {
    received = Serial.read();
    switch (received)
    {
      case 50:     // Received ASCII 0
       digitalWrite(2, HIGH);

        break;   //Move on
      case 51:     // Received ASCII 1
        digitalWrite(2, LOW);
 for(i=0; i<n;i++)
    {
    delayMicroseconds(300);
    digitalWrite(10, LOW);
    delayMicroseconds(600);
    digitalWrite(10, HIGH);
    delayMicroseconds(300);
    }
    delay(1000);
        break;   //Move on
    }
  }
}

Here is my C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace Serial_send
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           string[] ports = SerialPort.GetPortNames();
           foreach (string port in ports)
           {
               cbPORT.Items.Add(port);
           }
       }

       private void button2_Click(object sender, EventArgs e)
       {
           string n =  tbgoc.Text.ToString();
           string t = cbPORT.Text.ToString();
           string s = tbDATA.Text.ToString();
           serial(t, s);
       }

       void serial(string Port_name, string data_send)
       {
           SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
           sp.Open();

           sp.Write(data_send);
           sp.Close();
       }
   }
}

Thank u for your time to provide any help and sorry for my bad english.

regards

Hello,

One thing I noticed:

case 50:     // Received ASCII 0

this is wrong, ASCII 0 is 48, 1 is 49.

You could also write it like this:

case '0':     // Received ASCII 0

guix:
Hello,

One thing I noticed:

case 50:     // Received ASCII 0

this is wrong, ASCII 0 is 48, 1 is 49.

You could also write it like this:

case '0':     // Received ASCII 0

I changed that into 2 and 3 on my keyboard and thanks for your second option.

You should read this: Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

Have a look at the examples in Serial Input Basics

...R

           SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);
           sp.Open();

           sp.Write(data_send);
           sp.Close();

Open the serial port, resetting the Arduino. While it is rebooting, send it some data, then close the serial port.

And wonder why sending serial data to the Arduino doesn't seem to work.

It seems bizarre to me to have a button on the form that you have to click to populate the list of serial ports available, while not having one to open the serial port independent from writing to the port.

PaulS:

           SerialPort sp = new SerialPort(Port_name, 9600, Parity.None, 8, StopBits.One);

sp.Open();

sp.Write(data_send);
          sp.Close();



Open the serial port, resetting the Arduino. While it is rebooting, send it some data, then close the serial port.

And wonder why sending serial data to the Arduino doesn't seem to work.

It seems bizarre to me to have a button on the form that you have to click to populate the list of serial ports available, while not having one to open the serial port independent from writing to the port.

thanks for your help. Actually, i use a tutorial on youtube
link: https://www.youtube.com/watch?v=dFRa6KEDJ3I
i can send the n variable to arduino now thanks to this tutorial
link: Arduino Lesson 6: Reading From the Serial Port | Technology Tutorials
but i'm still having some trouble in controlling my stepper.

Robin2:
Have a look at the examples in Serial Input Basics

...R

thank you. I'm currently reading it.

As a follow-up to @PaulS's comment you may be interested in the system in this Python - Arduino demo. The same ideas can be used in any language.

...R