private void bt_connect_Click(object sender, EventArgs e)
 {
     try
     {
         if (lb_ComState.Text != "Connect !")
         {
             string comselect = cb_COM.Text;
             Port1.PortName = comselect;
             Port1.Open();
             lb_ComState.Text = "Connect !";
             bt_connect.Text  = "Disconnect ...";
             cb_COM.Enabled   = false;
         }
         else
         {
             Port1.Close();
             lb_ComState.Text = "Disconnect ...";
             bt_connect.Text  = "Connect !";
             cb_COM.Enabled   = true;
         }
     }
     catch
     {
         MessageBox.Show("Cannot connect to this COM port !");
     }
 }
Example #2
0
        //Logout method
        private void GoBackToLogin()
        {
            Login loginForm = new Login();

            loginForm.Show();
            Port1.Close();
            this.Close();
        }
Example #3
0
 private void bClose_Click(object sender, EventArgs e)
 {
     if (Port1.IsOpen)
     {
         Port1.Close();
     }
     bOpen.Enabled      = true;
     bClose.Enabled     = false;
     bSend.Enabled      = false;
     cbPort.Enabled     = true;
     cbBaud.Enabled     = true;
     cbDataBits.Enabled = true;
     cbParity.Enabled   = true;
     cbStopBits.Enabled = true;
 }
Example #4
0
        //Runs in the background and updates temperature, if Arduino is available
        private void updateTemperature()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(10000);

                    //Gets all available ports on the computer and tries to connect to it
                    foreach (string port in SerialPort.GetPortNames())
                    {
                        try
                        {
                            Port1.PortName = port;
                            Port1.Open();
                            Port1.ReadTimeout = 100;
                            Port1.ReadLine();
                            break;
                        }
                        catch { Port1.Close(); }
                    }

                    if (!Port1.IsOpen)
                    {
                        continue;
                    }

                    try
                    {
                        while (true)
                        {
                            string temperature;
                            temperature = Port1.ReadLine();
                            temperatureBox.Invoke(new Action(() => temperatureBox.Text = $"The tempeature is: {temperature.Replace("\r", "")} ℃"));
                        }
                    }
                    catch { continue; }
                }
            });
        }
Example #5
0
        private void bOpen_Click(object sender, EventArgs e)
        {
            if (Port1.IsOpen)
            {
                Port1.Close();
            }

            string s;

            // port
            Port1.PortName = cbPort.Text;
            s = Port1.PortName.ToUpper();
            // baud rate
            Port1.BaudRate = Convert.ToInt32(cbBaud.Text, 10);
            s += "@" + Port1.BaudRate.ToString();
            // data bits
            switch (cbDataBits.SelectedIndex)
            {
            case 0: Port1.DataBits = 7; s += ",7"; break;

            case 1: Port1.DataBits = 8; s += ",8"; break;

            case 2: Port1.DataBits = 9; s += ",9"; break;
            }
            // parity
            switch (cbParity.SelectedIndex)
            {
            case 0: Port1.Parity = System.IO.Ports.Parity.None; s += "N"; break;

            case 1: Port1.Parity = System.IO.Ports.Parity.Odd; s += "O"; break;

            case 2: Port1.Parity = System.IO.Ports.Parity.Even; s += "E"; break;

            case 3: Port1.Parity = System.IO.Ports.Parity.Mark; s += "M"; break;

            case 4: Port1.Parity = System.IO.Ports.Parity.Space; s += "S"; break;
            }
            // stop bits
            switch (cbStopBits.SelectedIndex)
            {
            case 0: Port1.StopBits = System.IO.Ports.StopBits.None; s += "0"; break;

            case 1: Port1.StopBits = System.IO.Ports.StopBits.One; s += "1"; break;

            case 2: Port1.StopBits = System.IO.Ports.StopBits.OnePointFive; s += "1.5"; break;

            case 3: Port1.StopBits = System.IO.Ports.StopBits.Two; s += "2"; break;
            }

            try
            {
                Port1.Open();
                bOpen.Enabled      = false;
                bClose.Enabled     = true;
                bSend.Enabled      = true;
                cbPort.Enabled     = false;
                cbBaud.Enabled     = false;
                cbDataBits.Enabled = false;
                cbParity.Enabled   = false;
                cbStopBits.Enabled = false;
                sStatus.Text       = s;
            }
            catch
            {
                MessageBox.Show("Unable to open port: " + Port1.PortName, "Port open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                sStatus.Text = "Port not opended";
            }
        }