/// <summary> /// 連接狀態變化事件 /// </summary> /// <param name="isConnect"></param> void OnConnectStatus(bool isConnect) { if (this.ConnectStatusEvent != null) { PLCEventArgs arg = new PLCEventArgs(); arg.IsConnecting = isConnect; this.ConnectStatusEvent(arg); } }
void DoWork() { PLCEventArgs arg; string underlyingData = "";//PLC底層數據 while (m_Running) { try { lock (m_SyncLock) { int stopState = 0; int realCounter = 0;//PLC實際計數,未減去清零后的計數 m_ProductionSpeed = m_Reader.GetProductionSpeed();//生產速度 m_MachineSpeed = m_Reader.GetMachineSpeed();//機台速度 ReturnInfo ri = m_Reader.GetCounter();//獲取機台實際計數 int.TryParse(ri.ReturnValue, out realCounter); underlyingData = ri.UnderlyingData; string logMsg = ""; if (realCounter < 0)//負數 { #region 返回機台實際計數為負數 //邏輯判斷,連續出現10次負數,IsLogicError值為True m_PLCLogicError.LogicPLCCounter(realCounter); if (m_PLCLogicError.IsLogicError) { arg = new PLCEventArgs(); arg.CurrentCounter = m_CurrentCounter; arg.RealCounter = realCounter; arg.State = -1; arg.ErrorMsg = "PLC計數負數"; // 連續出現10次負數的數據 arg.PLCErrorCnt = m_PLCLogicError.PLCErrorList; //m_PLCLogicError負數邏輯判斷,每連續10次負數,Cnt+1 arg.LogicErrorCnt = m_PLCLogicError.ErrorCnt; arg.PLCErrorType = ErrorTpye.MinusError; if (OnErrorPLCEvent != null) { OnErrorPLCEvent(arg); } } logMsg += "產量:" + ("PLC計數負數").PadRight(10); logMsg += "PLC計數:" + realCounter.ToString().PadRight(10); logMsg += "底層數據:" + underlyingData; m_PLCLog.WriteLog(logMsg); continue; #endregion } else//0、正整數 { #region 返回機台實際計數為零或者正整數 //獲取上次PLC清零后保存在txt中的計數 int counterAfterClear; counterAfterClear = m_PLCLog.ReadCounter(); if (realCounter >= 0 && realCounter >= counterAfterClear)/*PLC數量大於等於清零后的數量*/ { m_CurrentCounter = realCounter - counterAfterClear; } else { //邏輯判斷,連續出現10次PLC計數小於清零后的計數,IsLogicError值為True m_PLCLogicClsError.LogicClsCounter(realCounter, counterAfterClear); if (m_PLCLogicClsError.IsLogicError) { arg = new PLCEventArgs(); arg.CurrentCounter = m_CurrentCounter; arg.RealCounter = realCounter; arg.State = -1; arg.ErrorMsg = "PLC計數小於清零后第一次的計數"; // 連續出現10次PLC計數小於清零后的計數集合 arg.PLCErrorCnt = m_PLCLogicClsError.PLCErrorList; //邏輯判斷,每連續10次PLC計數小於清零后的計數,Cnt+1 arg.LogicErrorCnt = m_PLCLogicClsError.ErrorCnt; arg.PLCErrorType = ErrorTpye.LessThenClsCnt; if (OnErrorPLCEvent != null) { OnErrorPLCEvent(arg); } } logMsg += "產量:" + ("PLC計數異常").PadRight(10); logMsg += "PLC計數:" + realCounter.ToString().PadRight(10); logMsg += "PLC清零后第一次計數:" + counterAfterClear.ToString().PadRight(10); logMsg += "底層數據:" + underlyingData; m_PLCLog.WriteLog(logMsg); continue; } stopState = StopFacade(m_CurrentCounter, m_PreCounter); m_PreCounter = m_CurrentCounter; m_PLCOnline = true; arg = new PLCEventArgs(); arg.State = stopState; arg.ProductionSpeed = m_ProductionSpeed; arg.CurrentCounter = m_CurrentCounter; arg.UnderlyingData = underlyingData; arg.MachineSpeed = m_MachineSpeed; arg.RealCounter = realCounter; //Add by Jerryxiao on 2014-06-26 測試用 logMsg += "產量:" + m_CurrentCounter.ToString().PadRight(10); logMsg += "PLC計數:" + realCounter.ToString().PadRight(10); logMsg += "底層數據:" + underlyingData; m_PLCLog.WriteLog(logMsg); #endregion } } if (arg.State == 0) { if (OnPLCEvent != null) { OnPLCEvent(arg); } } else if (arg.State == 1) { if (OnStopPLCEvent != null) { OnStopPLCEvent(arg); } } } catch (Exception ex) { m_PLCOnline = false; arg = new PLCEventArgs(); arg.PLCErrorType = ErrorTpye.TimeOutError; arg.CurrentCounter = m_CurrentCounter; arg.State = -1; arg.ErrorMsg = ex.Message; if (OnErrorPLCEvent != null) { OnErrorPLCEvent(arg); } } Thread.Sleep(m_Interval); } }