Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody, I'm working C# and Arduino at now. I want to send two or more data from textboxes in C# form app. to serial monitor. But my serial port read only first number and other numbers was not seen. I searched some to clear first name after when serial port read. But I didn't find anything for my aim. My c# code :

C#
private void btnSend_Click(object sender, EventArgs e)
        {

            if (serialPort1.IsOpen)
            {
                int MyInt = Convert.ToInt32(textBox1.Text);
                byte[] b = BitConverter.GetBytes(MyInt);
                serialPort1.Write(b, 0, 4);
                if (MyInt == 3)
                {
                    Form DataSend = new Form();
                    DataSend.BackColor = Color.Empty;
                    DataSend.Text = "DATA SEND";
                    DataSend.Show(); // ya da ShowDialog();
                    DataSend.Cursor = Cursors.Hand;
                    DataSend.StartPosition = FormStartPosition.CenterScreen;
                    Button btn = new Button();
                    btn.Text = "Send Again";
                    btn.Name = "btnSendAgain";
                    btn.Location = new Point(100,50);
                    btn.Click += btn_Click;
                    TextBox txbox = new TextBox();
                    txbox.Text = "";
                    txbox.Name = "txtNumber2";
                    txbox.Location = new Point(100,20);
                    DataSend.Controls.Add(btn);
                    DataSend.Controls.Add(txbox);
                }
            }
            else
            {
                MessageBox.Show("Please check your connction maybe serial port is not connected");
            }

        }
 void btn_Click(object sender, EventArgs e)
        {
            
                
                int MyInt = Convert.ToInt32(textBox1.Text);
                byte[] b = BitConverter.GetBytes(MyInt);
                serialPort1.Write(b, 0, 4);
            
            
        }


My arduino code:

<pre>int led = 12;
int num1;
int data[4];

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}
void loop()
{
  if (Serial.available())
  {
    num1 = Serial.read();
    for (int i = 0; i < 2; i++)
    {
      data[i] = num1;
    }
    switch (num1)
    {
      case 1:
        openCloseThree();
        break;
      case 2:
        openCloseTwice();
        break;
      case 3:
         openCloseIwanted();
        break;
      default:
        break;
    }
  }
}

void openClose()
{
  for (int i = 0; i < 1; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }
}
void openCloseTwice()
{
  for (int i = 0; i < 2; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }
}
void openCloseThree()
{

  for (int i = 0; i < 3; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }
}
void openCloseIwanted()
{

  for (int i = 0; i < num1; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }

}


What I have tried:

Actually I want to that: When I send 3 using C#, another form is open and again I write any number, my led is open close until last number that I write.
Posted
Updated 9-Mar-17 22:33pm

It is not clear what do you really want to do. Anyway, please note:
  • In your C# code, if you enter, for instance, "1" in your TextBox then four bytes are sent to the serial line: three 0s and a single 1 (the 0s won't be handled by your Arduino code).
  • In your Arduino code you didn't check if Serial.read() returns -1 (i.e. no data available).
 
Share this answer
 
Comments
goksurahsan 9-Mar-17 6:22am    
Actually I want to send two data from c# to Arduino
CPallini 9-Mar-17 6:46am    
Two bytes or two integers? You know a 32 bit integer takes 4 bytes...
goksurahsan 9-Mar-17 6:56am    
Two integer.But when I send for example 3 next 5, I want to clear 3 in memory of Arduino.
I used Serial.flush(); method but it didn't work in my program.
CPallini 9-Mar-17 7:33am    
'clear from memory' is done overwriting the memory you used with the new value.
In your code, you are assigning the same value (num1) to data[0], data[1] and data[2]. That looks meaningless.
My Arduino part:

<pre>int number[3];
int n;
int i = 0;
int led = 12;
int led2=7;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    // read the incoming byte:
    n = Serial.read();
    if (i < 3)
    {
      number[i] = n;
      switch (number[0])
      {
        case 1:
          openClose();
          break;
        case 2:
          openCloseTwice();
          break;
        case 3:
          if (i == 2)
          {
            for (int j = 0; j < number[1]; j++)
            {
              delay(500);
              digitalWrite(led, HIGH);
              delay(500);
              digitalWrite(led, LOW);
            }
            delay(500);
            for (int k = 0; k < number[2]; k++)
            {
              delay(500);
              digitalWrite(led2, HIGH);
              delay(500);
              digitalWrite(led2, LOW);
            }
          }
          break;
        default:
          break;
      }
      i++;
    }
    else
    {
      i = 0;
    }

  }
}
void openClose()
{
  for (int i = 0; i < 1; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }
}
void openCloseTwice()
{
  for (int i = 0; i < 2; i++)
  {
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
  }
}


My c# part:

if (serialPort1.IsOpen)
            {
                int MyInt = Convert.ToInt32(textBox1.Text);
                byte[] b = BitConverter.GetBytes(MyInt);
                serialPort1.Write(b, 0, 1);

                int MyInt2 = Convert.ToInt32(textBox2.Text);
                byte[] z = BitConverter.GetBytes(MyInt2);
                serialPort1.Write(z, 0, 1);

                int MyInt3 = Convert.ToInt32(textBox3.Text);
                byte[] p = BitConverter.GetBytes(MyInt3);
                serialPort1.Write(p, 0, 1);
            }
            else
            {
                MessageBox.Show("Please control your connection");
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900