title: SerialPort access denied after a while
body: <p>I have a FEZ Board (I don't think this matters) and I'm communicating with it through a serial port.
  I made a WPF application, where I open the COM port and write data over it.
  This all works great, but after sending data a random amount of times (sometimes 3, sometimes 4) I get an exception saying "A device attached to the system is not functioning" on the Write method of the port, when I try sending again or try closing the port after this exception I get "Access to the port is denied". When I restart the program I get the same error on the Open method (which is logical ofcourse). When I just disconnect the USB which represents the serial link and reconnect it, I can use the port again.</p>
  
  <p>Here's my code: </p>
  
  <pre><code>    SerialPort port = new SerialPort("COM10", 9600);
      public MainWindow()
      {
          InitializeComponent();
          port.Open();
      }
  
      private void button1_Click(object sender, RoutedEventArgs e)
      {
          SendMessage("Relay1On\r\n");
      }
  
      private void button2_Click(object sender, RoutedEventArgs e)
      {
          SendMessage("Relay1Off\r\n");
      }
  
      private void SendMessage(string message)
      {
          try
          {
              port.Write(message);
          }
          catch (Exception exc)
          {
              listbox1.Items.Add(exc.Message);
          }
      }
  
      private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
          port.Close();
      }
  </code></pre>
  
  <p>I don't think I'm doing anything weird.
  The code which is running on the FEZ Board is the following:</p>
  
  <pre><code>    private Gadgeteer.Interfaces.DigitalOutput _relay;
  
      void ProgramStarted()
      {
          try
          {
              usbSerial.Configure(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8);
              usbSerial.SerialLine.LineReceivedEventDelimiter = "\r\n";
              usbSerial.SerialLine.AutoReadLineEnabled = true;
              usbSerial.SerialLine.ReadTimeout = GT.Interfaces.Serial.InfiniteTimeout;
              usbSerial.SerialLine.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(SerialLine_LineReceived);
              usbSerial.SerialLine.Open();
              _relay = eBlockExpansion.SetupDigitalOutput(GT.Socket.Pin.Nine, false);
          }
          catch (Exception exc)
          {
              throw exc;
          }
      }
  
      void SerialLine_LineReceived(GT.Interfaces.Serial sender, string data)
      {
          if (data == "Relay1On")
          {
              _relay.Write(true);
          }
          else if (data == "Relay1Off")
          {
              _relay.Write(false);            
          }
      }
  </code></pre>
  
  <p>But I have this feeling it has more to do with the Desktop side.</p>