1

I have an Arduino connected to an interface I made. Everything is working fine, however the Arduino is sending a string to the interface. The program is reading the data, and storing it in a variable.

The problem I am having is that the variable stores the data, but doesn't update it when new data is coming in from the Arduino .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public SerialPort myport;

        int irisvalue;
        string myString;
        string s = "";

        //String s2;

        public Form1()
        {
           InitializeComponent();
            //Load += new EventHandler(Form1_Load);
            connectbtn.Text = "Connect";
            disconnect.Text = "Disconnect";
            this.connectbtn.Click += new EventHandler(connectbtn_Click);
            this.disconnect.Click += new EventHandler(disconnect_Click);
            this.iris1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseDown);
            this.iris1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseUp);
            this.iris2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseDown);
            this.iris2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseUp);
            this.focus1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseDown);
            this.focus1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseUp);
            this.focus2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseDown);
            this.focus2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseUp);

        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


         void connect()
        {

            myport = new SerialPort();
            myport.BaudRate = 9600;
            myport.PortName = "COM3";
            myport.Open();

        }

        void read()
        {
           s = myport.ReadLine();
           //Form1_Load();

        }

        void discon()
        {


            myport.Close();

        }

        private void disconnect_Click(object sender, System.EventArgs e)
        {
            discon();
            if (myport.IsOpen)
            {

            }
            else
            {

                connectbtn.Text = "Connect";
                disconnect.BackColor = default(Color);
                connectbtn.BackColor = default(Color);
            }
            }

            private void connectbtn_Click(object sender, System.EventArgs e)
        {
            connect();

            if (myport.IsOpen)
            {
                connectbtn.Text = "Connected";
                connectbtn.BackColor = Color.Green;
                //Load += new EventHandler(Form1_Load);
                Form1_Load();
                disconnect.BackColor = Color.Red;
                disconnect.Text = "Disconnect";
                read();
                //s = myport.ReadLine();

            }
            else
            {
                connectbtn.Text = "Error";
                connectbtn.BackColor = Color.Red;
            }


        }


        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void iris1_MouseDown(object sender, MouseEventArgs e)
        {
            //Console.WriteLine("Hello");

            irisvalue = 1;
            myString = irisvalue.ToString();
            Form1_Load();

        }

        private void iris1_MouseUp(object sender, MouseEventArgs e)
        {

            irisvalue = 0;
            myString = irisvalue.ToString();
            Form1_Load();
        }



        private void iris2_MouseDown(object sender, MouseEventArgs e)
        {
            irisvalue = 2;
            myString = irisvalue.ToString();
            Form1_Load();

        }


        private void iris2_MouseUp(object sender, MouseEventArgs e)
        {
            irisvalue = 0;
            myString = irisvalue.ToString();
            Form1_Load();

        }

        private void focus1_MouseDown(object sender, MouseEventArgs e)
        {
            irisvalue = 3;
            myString = irisvalue.ToString();
            Form1_Load();

        }


        private void focus1_MouseUp(object sender, MouseEventArgs e)
        {
            irisvalue = 0;
            myString = irisvalue.ToString();
            Form1_Load();


        }

        private void focus2_MouseDown(object sender, MouseEventArgs e)
        {
            irisvalue = 4;
            myString = irisvalue.ToString();
            Form1_Load();

        }

        private void focus2_MouseUp(object sender, MouseEventArgs e)
        {

            irisvalue = 0;
            myString = irisvalue.ToString();
            Form1_Load();
        }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            public void Form1_Load()
            {




            textBox1.Text = s;



            Console.WriteLine(s);


            myport.WriteLine(myString);




        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    }

}
1
  • 2
    Please stop refering to the program as a visual studio program. Visual Studio is an IDE, C# is the programming language and Windows Forms is the GUI framework, you're using. Therefore this is a C# WinForms application. Commented Jul 25, 2017 at 8:40

1 Answer 1

3

To receive updates you should subscribe on serial port events. Try this code:

myport.DataReceived += (sender, e) =>
{
    if (e.EventType == SerialData.Chars)
        s = myport.ReadLine();
};
Sign up to request clarification or add additional context in comments.

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.