从Arduino读取Serial over Serial

时间:2017-07-25 08:33:29

标签: c# arduino serial-port

我有一个Arduino连接到我制作的界面。一切都运行正常,但Arduino正在向界面发送string。程序正在读取数据,并将其存储在变量中。

我遇到的问题是变量存储数据,但是当新数据从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 个答案:

答案 0 :(得分:3)

要接收更新,您应订阅串口事件。试试这段代码:

myport.DataReceived += (sender, e) =>
{
    if (e.EventType == SerialData.Chars)
        s = myport.ReadLine();
};