如何显示在的serialport的DataReceived事件检索处理程序读出的数据 [英] How to display the data read in DataReceived event handler of serialport

查看:872
本文介绍了如何显示在的serialport的DataReceived事件检索处理程序读出的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从端口读下面的代码,需要的数据,然后在文本框显示。我使用DataReceived事件检索处理程序用于此目的,但DONOT知道如何来显示文本框此数据。从各种来源我得知Invoke方法应当用于这个但DONOT知道如何使用它。建议请...

 私人无效Form1_Load的(对象发件人,EventArgs五)
{
//的SerialPort mySerialPort =新的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 + =新SerialDataReceivedEventHandler(mySerialPort_DataReceived);
mySerialPort.Open();
}

私人无效mySerialPort_DataReceived(对象发件人,SerialDataReceivedEventArgs E)
{
的SerialPort SP =(的SerialPort)发送;
字符串s = sp.ReadExisting();
//接下来,我想在一个文本框来显示s中的数据。 textbox1.text = s给出一个跨线程异常
}
私人无效的button1_Click(对象发件人,EventArgs五)
{

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

}


解决方案

在MSDN包含有关使用控制方法,并从其他线程特性例子很好的文章



在总之,你需要的是那台文本框的文字属性与给定的字符串委托方法。然后,通过调用该委托从 mySerialPort_DataReceived 处理程序中TextBox.Invoke()方法。事情是这样的:

 公共委托无效AddDataDelegate(字符串myString的); 
公共AddDataDelegate myDelegate;

私人无效Form1_Load的(对象发件人,EventArgs五)
{
// ...
this.myDelegate =新AddDataDelegate(AddDataMethod);
}

公共无效AddDataMethod(字符串MyString的)
{
textbox1.AppendText(myString的);
}

私人无效mySerialPort_DataReceived(对象发件人,SerialDataReceivedEventArgs E)
{
的SerialPort SP =(的SerialPort)发送;
字符串s = sp.ReadExisting();

textbox1.Invoke(this.myDelegate,新的对象[] {S});
}


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...

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

    }

解决方案

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

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

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

这篇关于如何显示在的serialport的DataReceived事件检索处理程序读出的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆