1

I'm building a weather station and I have a little problem. How do the received data to run at a textbox or rich box? The application connects Arduino, but does not operate to read data.

Thank you,

[enter image description here][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);  
    }
}
1
  • You shouldn't need the two delegates you defined. Commented Dec 29, 2015 at 20:17

2 Answers 2

1

Thank you very much, but unfortunately it did not work :( The application connects to the serialport but nothing happens. The connection to the Arduino program Putty works.

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

Sign up to request clarification or add additional context in comments.

Comments

0

try adding this event handler to your serial port

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.