从SerialDataReceivedEventHandler写入文本框控件

时间:2013-02-21 22:57:29

标签: c# winforms static controller

我很确定只是我以错误的方式做这件事。

我的Form1有一个按钮,可以在我的serialConn.cs中调用一个名为connect()的方法。

public static bool connect(string comPort) {
        BTserial = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One);
        BTserial.Open();
        if (BTserial.IsOpen) {
            BTserial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent);
            return true;
        } else {
            return false;
        }
    }

private static void DataReceivedEvent(object sender, SerialDataReceivedEventArgs e) {
        Debug.WriteLine("Data Incomming!");
        // Check if Chars are received
        if (e.EventType == SerialData.Chars) {
            Debug.WriteLine("Chars!");
            // Create new buffer
            byte[] ReadBuffer = new byte[BTserial.BytesToRead];

            // Read bytes from buffer
            BTserial.Read(ReadBuffer, 0, ReadBuffer.Length);
            BTserial.DiscardInBuffer();

            // Encode to string
            string data = bytesToString(ReadBuffer);

            ReadBuffer = null;
            data = null;
        }
    }

这一切都很好,但是当收到数据时,我希望它打印在TextBox的{​​{1}}控制器中。 但是因为我的Form1DataReceivedEvent()(我想我必须是?),我无法访问任何内容? 那么最好的解决方法是什么呢?

1 个答案:

答案 0 :(得分:1)

您需要将Form实例传递给此connect方法(来自按钮的事件处理程序):

public class Form1 : Form {

  public void Button1_Click(object sender, EventArgs e) {
    serialConn.connect("the com port here", this);
  }

  // ... etc ...

}

在serialConn中:

public static bool connect(string comPort, Form1 whichForm) {

然后你可以使用lambda函数并关闭它里面的“whichForm”引用:

但是,你应该确保你没有从主要的其他线程实际修改GUI - 这很可能是由于SerialPort类的性质,它可能很好地从另一个背景引发事件thread - 因此this.Invoke(其他一些lambda)整理那个特定的动作,然后在主线程上执行。

MSDN明确指出:http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx

从SerialPort对象接收数据时,在辅助线程上引发DataReceived事件。因为此事件是在辅助线程而不是主线程上引发的,所以尝试修改主线程中的某些元素(例如UI元素)可能会引发线程异常。如果有必要修改主窗体或控件中的元素,请使用Invoke发回更改请求,这将在正确的线程上完成工作。

public static bool connect(string comPort, Form1 whichForm) {
    BTserial = new SerialPort(comPort, 9600, Parity.None, 8, StopBits.One);
    BTserial.Open();
    if (BTserial.IsOpen) {
        BTserial.DataReceived += (sender, e) => {
          Debug.WriteLine("Data Incomming!");
          // Check if Chars are received
          if (e.EventType == SerialData.Chars) {
              Debug.WriteLine("Chars!");
              // Create new buffer
              byte[] ReadBuffer = new byte[BTserial.BytesToRead];

              // Read bytes from buffer
              BTserial.Read(ReadBuffer, 0, ReadBuffer.Length);
              BTserial.DiscardInBuffer();

              // Encode to string
              string data = bytesToString(ReadBuffer);


              Action toBeRunOnGuiThread = () => whichForm.theTextBox.Text = data;

              // to guard yourself from all evil
              // you could check to see if it is needed to
              if (whichForm.InvokeRequired) 
                // marshal the call to the action all the way to the GUI thread
                whichForm.Invoke(toBeRunOnGuiThread);
              else
                // or, if we ARE on the GUI thread already, just call it from this thread
                toBeRunOnGuiThread();

              ReadBuffer = null;
              data = null;
          }
    };
        return true;
    } else {
        return false;
    }
}