예제 #1
0
        // SeriPortForm Eventlari
        private void SeriPortForm_VisibleChanged(object sender, EventArgs e)
        {
            SerialPortForm seriPortForm = (SerialPortForm)sender;

            if (!seriPortForm.Visible)
            {
                string comName = seriPortForm.ReturnText;
                string comVal  = comName.Substring(comName.IndexOf("(COM") + 1, comName.IndexOf(")") - (comName.IndexOf("(COM") + 1));
                serialPort.SetPort(comVal, 115200);
                serialPort.Connect();
                seriPortForm.Dispose();
                if (lblStatus.InvokeRequired)
                {
                    lblStatus.Invoke(new Action(() => lblStatus.Text = "Status: Trying to connect..."));
                }
                else
                {
                    lblStatus.Text = "Status: Trying to connect...";
                }
                Helper.stopWatch.Start();
                Helper.threadIsConnect = new Thread(IsConnect);
                Helper.threadIsConnect.Start();
            }
        }
예제 #2
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            Helper.SerialPortDetect();
            List <string> comNames = Helper.comNames;

            if (!serialPort.IsConnected)
            {
                if (comNames.Count != 0)
                {
                    string device = string.Empty;
                    foreach (var item in comNames)
                    {
                        if (item.IndexOf("STM") != -1 || item.IndexOf("USB Seri Cihaz") != -1)
                        {
                            device = item;
                            break;
                        }
                    }

                    if (!string.IsNullOrEmpty(device))
                    {
                        string comVal = device.Substring(device.IndexOf("(COM") + 1, device.IndexOf(")") - (device.IndexOf("(COM") + 1));
                        serialPort.SetPort(comVal, 115200);
                        serialPort.Connect();
                        if (lblStatus.InvokeRequired)
                        {
                            lblStatus.Invoke(new Action(() => lblStatus.Text = "Status: Trying to connect..."));
                        }
                        else
                        {
                            lblStatus.Text = "Status: Trying to connect...";
                        }
                        Helper.stopWatch.Start();
                        Helper.threadIsConnect = new Thread(IsConnect);
                        Helper.threadIsConnect.Start();
                    }
                    else
                    {
                        var retVal = MessageBox.Show("The ST device was not found automatically!\n\nWould you like to choose the com port?", "Serial Port Connection", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (retVal.ToString() == "Yes")
                        {
                            Helper.SerialPortDetect();
                            SerialPortForm seriPortForm = new SerialPortForm(comNames);
                            seriPortForm.VisibleChanged += SeriPortForm_VisibleChanged;
                            seriPortForm.Show();
                        }
                        else
                        {
                            string text = "Status: Com ports connected to the computer were found but ST device was not found automatically!";
                            if (lblStatus.InvokeRequired)
                            {
                                lblStatus.Invoke(new Action(() => lblStatus.Text = text));
                            }
                            else
                            {
                                lblStatus.Text = text;
                            }
                        }
                    }
                }
                else
                {
                    if (lblStatus.InvokeRequired)
                    {
                        lblStatus.Invoke(new Action(() => lblStatus.Text = "Status: Com port connected to computer not found!"));
                    }
                    else
                    {
                        lblStatus.Text = "Status: Com port connected to computer not found!";
                    }
                    MessageBox.Show("The ST device not detected!", "Serial Port Connection", 0, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("The device is already connected!", "Serial Port Connection", 0, MessageBoxIcon.Information);
            }
        }