Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        string[] portlar = SerialPort.GetPortNames();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string port in portlar)
            {
                comboBox1.Items.Add(port);
                comboBox1.SelectedIndex = 0;

            }
            comboBox2.Items.Add("4800");
            comboBox2.Items.Add("9600");
            comboBox2.SelectedIndex = 1;
            label2.Text = "Not connected";


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                string sonuc = serialPort1.ReadLine();
                string[] pot = sonuc.Split(',');
                label2.Text = sonuc + " ";
                textBox1.Text = pot[0];
                textBox2.Text = pot[1];
                textBox3.Text = pot[2];
               textBox4.Text = pot[3];
                serialPort1.DiscardInBuffer();

                int b;
                int d;
                int f;
                int g;

                bool result = Int32.TryParse(pot[0], out b);
                bool answer = Int32.TryParse(pot[1], out d);
                bool result1 = Int32.TryParse(pot[2], out f);
                bool answer1 = Int32.TryParse(pot[3], out g);

               
                if (result)
                {
                    if (b < 224)
                    {

                        button3.BackColor = Color.Red;
                        label6.Text = " OCCUPIED";
                       
                    }
                    else
                    {
                        button3.BackColor = Color.Lime;
                        label6.Text = " AVAILABLE";
                    }
                }

                if (answer)
                {
                    if (d < 224)
                    {

                        button4.BackColor = Color.Red;
                        label8.Text = " OCCUPIED";
                        
                    }
                    else
                    {
                        button4.BackColor = Color.Lime;
                        label8.Text = " AVAILABLE";
                        
                    }
                }
                if (result1)
                {
                    if (f < 400)
                    {

                        button5.BackColor = Color.Red;
                        label10.Text = " OCCUPIED";
                    }
                    else
                    {
                        button5.BackColor = Color.Lime;
                        label10.Text = " AVAILABLE";

                    }
                }

               if (answer1)
                {
                    if (g < 400)
                    {

                        button6.BackColor = Color.Red;
                        label12.Text = " OCCUPIED";

                    }
                    else
                    {
                        button6.BackColor = Color.Lime;
                        label12.Text = "AVAILABLE";

                    }
                }

            }


            catch (Exception ex) {
                MessageBox.Show(ex.Message);
                timer1.Stop();

                
            
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            if (serialPort1.IsOpen == false) {
                if(comboBox1.Text==" ")

                return;
                serialPort1.PortName = comboBox1.Text;
                serialPort1.BaudRate = Convert.ToInt16(comboBox2.Text);
                try
                {
                    serialPort1.Open();
                    label2.Text = "baglanti acik ";


                }
                catch (Exception hata) { 
                
                    MessageBox.Show("Hata:"+ hata.Message);
                     }
            }

          


            else
            {
                label2.Text = "baglanti kapali";

            }

            }

        private void button2_Click(object sender, EventArgs e)
        {
        timer1.Stop();
        serialPort1.DiscardInBuffer();

            if (serialPort1.IsOpen==true)

            {
            serialPort1.Close();
            label2.Text="baglanti kapali";
            
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Close();
                

            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {

        }
        }
    }


What I have tried:

l wrote my code in C# and l am having troubles because it reacts very slowly lam reading values from sensors to use on my c# project.When l execute my code its very slow and if l try to stop it with button 2 it not working.Is there a way in which l can improve my code hand the speed
Posted
Updated 1-May-18 8:19am
Comments
[no name] 1-May-18 13:05pm    
Stop reading the Serial port by the help of a timer. Have a look here: Serial Comms in C# for Beginners[^]

Btw: comparing boolean values like this "thisBool == true" or "thisBool == false", looks like a lack of knowledge what booleans are ;)
Member 13806674 1-May-18 14:14pm    
Thank you very much it worked.
[no name] 1-May-18 14:27pm    
You are very welcome, glad it helped

what is serialport1? where you get it?
 
Share this answer
 
Comments
Dave Kreskowiak 27-Jul-24 12:04pm    
You posted this an ANSWER to an old question and will be ignored.
Answered only to remove from unanswered queue: solved by OP / 0x01AA
 
Share this answer
 
Comments
[no name] 1-May-18 14:53pm    
Thanks for tidying up ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900