2017-07-25 160 views
1

我有一个Arduino连接到我所做的一个接口。一切工作正常,但是Arduino发送一个string到接口。该程序正在读取数据,并将其存储在一个变量中。从串口读取字符串从Arduino

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




     } 

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


    } 

} 
+2

请停止将该程序作为'visual studio program'引用。 Visual Studio是一个IDE,C#是编程语言,“Windows Forms”是你正在使用的GUI框架。因此这是一个C#WinForms应用程序。 – jAC

回答

3

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

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

非常感谢你的工作 –