public void Init() { try { // System.Timers.Timer tmpTimer = new System.Timers.Timer(5000); //Timer的时间间隔为1000毫秒 = 1s // tmpTimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件 // tmpTimer.AutoReset = true;//设置是执行一次(false)还是一直执行(true) // tmpTimer.Elapsed += new System.Timers.ElapsedEventHandler(tmpTimer_Elapsed); // m_PLCServer = CCommonMethods.myMethods.GetCommModule(m_nWareHouse); if (null == m_PLCServer) { CLOGException.Trace("WorkFlowLib.CWorkFlow.InitialTelegramMgmt", "获取通信模式块失败"); return; //获取通信模式块失败,请查看配置文件是否正确 } // add by wanxiaona20150728 if (!CConfigManagement.myPara.HashAllPLCItemAdress.ContainsKey(m_nWareHouse)) { CLOGException.Trace("WorkFlowLib.CWorkFlow.InitialTelegramMgmt", "无效库区号"); return; } // add by wanxiaona20150728 // lock (m_commModule) //{ m_PLCServer.Initialize((List <string>)CConfigManagement.myPara.HashAllPLCItemAdress[m_nWareHouse], false); bool bIsSended = false; bool bIsACKReceived = false; while (!bIsSended || !bIsACKReceived) { bIsSended = SendData(); Int16[] data = (Int16[])m_objData; if (bIsSended) { int nMillisecond = 0; while (nMillisecond < 10000) { short recvBuffer = 0; bool isOK = m_PLCServer.Read(ref recvBuffer, 1); if (isOK) { if (recvBuffer == 9999) { Int16[] ackTelegram = null; m_PLCServer.Read(ref ackTelegram, 0);// 读取PLC回应ACK报文 bIsACKReceived = CBaseMethods.MyBase.IsPLCAck((Int16[])data, ackTelegram); if (bIsACKReceived) { //更新PLC发送缓冲区标志位为0 if (m_PLCServer.Write(0, 1)) { ThreadPool.QueueUserWorkItem(new WaitCallback(ReWritePLCFlag)); } //接收报文ack成功 CCommonMethods.myMethods.InsertTelegramLog(ackTelegram, 0); break; } else { StringBuilder sb = new StringBuilder(); Int16[] dataTmp = (Int16[])ackTelegram; sb.Append("EMS:" + dataTmp[0] + "|"); sb.Append("PLC:" + dataTmp[1] + "|"); sb.Append("TeleType:" + dataTmp[2] + "|"); sb.Append("EMSSubType:" + dataTmp[3] + "|"); sb.Append("PLCSubType:" + dataTmp[4] + "|"); sb.Append("DeviceID:" + dataTmp[6] + "|"); sb.Append("ID:" + dataTmp[48]); string telegram = sb.ToString(); CLOGException.Trace(m_nWareHouse, "WorkFlowLib.TelegramHandler", "Send telegram failed:" + telegram); } } } Thread.Sleep(1000);//4000 nMillisecond += 1000; } if (bIsACKReceived) { break; } } } CLOGException.Trace(m_nWareHouse, "WorkFlowLib.TelegramHandler", "***************************WorkFlowLib.CTelegramHandler.Init*****************************"); m_PLCServer.Dispose(); } catch (Exception ex) { CLOGException.Trace("AbstractActionLib.CTelegramHandler.Init 异常", CBaseMethods.MyBase.GetExceptionInfo(ex)); } }
/// <summary> /// 构造函数 /// </summary> public void Init(Dictionary <int, CLedShower> dicLedDevice) { try { //CLOGException.Trace("——————WorkFlowLib.CWorkFlow.Init "); if (!CConfigManagement.myPara.DicAllDeviceID.ContainsKey(m_nPLCID)) { CLOGException.Trace("WorkFlowLib.CWorkFlow.Init", "HashAllDeviceID-无效PLCID"); return;//无效PLCID } m_telegramParser = new CTelegramParse(); m_struTelegram = new struTelegram(); m_soundAction = new CSoundAction(); m_modifyDBAction = new CModifyDBAction(); m_sendTelegramAction = new CSendTelegramAction(); m_taskTrigger = new CTaskTriggerMgmt(); m_taskTrigger.SetTaskTrigger(m_nPLCID); m_dictHallID = new Dictionary <int, struHallEquips>(); if (!CConfigManagement.myPara.DicHallIDDictionary.ContainsKey(m_nPLCID)) { CLOGException.Trace("WorkFlowLib.CWorkFlow.Init", "HashHallIDDictionary-无效PLCID"); return; //无效PLCID } m_dictHallID = (Dictionary <int, struHallEquips>)CConfigManagement.myPara.DicHallIDDictionary[m_nPLCID]; //.GetHallIDDictionary(m_nPLCID); if (!CConfigManagement.myPara.HashAllPLCItemAdress.ContainsKey(m_nPLCID)) { CLOGException.Trace("WorkFlowLib", "HashAllPLCItemAdress-无效PLC ID"); return;//无效PLCID } m_commModule = CCommonMethods.myMethods.GetCommModule(m_nPLCID); if (null == m_commModule) { CLOGException.Trace(m_nPLCID, "WorkFlowLib", "Get communication module failed"); return; //获取通信模式块失败,请查看配置文件是否正确 } m_commModule.Initialize((List <string>)CConfigManagement.myPara.HashAllPLCItemAdress[m_nPLCID]); m_commModule.Write(0, 1);//更新PLC发送缓冲区标志位为0 m_dicSound = new Dictionary <int, CSound>(); foreach (int hallID in (List <int>)CConfigManagement.myPara.HashHallDeviceID[m_nPLCID]) { //获取语音模块类型的对应类名及相应参数 string strClassName = string.Format("CSound{0}", CConfigManagement.myPara.AudioAPI); CSound sound = new CSound(); if (string.IsNullOrWhiteSpace(strClassName)) { CLOGException.Trace(m_nPLCID, "IEG_AP_Proc", "Get AudioAPI name failed"); return; //获取语音模式块失败,使用默认 } // 配置 SpeechSynthesizer 对象以发送输出到默认音频设备。 sound = (CSound)Assembly.Load("SoundManagementLib").CreateInstance("SoundManagementLib." + strClassName, false, BindingFlags.Default, null, null, null, null); if (null == sound) { CLOGException.Trace(m_nPLCID, "IEG_AP_Proc", "Failed to new sound object, strClassName= " + strClassName); return; } m_dicSound.Add(hallID, sound); } m_dicLEDDevice = dicLedDevice; } catch (Exception ex) {//打印日志 CLOGException.Trace(m_nPLCID, "WorkFlowLib.CWorkFlow.Init 异常", CBaseMethods.MyBase.GetExceptionInfo(ex)); } }