<small id='cE6R8'></small><noframes id='cE6R8'>

  • <legend id='cE6R8'><style id='cE6R8'><dir id='cE6R8'><q id='cE6R8'></q></dir></style></legend>

    <i id='cE6R8'><tr id='cE6R8'><dt id='cE6R8'><q id='cE6R8'><span id='cE6R8'><b id='cE6R8'><form id='cE6R8'><ins id='cE6R8'></ins><ul id='cE6R8'></ul><sub id='cE6R8'></sub></form><legend id='cE6R8'></legend><bdo id='cE6R8'><pre id='cE6R8'><center id='cE6R8'></center></pre></bdo></b><th id='cE6R8'></th></span></q></dt></tr></i><div id='cE6R8'><tfoot id='cE6R8'></tfoot><dl id='cE6R8'><fieldset id='cE6R8'></fieldset></dl></div>

  • <tfoot id='cE6R8'></tfoot>

        <bdo id='cE6R8'></bdo><ul id='cE6R8'></ul>
      1. 如何显示在串口的 DataReceived 事件处理程序中读取的数据

        How to display the data read in DataReceived event handler of serialport(如何显示在串口的 DataReceived 事件处理程序中读取的数据)
        <tfoot id='DDTpM'></tfoot>

            <tbody id='DDTpM'></tbody>

            <legend id='DDTpM'><style id='DDTpM'><dir id='DDTpM'><q id='DDTpM'></q></dir></style></legend>
            • <bdo id='DDTpM'></bdo><ul id='DDTpM'></ul>
                <i id='DDTpM'><tr id='DDTpM'><dt id='DDTpM'><q id='DDTpM'><span id='DDTpM'><b id='DDTpM'><form id='DDTpM'><ins id='DDTpM'></ins><ul id='DDTpM'></ul><sub id='DDTpM'></sub></form><legend id='DDTpM'></legend><bdo id='DDTpM'><pre id='DDTpM'><center id='DDTpM'></center></pre></bdo></b><th id='DDTpM'></th></span></q></dt></tr></i><div id='DDTpM'><tfoot id='DDTpM'></tfoot><dl id='DDTpM'><fieldset id='DDTpM'></fieldset></dl></div>

                  <small id='DDTpM'></small><noframes id='DDTpM'>

                1. 本文介绍了如何显示在串口的 DataReceived 事件处理程序中读取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  推荐答案

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

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

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

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

                  这篇关于如何显示在串口的 DataReceived 事件处理程序中读取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <legend id='nUNBN'><style id='nUNBN'><dir id='nUNBN'><q id='nUNBN'></q></dir></style></legend>

                        <tbody id='nUNBN'></tbody>
                      <i id='nUNBN'><tr id='nUNBN'><dt id='nUNBN'><q id='nUNBN'><span id='nUNBN'><b id='nUNBN'><form id='nUNBN'><ins id='nUNBN'></ins><ul id='nUNBN'></ul><sub id='nUNBN'></sub></form><legend id='nUNBN'></legend><bdo id='nUNBN'><pre id='nUNBN'><center id='nUNBN'></center></pre></bdo></b><th id='nUNBN'></th></span></q></dt></tr></i><div id='nUNBN'><tfoot id='nUNBN'></tfoot><dl id='nUNBN'><fieldset id='nUNBN'></fieldset></dl></div>

                      <small id='nUNBN'></small><noframes id='nUNBN'>

                        <tfoot id='nUNBN'></tfoot>
                          <bdo id='nUNBN'></bdo><ul id='nUNBN'></ul>