C#接收到来自Arduino的数据

本文关键字:Arduino 数据 | 更新日期: 2023-09-27 18:28:21

我正在建一个气象站,遇到了一个小问题。如何在文本框或富框中运行接收到的数据?该应用程序连接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);  
    }
}

C#接收到来自Arduino的数据

非常感谢,但不幸的是,它没有工作:(应用程序连接到串行端口,但什么都没发生。连接到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; }
    }
}

在此处输入图像描述

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

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