RFID設備管理C#客戶端學習筆記之5:Timer實時發送指令,存日誌文件

利用Timer控件能夠自動發送指令到讀寫器,而自動監控固然就須要將指定的信息存儲到日誌文件中以方便咱們查看其某時刻的狀態。咱們設定每一秒讀寫器返回一次識別結果,下側的statusStripLabel控件顯示每次的事件添加結果。
c#

爲這個控件添加Text改變事件,每次改變都寫此時的文本信息到log.txt中就ok啦,像下面這樣:數組

圖1 程序在服務器機器(Windows Server 2012操做系統)上運行服務器

圖2 日誌文件

this

一、爲timer1添加Tick事件

/// <summary>
        /// 每隔1秒自動發送識別指令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                string s = cb_readerList.Text;//發送給誰
                string cmd = cmds["設備識別"];
                Debug.WriteLine(DateTime.Now + "自動發送:" + cmd);
                byte[] buff = new byte[20];
                string[] str = cmd.Split(' ');
                //將byte數組轉化爲16進制
                int k = 0;
                foreach (string item in str)
                {
                    if (item.Trim() != "")
                    {
                        buff[k++] = byte.Parse(item, System.Globalization.NumberStyles.HexNumber);
                    }
                }

                if (cb_readerList.Items.Count == 0)
                {
                    toolStripStatusLabel1.Text = DateTime.Now + "沒有讀寫器鏈接到";
                    timer1.Stop();
                    //重啓服務                    
                    this.ServerStop();
                    Thread.Sleep(2000);
                    this.ServerStart();
                    return;
                }
                else
                {
                    this.mySend(s, buff);
                }                
            }
            catch (Exception ex)
            {                
                toolStripStatusLabel1.Text = "timer1_Tick中的異常:" + ex;
            }
        }

實際運行的時候發現,讀寫器可能會忽然斷開鏈接,而後再次連上。這時候ip不變,端口變了。向已經斷開鏈接的客戶發送指令會出現spa

ObjectDisposedException異常,因此咱們須要及時移除失效的coonSocket和thread
操作系統

/// <summary>
        /// 向讀寫器發送數據
        /// </summary>
        /// <param name="s">reader list的值</param>
        /// <param name="buff">發送的內容</param>
        private void mySend(string s,byte[] buff)
        {                            
            if (s != "全部讀寫器")
            {
                try
                {
                    dicSocket[s].Send(buff, buff.Length, SocketFlags.None);//向單個客戶端發送信息 
                }
                catch (ObjectDisposedException)
                {
                    //出現此種異常就直接刪除之:System.ObjectDisposedException: 沒法訪問已釋放的對象。
                    dicSocket[s].Close();
                    dicThread[s].Abort();
                    dicSocket.Remove(s);
                    dicThread.Remove(s);
                    this.cb_readerList.Items.Remove(s);
                    label1.Text = "在線讀寫器數:" + Convert.ToString(cb_readerList.Items.Count);
                }
                catch (Exception)
                {
                    toolStripStatusLabel1.Text = DateTime.Now + ":發送出現其餘異常";
                    throw;
                }
            }
            else
            {
                foreach (string i in cb_readerList.Items)
                {
                    try
                    {
                        dicSocket[i].Send(buff, buff.Length, SocketFlags.None);//向單個客戶端發送信息 
                    }
                    catch (ObjectDisposedException)
                    {
                        //出現此種異常就直接刪除之:System.ObjectDisposedException: 沒法訪問已釋放的對象。
                        dicSocket[i].Close();
                        dicThread[i].Abort();
                        dicSocket.Remove(i);
                        dicThread.Remove(i);
                        this.cb_readerList.Items.Remove(i);
                        label1.Text = "在線讀寫器數:" + Convert.ToString(cb_readerList.Items.Count);
                    }
                    catch (Exception)
                    {
                        toolStripStatusLabel1.Text = DateTime.Now + ":發送出現其餘異常";
                        throw;
                    }
                }
                    
            }

二、爲toolStripStatusLabel1添加Text變化事件

/// <summary>
        /// 狀態欄文本改變時,將記錄存儲到txt中
        /// </summary>
        private void toolStripStatusLabel1_TextChanged(object sender, EventArgs e)
        {
            //ref:寫入文本文件 https://msdn.microsoft.com/zh-cn/library/8bh11f1k.aspx
            string filePath = DateTime.Now.ToString("yyyy-MM-dd") + "log.txt";//日誌文件以天爲單位
            if (File.Exists(filePath))
            {
                using (StreamWriter sw = new StreamWriter(filePath, true))
                {
                    sw.WriteLine(toolStripStatusLabel1.Text);
                }
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine(toolStripStatusLabel1.Text);
                }
            }
        }

是的,log文件的名稱是按當天日期命名,運行24小時後,log文件通常會在3000kb以上。日誌

就醬~
code

相關文章
相關標籤/搜索