public bool FindHmdKitDevice()

in hmdvalidationkit/managed/HmdKit.cs [130:210]


        public bool FindHmdKitDevice()
        {
            lock (this.hmdKitLock)
            {
                if (this.serialPort != null)
                {
                    if (this.serialPort.IsOpen)
                    {
                        string response = string.Empty;
                        this.SendCommand("GetVersion", ResponseTimeDefault, null, out response);
                        if (response == (HmdKitVersion + HmdKitShieldType))
                        {
                            this.isPresent = true;
                            return true;
                        }
                        else
                        {
                            this.serialPort.Close();
                        }
                    }
                }

                string[] ports = SerialPort.GetPortNames();
                foreach (string port in ports)
                {
                    SerialPort sp = new SerialPort(port, HmdKitBaudRate, Parity.None, 8, StopBits.One);
                    sp.Handshake = Handshake.None;
                    sp.DtrEnable = true;
                    sp.ReadTimeout = 500;
                    try
                    {
                        sp.Open();
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Serial port is already open or access is denied
                        continue;
                    }
                    catch (InvalidOperationException)
                    {
                        // The specified port on the current instance of the SerialPort is already open
                        continue;
                    }
                    catch (IOException)
                    {
                        continue;
                    }

                    // Wait for the device to initialize
                    Thread.Sleep(2000);

                    sp.NewLine = "\r\n";
                    this.serialPort = sp;
                    string response = string.Empty;
                    this.serialPort.WriteLine("version");
                    try
                    {
                        response = this.serialPort.ReadLine();
                    }
                    catch (TimeoutException)
                    {
                        sp.Dispose();
                        continue;
                    }

                    if (response == (HmdKitVersion + HmdKitShieldType))
                    {
                        this.isPresent = true;
                        return true;
                    }
                    else
                    {
                        sp.Dispose();
                    }
                }

                this.serialPort = null;
                this.isPresent = false;
                return false;
            }
        }