简体   繁体   中英

Read string over Serial from Arduino

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




        }

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


    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM