• <bdo id='pVXyV'></bdo><ul id='pVXyV'></ul>
    <legend id='pVXyV'><style id='pVXyV'><dir id='pVXyV'><q id='pVXyV'></q></dir></style></legend><tfoot id='pVXyV'></tfoot>

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

    1. <i id='pVXyV'><tr id='pVXyV'><dt id='pVXyV'><q id='pVXyV'><span id='pVXyV'><b id='pVXyV'><form id='pVXyV'><ins id='pVXyV'></ins><ul id='pVXyV'></ul><sub id='pVXyV'></sub></form><legend id='pVXyV'></legend><bdo id='pVXyV'><pre id='pVXyV'><center id='pVXyV'></center></pre></bdo></b><th id='pVXyV'></th></span></q></dt></tr></i><div id='pVXyV'><tfoot id='pVXyV'></tfoot><dl id='pVXyV'><fieldset id='pVXyV'></fieldset></dl></div>
      1. .NET SerialPort DataReceived 事件未触发

        .NET SerialPort DataReceived event not firing(.NET SerialPort DataReceived 事件未触发)
        • <bdo id='DGFrk'></bdo><ul id='DGFrk'></ul>

          <tfoot id='DGFrk'></tfoot>
          1. <i id='DGFrk'><tr id='DGFrk'><dt id='DGFrk'><q id='DGFrk'><span id='DGFrk'><b id='DGFrk'><form id='DGFrk'><ins id='DGFrk'></ins><ul id='DGFrk'></ul><sub id='DGFrk'></sub></form><legend id='DGFrk'></legend><bdo id='DGFrk'><pre id='DGFrk'><center id='DGFrk'></center></pre></bdo></b><th id='DGFrk'></th></span></q></dt></tr></i><div id='DGFrk'><tfoot id='DGFrk'></tfoot><dl id='DGFrk'><fieldset id='DGFrk'></fieldset></dl></div>
              <tbody id='DGFrk'></tbody>
          2. <legend id='DGFrk'><style id='DGFrk'><dir id='DGFrk'><q id='DGFrk'></q></dir></style></legend>
              • <small id='DGFrk'></small><noframes id='DGFrk'>

                1. 本文介绍了.NET SerialPort DataReceived 事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 WPF 测试应用程序,用于评估基于事件的串行端口通信(与轮询串行端口相比).问题是 DataReceived 事件似乎根本没有触发.

                  I have a WPF test app for evaluating event-based serial port communication (vs. polling the serial port). The problem is that the DataReceived event doesn't seem to be firing at all.

                  我有一个非常基本的 WPF 表单,其中包含一个用于用户输入的 TextBox、一个用于输出的 TextBlock,以及一个用于将输入写入串行端口的按钮.

                  I have a very basic WPF form with a TextBox for user input, a TextBlock for output, and a button to write the input to the serial port.

                  代码如下:

                  public partial class Window1 : Window
                  {
                      SerialPort port;
                  
                      public Window1()
                      {
                          InitializeComponent();
                  
                          port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
                          port.DataReceived +=
                              new SerialDataReceivedEventHandler(port_DataReceived);  
                          port.Open();
                      }
                  
                      void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                      {
                          Debug.Print("receiving!");
                          string data = port.ReadExisting();
                          Debug.Print(data);
                          outputText.Text = data;
                      }
                  
                      private void Button_Click(object sender, RoutedEventArgs e)
                      {
                          Debug.Print("sending: " + inputText.Text);
                          port.WriteLine(inputText.Text);
                      }
                  }

                  现在,这里是复杂的因素:

                  Now, here are the complicating factors:

                  1. 我正在使用的笔记本电脑没有串口,所以我使用了一款名为 Virtual Serial Port Emulator 的软件来设置 COM2.VSPE 过去的表现令人钦佩,目前尚不清楚为什么它只会与 .NET 的 SerialPort 类一起出现故障,但我提一下以防万一.

                  1. The laptop I'm working on has no serial ports, so I'm using a piece of software called Virtual Serial Port Emulator to setup a COM2. VSPE has worked admirably in the past, and it's not clear why it would only malfunction with .NET's SerialPort class, but I mention it just in case.

                  当我点击表单上的按钮发送数据时,我的超级终端窗口(连接到 COM2)显示数据正在通过.是的,当我想测试我的表单读取端口的能力时,我断开了超级终端.

                  When I hit the button on my form to send the data, my Hyperterminal window (connected on COM2) shows that the data is getting through. Yes, I disconnect Hyperterminal when I want to test my form's ability to read the port.

                  我尝试在连接事件之前打开端口.没有变化.

                  I've tried opening the port before wiring up the event. No change.

                  我在这里阅读了另一篇帖子,其中其他人遇到了类似的问题.在这种情况下,这些信息都没有帮助我.

                  I've read through another post here where someone else is having a similar problem. None of that info has helped me in this case.

                  这里是控制台版本(修改自 http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx):

                  Here's the console version (modified from http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx):

                  class Program
                  {
                      static SerialPort port;
                  
                      static void Main(string[] args)
                      {
                          port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
                          port.DataReceived +=
                              new SerialDataReceivedEventHandler(port_DataReceived);
                          port.Open();
                  
                          string text;
                          do
                          {
                              text = Console.ReadLine();
                              port.Write(text + "
                  ");
                          }
                          while (text.ToLower() != "q");
                      }
                  
                      public static void port_DataReceived(object sender,
                          SerialDataReceivedEventArgs args)
                      {
                          string text = port.ReadExisting();
                          Console.WriteLine("received: " + text);
                      }
                  }

                  这应该消除任何关于这是线程问题的担忧(我认为).这也不起作用.再次,超级终端报告通过端口发送的数据,但控制台应用程序似乎没有触发 DataReceived 事件.

                  This should eliminate any concern that it's a Threading issue (I think). This doesn't work either. Again, Hyperterminal reports the data sent through the port, but the console app doesn't seem to fire the DataReceived event.

                  编辑 #2:

                  我意识到我有两个独立的应用程序应该从串口发送和接收,所以我决定尝试同时运行它们...

                  I realized that I had two separate apps that should both send and receive from the serial port, so I decided to try running them simultaneously...

                  如果我在控制台应用程序中键入内容,WPF 应用程序 DataReceived 事件将触发,并出现预期的线程错误(我知道如何处理).

                  If I type into the console app, the WPF app DataReceived event fires, with the expected threading error (which I know how to deal with).

                  如果我在 WPF 应用程序中键入内容,控制台应用程序 DataReceived 事件将触发,并回显数据.

                  If I type into the WPF app, the console app DataReceived event fires, and it echoes the data.

                  我猜测问题出在我使用 VSPE 软件的某个地方,该软件设置为将一个串行端口同时视为输入和输出.通过 SerialPort 类的一些奇怪之处,一个串行端口的实例不能同时是发送者和接收者.反正我觉得解决了.

                  I'm guessing the issue is somewhere in my use of the VSPE software, which is set up to treat one serial port as both input and output. And through some weirdness of the SerialPort class, one instance of a serial port can't be both the sender and receiver. Anyway, I think it's solved.

                  推荐答案

                  我只能推测问题确实出在 Virtual Serial Port Emulator 程序上.这并不是说该软件有问题:到目前为止,VSPE 对我来说效果很好.但是我的代码和我设置 VSPE 连接器的方式之间存在一些冲突.

                  I can only surmise that the problem was indeed with the Virtual Serial Port Emulator program. this is NOT to say there is a problem with that software: VSPE has worked very well for me so far. But there was some conflict between my code and how I had set up the VSPE connector.

                  这篇关于.NET SerialPort DataReceived 事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  Func Delegate vs Function(函数委托与函数)
                  CreateDelegate with unknown types(具有未知类型的 CreateDelegate)

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

                          <tbody id='tBfGa'></tbody>
                        • <bdo id='tBfGa'></bdo><ul id='tBfGa'></ul>

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