title: .NET SerialPort DataReceived event not firing
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>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);
      }
  }</code></pre>
  
  <p>Now, here are the complicating factors:</p>
  
  <ol>
  <li><p>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.</p></li>
  <li><p>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.</p></li>
  <li><p>I've tried opening the port before wiring up the event.  No change.</p></li>
  </ol>
  
  <p>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.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's the console version (modified from <a href="http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx" rel="noreferrer">http://mark.michaelis.net/Blog/TheBasicsOfSystemIOPortsSerialPort.aspx</a>):</p>
  
  <pre><code>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 + "\r\n");
          }
          while (text.ToLower() != "q");
      }
  
      public static void port_DataReceived(object sender,
          SerialDataReceivedEventArgs args)
      {
          string text = port.ReadExisting();
          Console.WriteLine("received: " + text);
      }
  }</code></pre>
  
  <p>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.</p>
  
  <p><strong>EDIT #2:</strong></p>
  
  <p>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...</p>
  
  <p>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).</p>
  
  <p>If I type into the WPF app, the console app DataReceived event fires, and it echoes the data.</p>
  
  <p>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.</p>