如何使用C#清除arduino中的串行数据? [英] How to clear serial data in arduino using C#?

查看:176
本文介绍了如何使用C#清除arduino中的串行数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我现在正在和C#和Arduino一起工作。我想从C#表单应用程序中的文本框中发送两个或更多数据。到串口监视器。但我的串口只读取第一个数字,其他数字没有看到。串口读取后,我搜索了一些清除名字。但我没有找到任何符合我目标的东西。我的c#代码:



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 :

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);
            
            
        }





我的arduino代码:





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);
  }

}





我尝试过:



实际上我想这样:当我用C#发送3时,另一个表格打开并再次写入任何数字,我的指示是开放的,直到我写的最后一个数字。



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.

推荐答案

目前尚不清楚你真正想做什么。无论如何,请注意:

  • 在您的C#代码中,如果您在TextBox中输入1然后将四个字节发送到串行线:三个 0 s和一个 1 (你的Arduino代码不会处理 0 )。
  • 在你的Arduino代码中,你没有检查是否 Serial.read()返回 -1 (即没有可用数据)。
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).


我的Arduino部分:



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);
  }
}





我的c#部分:





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");
            }


这篇关于如何使用C#清除arduino中的串行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆