C# 如何显示在串行端口的 DataReceived 事件处理程序中读取的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11590945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:28:43  来源:igfitidea点击:

How to display the data read in DataReceived event handler of serialport

c#multithreadingserial-port

提问by Cdeez

I have the following code which needs the data to be read from port and then display in a textbox. I am using DataReceived event handler for this purpose but donot know how to display this data in textbox. From various sources i learnt that Invoke method should be used for this but donot know how to use it. Suggestions please...

我有以下代码需要从端口读取数据然后显示在文本框中。我为此使用 DataReceived 事件处理程序,但不知道如何在文本框中显示此数据。我从各种来源了解到应该为此使用 Invoke 方法,但不知道如何使用它。建议请...

    private void Form1_Load(object sender, EventArgs e)
    {
        //SerialPort mySerialPort = new SerialPort("COM3");
        mySerialPort.PortName = "COM3";
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
        mySerialPort.Open();
    }

    private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string s= sp.ReadExisting();
        // next i want to display the data in s in a textbox. textbox1.text=s gives a cross thread exception
    }
    private void button1_Click(object sender, EventArgs e)
    {

        mySerialPort.WriteLine("AT+CMGL=\"ALL\"");

    }

采纳答案by Frank Bollack

The MSDN contains a good articlewith examples about using control methods and properties from other threads.

MSDN 包含一篇很好的文章,其中包含有关使用其他线程的控制方法和属性的示例。

In short, what you need is a delegate method that sets the Textproperty of your text box with a given string. You then call that delegate from within your mySerialPort_DataReceivedhandler via the TextBox.Invoke()method. Something like this:

简而言之,您需要的是一个委托方法,它Text使用给定的字符串设置文本框的属性。然后,您可以mySerialPort_DataReceived通过该TextBox.Invoke()方法从处理程序中调用该委托。像这样的东西:

public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;

private void Form1_Load(object sender, EventArgs e)
{
    //...
    this.myDelegate = new AddDataDelegate(AddDataMethod);
}

public void AddDataMethod(String myString)
{
    textbox1.AppendText(myString);
}

private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   SerialPort sp = (SerialPort)sender;
   string s= sp.ReadExisting();

   textbox1.Invoke(this.myDelegate, new Object[] {s});       
}

回答by KMC

Try this (works for me):

试试这个(对我有用):

private delegate void UpdateUiTextDelegate(string text);

private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    if (mySerialPort.IsOpen)
    {
        RxString = mySerialPort.ReadLine();
        Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), RxString);
    }
}

private void DisplayText(string RxString)
{
    myTextBox.Text = RxString;
}

回答by Fixer

I'm creating a GUI "Form" for USB COM Ports. This is how I send data to the window without getting a "Cross-Thread" error.

我正在为 USB COM 端口创建一个 GUI“表单”。这就是我如何将数据发送到窗口而不会出现“跨线程”错误。

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string inData = serialPort1.ReadLine(); // ReadLine includes the + "\n"
    displayToWindow(inData);
}

private void displayToWindow(string inData)
{
    BeginInvoke(new EventHandler(delegate
    {
        richTextBox1.AppendText(inData);
        richTextBox1.ScrollToCaret();
    }));
}