Пример #1
0
        private void AddPorts()
        {
            if (_ComPortInfoList == null || _ComPortInfoList.Count == 0)
            {
                _ComPortInfoList = COMPortInfo.GetCOMPortsInfo();
            }
            Graphics ComboGraphics = SerialPortCombo.CreateGraphics();
            Font     ComboFont     = SerialPortCombo.Font;
            int      MaxWidth      = 0;

            foreach (COMPortInfo ComPort in _ComPortInfoList)
            {
                string s = ComPort.Name + " - " + ComPort.Description;
                SerialPortCombo.Items.Add(s);
                int VertScrollBarWidth = (SerialPortCombo.Items.Count > SerialPortCombo.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
                int DropDownWidth      = (int)ComboGraphics.MeasureString(s, ComboFont).Width + VertScrollBarWidth;
                if (MaxWidth < DropDownWidth)
                {
                    SerialPortCombo.DropDownWidth = DropDownWidth;
                    MaxWidth = DropDownWidth;
                }
            }
            if (SerialPortCombo.Items.Count > 0)
            {
                SerialPortCombo.SelectedIndex = 0;
            }
            Log.PrintLog(this, _ComPortInfoList.Count.ToString() + " serial ports found.", LogDetailLevel.LogRelevant);
        }
Пример #2
0
        private void AddPorts()
        {
            _ComPortInfoList = _mTKSerialPort.ComPortList;
            Graphics ComboGraphics = SerialPortCombo.CreateGraphics();
            Font     ComboFont     = SerialPortCombo.Font;
            int      MaxWidth      = 0;

            foreach (COMPortInfo ComPort in _ComPortInfoList)
            {
                string s = ComPort.Name + " - " + ComPort.Description;
                SerialPortCombo.Items.Add(s);
                int VertScrollBarWidth = (SerialPortCombo.Items.Count > SerialPortCombo.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
                int DropDownWidth      = (int)ComboGraphics.MeasureString(s, ComboFont).Width + VertScrollBarWidth;
                if (MaxWidth < DropDownWidth)
                {
                    SerialPortCombo.DropDownWidth = DropDownWidth;
                    MaxWidth = DropDownWidth;
                }
            }
            if (SerialPortCombo.Items.Count > 0)
            {
                SerialPortCombo.SelectedIndex = 0;
            }
        }
 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;
        }
        private async Task Connect(CheckBox box)
        {
            box.IsEnabled      = false;
            _context.State     = MainWindowContext.ConnectionState.Connecting;
            _context.ErrorInfo = "";

            var flag     = true;
            var progress = .0;

            _ = Task.Run(async() => {
                while (flag)
                {
                    _context.Progress = progress;
                    await Task.Delay(20).ConfigureAwait(false);
                }
                await Task.Delay(100).ConfigureAwait(false);
                _context.Progress = progress;
            });

            var port = SerialPortCombo.SelectedItem.ToString();
            {
                var temp = SerialPortCombo.SelectedItem;
                RefreshCombo();
                SerialPortCombo.SelectedItem = temp;
            }
            await Task.Run(async() => {
                try {
#if DEBUG
                    if (port != UITestString)
#endif
                    port = Methods.Initialize(port == AutoSelectString ? "" : port,
                                              out progress);

                    SerialPortCombo.Dispatch((it) => {
                        if (!it.Items.Contains(port))
                        {
                            it.Items.Add(port);
                        }
                        it.SelectedItem = port;
                    });

                    MainTab.Dispatch(it => {
                        if (it.SelectedContent is ITabControl control)
                        {
                            control.OnEnter();
                        }
                    });

                    _context.State = MainWindowContext.ConnectionState.Connected;
                } catch (Exception exception) {
                    _context.State     = MainWindowContext.ConnectionState.Disconnected;
                    _context.ErrorInfo = exception.Message;
                    return;
                } finally {
                    flag = false;
                    box.Dispatch((it) => it.IsEnabled = true);
                }

                _connecting = new CancellationTokenSource();
                await ToolFunctions.Handle(_context, _connecting).ConfigureAwait(false);
            }).ConfigureAwait(true);
        }