zoukankan      html  css  js  c++  java
  • C#SerialPort实现串口控制继电器

    最近做了一个小系统,麻雀虽小五脏俱全呀,用到各种线程控制,串口控制等技术。其中串口控制最麻烦,因为继电器的响应很快,根据不同的转接口,返回的数据质量是不一样的,所以不能直接wirte,然后马上read,这样经常得到的效应状态是错误的。因此需要用到backgroundworker不停地read,校验数据成功后再设置成功状态标志,最后让timer定时获取改标志,成功后立即控制程序界面上的按钮等控件。

    public void openSerial()
            {
                try
                {
                    foreach (GlobaConfig config in GlobaConfigInstance.Instance)
                    {
                        if ((!config.SerialPort.IsOpen))
                        {
                            config.SerialPort.Open();
                            config.SerialPort.DiscardInBuffer();
                            config.SerialPort.DiscardOutBuffer();
                        }
                    }
                }
                catch(Exception ex)
                {
                    //MessageBox.Show("打开失败!" + ex.Message);
                    NotificationManager.Show(this, "打开失败!" + ex.Message,
                                    Color.Gold, 1000);
                    return;
                }
            }
    
    private void SendOpenDemand(byte port)
            {
                byte[] buffer = new byte[20];
                byte[] buffer2 = new byte[2];
                buffer[0] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
                buffer[1] = 5;
                buffer[2] = 0;
                buffer[3] = port;
                buffer[4] = 0xff;
                buffer[5] = 0;
                A101.GetCRC(buffer, 6, buffer2);
                buffer[6] = buffer2[0];
                buffer[7] = buffer2[1];
                if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                {
                    this.SerialSendFrame(buffer, 0, 8);
                }
                else
                {
                    //MessageBox.Show("亲,你要先打开串口哦!");
                }
            }
    
    
            private void SendCloseDemand(byte port)
            {
                byte[] buffer = new byte[20];
                byte[] buffer2 = new byte[2];
                buffer[0] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
                buffer[1] = 5;
                buffer[2] = 0;
                buffer[3] = port;
                buffer[4] = 0;
                buffer[5] = 0;
                A101.GetCRC(buffer, 6, buffer2);
                buffer[6] = buffer2[0];
                buffer[7] = buffer2[1];
                if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                {
                    this.SerialSendFrame(buffer, 0, 8);
                }
                else
                {
                    //MessageBox.Show("亲,你要先打开串口哦!");
                }
            }
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                byte[] buffer;
                while (true)
                {
                    try
                    {
    
                        if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                        {
                            int bytesToRead = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.BytesToRead;
                            buffer = new byte[0xff];
                            if (bytesToRead > 0)
                            {
                                int num2;
                                bytesToRead = 0x45;
                                GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.Read(buffer, 0, bytesToRead);
                                bool isValid = A101.ValidCRC(buffer, (byte)bytesToRead);
                                System.Diagnostics.Debug.WriteLine("isValid: " + isValid);
                                if (isValid)
                                {
                                    for (num2 = 0; num2 < bytesToRead; num2++)
                                    {
                                        this.SerialRecBuf[num2] = buffer[num2];
                                    }
    
                                    this.SerialRecFlag = true;
                                }
                                else
                                    this.SerialRecFlag = false;
    
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        
                        Console.WriteLine(ex);
                    }
                    Thread.Sleep(1);
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (this.SerialRecFlag)
                {
                    this.SerialRecFlag = false;
                    if (((this.SerialRecBuf[0] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[1] == 5)) && (this.SerialRecBuf[4] == 0))
                    {
                        this.BtnSendFlag = false;
                        if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                        {
                            Control[] controls = this.kryptonNavigator1.SelectedPage.Controls.Find("btnRelay" + kryptonNavigator1.SelectedIndex + this.SerialRecBuf[3], true);
                            if (controls.Length > 0)
                            {
                                KryptonCheckButton button = controls[0] as KryptonCheckButton;
                                button.Checked = false;
                                button.Tag = "断开";
                                button.Enabled = true;
                            }
                        }
                    }
                    else if (((this.SerialRecBuf[0] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[1] == 5)) && (this.SerialRecBuf[4] == 0xff))
                    {
                        this.BtnSendFlag = false;
    
                        if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                        {
                            Control[] controls = this.kryptonNavigator1.SelectedPage.Controls.Find("btnRelay" + kryptonNavigator1.SelectedIndex + this.SerialRecBuf[3], true);
                            if (controls.Length > 0)
                            {
                                KryptonCheckButton button = controls[0] as KryptonCheckButton;
                                button.Checked = true;
                                button.Tag = "闭合";
                                button.Enabled = true;
                            }
                        }
                    }
                    else if (((this.SerialRecBuf[0] == GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr) && (this.SerialRecBuf[1] == 1)) && (this.SerialRecBuf[2] == 4))
                    {
                        uint[] numArray = new uint[4];
                        uint resource = 0;
                        numArray[0] = this.SerialRecBuf[3];
                        numArray[1] = this.SerialRecBuf[4];
                        numArray[2] = this.SerialRecBuf[5];
                        numArray[3] = this.SerialRecBuf[6];
                        resource = (((numArray[3] << 0x18) | (numArray[2] << 0x10)) | (numArray[1] << 8)) | numArray[0];
    
                        if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                        {
                            foreach (Control control in this.kryptonNavigator1.SelectedPage.Controls[2].Controls)
                            {
                                KryptonCheckButton button = control as KryptonCheckButton;
                                if (this.GetRegBit(resource, (byte)control.TabIndex) == 0)
                                {
                                    button.Checked = false;
                                    button.Tag = "断开";
                                }
                                else
                                {
                                    button.Checked = true;
                                    button.Tag = "闭合";
                                }
                                button.Enabled = true;
                            }
                        }
                    }
    
                }
            }
    
            private void timer2_Tick(object sender, EventArgs e)
            {
                if (!this.BtnSendFlag)
                {
                    byte[] buffer = new byte[20];
                    byte[] buffer2 = new byte[2];
                    buffer[0] = GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].Addr;
                    buffer[1] = 1;
                    buffer[2] = 0;
                    buffer[3] = 0;
                    buffer[4] = 0;
                    buffer[5] = 0x1c;
                    A101.GetCRC(buffer, 6, buffer2);
                    buffer[6] = buffer2[0];
                    buffer[7] = buffer2[1];
                    System.Diagnostics.Debug.WriteLine(kryptonNavigator1.SelectedIndex + ":" + GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen);
                    if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.IsOpen)
                    {
                        this.SerialSendFrame(buffer, 0, 8);
                        this.timer1.Start();
                    }
                    else
                    {
                        if (GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].IsValid == true)
                        {
                            GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].IsValid = false;
                            try
                            {
                                GlobaConfigInstance.Instance[kryptonNavigator1.SelectedIndex].SerialPort.Open();
                                //修復退出的線程
                                //this.timer1 = new System.Windows.Forms.Timer();
                                this.timer1.Start();
                            }
                            catch (Exception ex)
                            {
                                NotificationManager.Show(this, "主机" + (kryptonNavigator1.SelectedIndex + 1) + "出现异常:" + ex.Message, 
                                    Color.Gold, 1000);
                                //MessageBox.Show("主机" + (kryptonNavigator1.SelectedIndex + 1) + "出现异常:" + ex.Message);
                            }
                        }
                    }
                }
            }
  • 相关阅读:
    站立会议(3)
    站立会议(2)
    站立会议(1)
    团队报告
    Laravel5.1 模型 --一对多关系
    jQuery 操作DOM
    Laravel5.1 模型 --一对一关系
    Laravel5.1 模型--查询作用域
    Laravel5.1 模型--ModelFactory
    jQuery 选择器
  • 原文地址:https://www.cnblogs.com/plain-heart/p/3408891.html
Copyright © 2011-2022 走看看