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

    1. <small id='PNbUD'></small><noframes id='PNbUD'>

    2. <tfoot id='PNbUD'></tfoot>
      <legend id='PNbUD'><style id='PNbUD'><dir id='PNbUD'><q id='PNbUD'></q></dir></style></legend>

        在两种不同的形式上使用相同的串口数据接收事件

        Using same serial port data received event on two different forms(在两种不同的形式上使用相同的串口数据接收事件)

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

            <small id='36L2v'></small><noframes id='36L2v'>

              <bdo id='36L2v'></bdo><ul id='36L2v'></ul>

                  <tfoot id='36L2v'></tfoot>

                  <legend id='36L2v'><style id='36L2v'><dir id='36L2v'><q id='36L2v'></q></dir></style></legend>
                  本文介绍了在两种不同的形式上使用相同的串口数据接收事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 SerialDataReceivedEventHandler 出现问题,无法响应 serail 端口中的数据.

                  I have a problem with my SerialDataReceivedEventHandler whitch fails to respond to data in serail port.

                  我有一个主要形式,我打开端口并做其他事情需要完成正确的串行端口通信(发送和接收工作)!然后我在同一个项目中打开另一个表单,需要相同的串行端口进行读写!问题是我在 form2 中的 SerialDataReceivedEventHandler 无法正常工作,它与 mainform 中的第一个完全相同.(如果我在主窗体中调用 serial.close() 应用程序冻结或导致巨大延迟)

                  I have one main form in whitch i open port and do other stuf whitch need to be done for proper serial port communication (sending and receiving work)! Then i open another form in same project whitch need same serial port for reading and writing! Problem is that my SerialDataReceivedEventHandler in form2 not working jet it is completely identical to the first in mainform. (if i call serial.close() in main form app freezes or cause huge delay)

                  可能我必须将此事件的主要内容公开,但我仍然不知道如何使我的自定义事件或其他会触发表单 2 中的事件的事件,即数据到达端口

                  probably i have to make in my main from this event public, but i still don't konow how to make my custom event or something else that will trigger the event in form 2 that data is arrived on port

                  我找到此链接寻求帮助,但不适用于我的应用程序.

                  I found this link for help but does not work with my app.

                  http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/7efccf0e-b412-4869-b942-a006773a833f

                  我正在使用VS2008,framework3.5(智能设备项目)

                  i'm using VS2008,framework3.5 (smart device project)

                  有人可以帮我吗?,请!

                  can somebody help me with that? , Please!

                  推荐答案

                  将您的 SerialPort 消耗移动到单独的静态(或单例)类中.在该类中创建一个 DataReceived 事件,并在每次接收到数据时触发它.让两个表单都订阅 DataReceived 事件 - 这样两个表单都将接收数据.

                  Move your SerialPort consumption into a separate static (or a singleton) class. Create a DataReceived event in that class and fire it every time data is received. Have both forms subscribe to the DataReceived event - this way both forms will receive the data.

                  编辑 1:伪代码示例

                  public static class Serial {
                      public static delegate void DataReceivedEventHandler(object sender, ReceivedEventArgs e);
                      public static event DataReceivedEventHandler DataReceived;
                      static SerialPort serialPort = new SerialPort();        
                  
                      static Serial() {
                          serialPort = new SerialPort();
                          serialPort.DataReceived += Incoming;
                          serialPort.Open();
                      }
                  
                      private static void Incoming(object sender, SerialDataReceivedEventHandler args) {
                          if (DataReceived != null) {
                             ReceivedEventArgs rea = new ReceivedEventArgs {Data = args.Data};
                             DataReceived(this, rea);
                          }
                      }
                  }
                  
                  public class ReceivedEventArgs : EventArgs {
                     public string Data { get; set;}
                  }
                  
                  public class Form1: Form {
                      public Form1() {
                         Serial.DataReceived += Incoming;
                      }
                  
                      private void Incoming(object sender, ReceivedEventArgs e) {
                          // you receive the data here
                          Debug.WriteLine(e.Data);
                      }
                  }
                  
                  public class Form2: Form {
                      public Form2() {
                         Serial.DataReceived += Incoming;
                      }
                  
                      private void Incoming(object sender, ReceivedEventArgs e) {
                          // you receive the data here
                          Debug.WriteLine(e.Data);
                      }
                  }
                  

                  同样,这是伪代码,附近没有编译器.希望这会有所帮助.

                  Again, this is pseudo code, without compiler nearby. Hope this helps.

                  这篇关于在两种不同的形式上使用相同的串口数据接收事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                  <i id='sythj'><tr id='sythj'><dt id='sythj'><q id='sythj'><span id='sythj'><b id='sythj'><form id='sythj'><ins id='sythj'></ins><ul id='sythj'></ul><sub id='sythj'></sub></form><legend id='sythj'></legend><bdo id='sythj'><pre id='sythj'><center id='sythj'></center></pre></bdo></b><th id='sythj'></th></span></q></dt></tr></i><div id='sythj'><tfoot id='sythj'></tfoot><dl id='sythj'><fieldset id='sythj'></fieldset></dl></div>

                        <tbody id='sythj'></tbody>
                      <tfoot id='sythj'></tfoot>
                          <bdo id='sythj'></bdo><ul id='sythj'></ul>

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

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