public void DisconnectSerialPort()
 {
     this.StopCheckingConnectionStatus();
     try
     {
         if (this._DeviceSerialPort.IsOpen == true)
         {
             this._DeviceSerialPort.Close();
             SerialPortCombo.Enabled = true;
             ConnectButton.Text      = "Co&nnect";
             RefreshButton.Enabled   = true;
             Log.PrintLog(this, "Disconnected from \"" + SerialPortCombo.GetItemText(SerialPortCombo.SelectedItem) + "\".", LogDetailLevel.LogRelevant);
             if (SerialPortType == PortType.Host)
             {
                 CurHostConnStatus = "DISCONNECTED";
                 if (OnHostConnectionStatusChange != null)
                 {
                     OnHostConnectionStatusChange("DISCONNECTED");
                 }
                 if (CheckDUTPresence)
                 {
                     if (OnDUTConnectionStatusChange != null)
                     {
                         OnDUTConnectionStatusChange("DISCONNECTED");
                     }
                 }
             }
             else if (SerialPortType == PortType.DUT)
             {
                 CurDUTConnStatus = "DISCONNECTED";
                 if (OnDUTConnectionStatusChange != null)
                 {
                     OnDUTConnectionStatusChange("DISCONNECTED");
                 }
             }
         }
     }
     catch
     {
         SerialPortCombo.Enabled = true;
         ConnectButton.Text      = "Co&nnect";
         RefreshButton.Enabled   = true;
         Log.PrintLog(this, "Serial port already disconnected.", LogDetailLevel.LogRelevant);
         RefreshPortList();
         if (SerialPortType == PortType.Host)
         {
             CurHostConnStatus = "DISCONNECTED";
             if (OnHostConnectionStatusChange != null)
             {
                 OnHostConnectionStatusChange("DISCONNECTED");
             }
             if (CheckDUTPresence)
             {
                 if (OnDUTConnectionStatusChange != null)
                 {
                     OnDUTConnectionStatusChange("DISCONNECTED");
                 }
             }
         }
         else if (SerialPortType == PortType.DUT)
         {
             CurDUTConnStatus = "DISCONNECTED";
             if (OnDUTConnectionStatusChange != null)
             {
                 OnDUTConnectionStatusChange("DISCONNECTED");
             }
         }
     }
 }
        private void ConnectPort()
        {
            ConnectionTime = true;
            if (ConnectButton.Text == "Co&nnect")
            {
                _DeviceSerialPort.PortName = ComPortInfoList[SerialPortCombo.SelectedIndex].Name;
                try
                {
                    _DeviceSerialPort.Open();
                }
                catch
                {
                    MessageBox.Show(_DeviceSerialPort.PortName + " - is in use.",
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (_DeviceSerialPort.IsOpen == true)
                {
                    if (AutoVerifyON)
                    {
                        if (!VerifyDeviceType())
                        {
                            if (SerialPortType == PortType.Host)
                            {
                                Log.PrintLog(this, "Expecting a MTK Host device.", LogDetailLevel.LogRelevant);
                            }
                            else if (SerialPortType == PortType.DUT)
                            {
                                Log.PrintLog(this, "Expecting a DUT.", LogDetailLevel.LogRelevant);
                            }
                            else if (SerialPortType == PortType.Anritsu)
                            {
                                Log.PrintLog(this, "Expecting an Anritsu tester.", LogDetailLevel.LogRelevant);
                            }

                            _DeviceSerialPort.Close();
                            ConnectButton.Enabled = true;
                            ConnectionTime        = false;
                            return;
                        }
                        ConnectionTime   = false;
                        ContinuePoll     = true;
                        DevicePollThread = new Thread(() => PollDevices());
                        DevicePollThread.Start();
                    }

                    SerialPortCombo.Enabled = false;
                    RefreshButton.Enabled   = false;
                    ConnectButton.Text      = "&Disconnect";
                    Log.PrintLog(this, "Connected to \"" + SerialPortCombo.GetItemText(SerialPortCombo.SelectedItem) + "\".", LogDetailLevel.LogRelevant);
                    if (CloseOnConnect)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
            else
            {
                DisconnectSerialPort();
            }
            ConnectionTime = false;
        }