title: Semaphore time out while looping thu serialport.open()
body: <p>I have a piece of code which scans for all available comports. And than it will try to find the correct device by opening the comport and send a message, if the message is correct I have find the correct device. Now this worked pretty well but now I discovered a bug. I think I know what is happening but I'm not sure if this is the reason and also how to solve it.</p>
  <p>I get the error message: &quot;The semaphore timeout period has expired.&quot;
  Searching on Google it told me this message appears when there are problems with receive data back (files or so). I think when reaching the particular where the error appears it is one that is already open and connected to a device.</p>
  <p>I wrote my code like this:</p>
  <pre><code>            // Detect VM25 and show connection is established
          string device;
          string CheckConnection;
          string[] ports = SerialPort.GetPortNames();
          int count = 0 ;
  
          //Test which enabled port is the VM25.
          foreach (string port in ports)
          {
              serialPort1.PortName = port;
              serialPort1.ReadTimeout = 2000;
              serialPort1.Open();
  
              // Display each port name to the console.
  
              Console.WriteLine(port);
              if (serialPort1.IsOpen)
              {
                  device = &quot;&quot;;
                  serialPort1.WriteLine(&quot;#S&quot; + Environment.NewLine);
                  serialPort1.WriteTimeout = 1000;
  
                  try
                  {
                      answer = serialPort1.ReadTo(&quot;/a&quot;);
                      serialPort1.ReadTimeout = 3500;
                      serialPort1.Close();
  
                      if (answer != &quot;&quot;)
                      {
                          device = answer.Substring(0, 4);
  
                          //Find the correct response from the Com port where the VM25 is connected
                          if (device == &quot;VM25&quot;)
                          {
                              statusLblDevice.ForeColor = Color.LawnGreen;
                              statusLblDevice.Text = port + &quot; - &quot; + device + &quot; - Connected&quot;;
                              VM25Port = port;
                              serialPort1.PortName = VM25Port;
                              serialPort1.Open();
                              serialPort1.WriteLine(&quot;#H&quot; + Environment.NewLine);
                              serialPort1.WriteTimeout = 2000;                                            //Timeout for writing the comment
                              string input = serialPort1.ReadTo(&quot;/a&quot;);
                              serialPort1.ReadTimeout = 5500;                                             //Timeout for reading the data                                
                              string totalMeasurements = &quot;Measurements: &quot; + input.Substring(9, 3);        //Take the amount of measurements into a string
                              MeasTotal = int.Parse(input.Substring(9, 3));
                              string totalFFT = &quot;FFTs: &quot; + input.Substring(18, 5);                        //Take the amount of FFTs into a string
                              statusLblMeas.Text = totalMeasurements;
                              statusLblFFT.Text = totalFFT;
                              FFTtotal = int.Parse(input.Substring(18, 5));
                              serialPort1.Close();
                              
                          }
                      }
                  }
                  catch (TimeoutException)
                  {                       
                      serialPort1.Close();
                  }    
              }
  
          }
  </code></pre>
  <p>I think I need to adjust my code such that if the comport is already open it skips the and goes to the next item in the <code>foreach</code> loop? Or are the any better ways to solve this?</p>
  <p>Thanks in advance</p>