使用C#从串口读取多个数据值 [英] Reading multiple data value from the serial port with C#

查看:689
本文介绍了使用C#从串口读取多个数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
        {

        }
        }
    }





我尝试了什么:



l在C#中编写了我的代码,我遇到了麻烦,因为它对传感器读数值的反应很慢在我的c#项目上使用。当我执行我的代码时它非常慢,如果我试图用按钮2停止它它不工作。有一种方法,我可以提高我的代码手速度



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

推荐答案

仅回答从未答复的队列中删除:由OP / 0x01AA解决
Answered only to remove from unanswered queue: solved by OP / 0x01AA


这篇关于使用C#从串口读取多个数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆