C#从Arduino接收数据

时间:2015-12-29 18:28:53

标签: c# arduino

我正在建一个气象站,我有一点问题。如何在文本框或富文件框中运行接收的数据?该应用程序连接Arduino,但不操作读取数据。

谢谢,

[在此处输入图像说明] [1]

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string InputData = String.Empty;
        delegate void SetTextCallback(string text);


        public Form1()
        {
            InitializeComponent();
            string[] ports = SerialPort.GetPortNames();


            Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
         }   

          public void guzikpolacz_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = comboBox1.Text;         // Ustawiamy numer Portu COM
                    // Ustawiamy wartość baudrate portu            

            //Inne ustawienia zdefiniowane na stałe
            serialPort1.Parity = Parity.None;   // bez parzystości
            serialPort1.StopBits = StopBits.One;  // jeden bit stopu
            serialPort1.DataBits = 8;    // osiem bitów danych   



            serialPort1.Open();

            polestatusu.Text = "Port Otwarty";
            polestatusu = SerialPort;
            panel1.BackColor = Color.Lime;
            guzikpolacz.Enabled = false;          //blokujemy przycisk Połącz
            guzikrozlacz.Enabled = true;   
    }

        private void guzikrozlacz_Click(object sender, EventArgs e)
        {
            serialPort1.Close();             //Zamykamy SerialPort

            panel1.BackColor = Color.Red;
            polestatusu.Text = "Port Zamknięty";
            guzikpolacz.Enabled = true;   //aktywujemy przycisk Połącz
            guzikrozlacz.Enabled = false;  // deaktywujemy przycisk Rozłącz
        }

        private delegate void UpdateUiTextDelegate(string text);  
    }
}

2 个答案:

答案 0 :(得分:1)

非常感谢,但遗憾的是它无效:(应用程序连接到serialport但没有任何反应。与Arduino程序Putty的连接有效。

namespace WindowsFormsApplication1
{

 public partial class Form1 : Form
{
    string InputData = String.Empty;
    delegate void SetTextCallback(string text);


    public Form1()
    {
        InitializeComponent();
        string[] ports = SerialPort.GetPortNames();


        Console.WriteLine("The following serial ports were found:");

        // Display each port name to the console.
        foreach (string port in ports)
        {
            comboBox1.Items.Add(port);
        }
    ;}



      public void guzikpolacz_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = comboBox1.Text;         // Ustawiamy numer Portu COM
                // Ustawiamy wartość baudrate portu            

        //Inne ustawienia zdefiniowane na stałe
        serialPort1.Parity = Parity.None;   // bez parzystości
        serialPort1.StopBits = StopBits.One;  // jeden bit stopu
        serialPort1.DataBits = 8;    // osiem bitów danych   



        serialPort1.Open();

        polestatusu.Text = "Port Otwarty";

        panel1.BackColor = Color.Lime;
        guzikpolacz.Enabled = false;          //blokujemy przycisk Połącz
        guzikrozlacz.Enabled = true;   
}

    private void guzikrozlacz_Click(object sender, EventArgs e)
    {
        serialPort1.Close();             //Zamykamy SerialPort

        panel1.BackColor = Color.Red;
        polestatusu.Text = "Port Zamknięty";
        guzikpolacz.Enabled = true;   //aktywujemy przycisk Połącz
        guzikrozlacz.Enabled = false;  // deaktywujemy przycisk Rozłącz
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Action(() => serialPort1_DataReceived(sender, e)));
        }
        else
        {
            var sp = sender as SerialPort;
            //this assumes you want the data from the arduino as text.  
            // you may need to decode it here.
            textBox1.Text = sp.ReadExisting();
        }
    }



 private delegate void UpdateUiTextDelegate(string text);



 public Delegate myDelegate { get; set; }
    }
}

enter image description here

答案 1 :(得分:0)

尝试将此事件处理程序添加到串行端口

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    if (this.InvokeRequired)
    {
        this.Invoke(new Action(() => serialPort1_DataReceived(sender, e)));
    }
    else
    {
        var sp = sender as SerialPort;
        //this assumes you want the data from the arduino as text.  
        // you may need to decode it here.
        polestatusu.Text = sp.ReadExisting();
    }
}