title: SerialPort doesn't actually open the VirtualCOM port
body: <p>I have a microcontroller hooked up via USB CDC/ACM to the PC.  When I try to open the port with an app I'm trying to make that uses SerialPort, I see nothing on the microcontroller.  I can easily set break points in the micro and watch the port open/write/read/close events get triggered when using RealTerm, TeraTerm, PuTTy, etc, but for my application, nothing.  No error is given and the application seems to think that everything worked fine.  After opening, even though I see nothing on the micro, the other terminal applications can no longer connect on that port, but once I close it in my app, they work again.   Further, if I try to write to the port after opening, the app just hangs. So what step am I missing?</p>
  <p>Here is my code:</p>
  <pre><code>using System;
  using System.ComponentModel;
  using System.IO.Ports;
  using System.Windows;
  using System.Windows.Controls;
  
  namespace SerialApp
  {
      public partial class MainWindow : Window
      {
          static SerialPort _serialPort = new SerialPort();
          static byte[] read_buffer = new byte[4096];
          static byte[] getSerialNumberCmd = { 0x6D, 0x69, 0x6E, 0x64, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void comboBoxPorts_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              string port = comboBoxPorts.SelectedIndex.ToString();
              port = &quot;COM&quot; + port;
  
              _serialPort.PortName = port;
              _serialPort.BaudRate = 115200;
              _serialPort.Parity = Parity.None;
              _serialPort.DataBits = 8;
              _serialPort.StopBits = StopBits.One;
              _serialPort.Handshake = Handshake.None;
              _serialPort.ReadBufferSize = 4096;
              _serialPort.WriteTimeout = 500;
              _serialPort.ReadTimeout = 500;
          }
  
          private void button_Click(object sender, RoutedEventArgs e)
          {
              try
              {
                  if (_serialPort.IsOpen) _serialPort.Close();
                  if (_serialPort != null)
                  {
                      _serialPort.Open();
                      _serialPort.DataReceived += Serial_DataReceived;
  
                      comboBoxPorts.IsEnabled = false;
                      buttonClosePort.IsEnabled = true;
                      buttonOpenPort.IsEnabled = false;
                      label1.Content = &quot;Connected to &quot; + _serialPort.PortName;
                  }
                  else
                  {
                      label1.Content = &quot;Choose a COM port.&quot;;
                  }
              }
              catch(Exception ex)
              {
                  label1.Content = &quot;Invalid COM port.&quot;;
              }
          }
  
          private void Serial_DataReceived(object s, SerialDataReceivedEventArgs e)
          {
              _serialPort.Read(read_buffer, 0, 64 + 12);
          }
  
          private void Window_Closing(object sender, CancelEventArgs e)
          {
              if (_serialPort.IsOpen)
              {
                  _serialPort.Close();
              }
              if (_serialPort != null)
              {
                  _serialPort.Dispose();
              }
          }
  
          private void buttonClosePort_Click(object sender, RoutedEventArgs e)
          {
              if (_serialPort.IsOpen)
              {
                  _serialPort.Close();
                  
                  buttonClosePort.IsEnabled = false;
                  buttonOpenPort.IsEnabled = true;
                  comboBoxPorts.IsEnabled = true;
                  label1.Content = &quot;Choose a COM port.&quot;;
              }
          }
  
          private void buttonGetSerial_Click(object sender, RoutedEventArgs e)
          {
              try
              {
                  if (_serialPort.IsOpen)
                  {
                      _serialPort.Write(getSerialNumberCmd, 0, 64);
                  }
              }
              catch(Exception ex)
              {
                  _serialPort.Close();
                  labelSerialNumber.Content = ex.Message;
              }
          }
      }
  }
  </code></pre>
  <p>Incidentally I tried to do the same thing on another microcontroller of a completely different family and I see the same behavior.</p>