簡體   English   中英

C#從Arduino接收數據

[英]C# Received data from 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);  
    }
}

非常感謝,但不幸的是,它沒有用:(應用程序連接到串行端口,但是什么也沒發生。與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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM