Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        /// <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));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 分库发送报文
        /// </summary>
        /// <param name="data"></param>
        /// <param name="wareHouse"></param>
        /// <returns></returns>
        private bool SendData()
        {
            if (null == m_PLCServer)
            {
                return(false);
            }
            bool isok = false;

            if (m_PLCServer.IsConnected)
            {
                #region add 20150229 在发送报文前,先判断PLC发送缓冲区是否为空,如果是ACK,没有办法再读取了,则强制清0,以让PLC发送缓冲区处于空闲状态
                bool  isACK      = false;
                Int16 recvBuffer = 0;
                bool  isExit     = m_PLCServer.Read(ref recvBuffer, 1);// 读取PLC发送缓冲区标志位
                if (isExit)
                {
                    if (recvBuffer == 9999)
                    {
                        short[] recvTele = new short[50];
                        bool    isRecvOK = m_PLCServer.Read(ref recvTele, 0);
                        if (isRecvOK)
                        {
                            if (recvTele[4] == 9999)
                            {
                                isACK = true;
                                Thread.Sleep(400);
                            }
                        }
                    }
                    if (isACK)
                    {
                        recvBuffer = 0;
                        isExit     = m_PLCServer.Read(ref recvBuffer, 1);
                        if (isExit)
                        {
                            if (recvBuffer == 9999)
                            {
                                m_PLCServer.Write(0, 1);
                            }
                        }
                    }
                }
                #endregion
                Int16 wflag   = 0;
                bool  wisgood = m_PLCServer.Read(ref wflag, 3);    // 读取PLC接收缓冲区标志位

                if (wisgood)                                       // 读取数据有效Good
                {
                    if ((Int16)wflag == 0)                         // 数据已经被PLC处理(PLC数据为空,此时可以填充数据)
                    {
                        wisgood = m_PLCServer.Write(m_objData, 2); // 向OPC写数据

                        if (wisgood)                               // 写入数据正常完成
                        {
                            wflag   = 9999;
                            wisgood = m_PLCServer.Write(wflag, 3);// 更新PLC接收缓冲区标志位为9999
                            if (!wisgood)
                            {
                                ThreadPool.QueueUserWorkItem(new WaitCallback(ReWriteEMSFlag));
                            }
                            isok = wisgood;
                        }
                    }
                }
                if (isok)
                {
                    //发送报文成功
                    CCommonMethods.myMethods.InsertTelegramLog(m_objData, 1);
                }
            }
            else
            {
                m_PLCServer.ReConnectServer();
            }

            return(isok);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 发送报文并判断是否重发,如果重发次数不超过m_retryCount
        /// </summary>
        /// <param name="data"></param>
        /// <param name="wareHouse"></param>
        private void DealSendTelegram_que(object data, int wareHouse)
        {
            if (null == m_PLCServer)
            {
                return;
            }
            int  count       = m_retryCount;// 重发次数
            bool bSendResult = false;
            bool bACKResult  = false;

            while (0 < count--)
            {
                bSendResult = SendData(data, wareHouse);
                if (bSendResult)
                {
                    int nMillisecond = 0;
                    while (nMillisecond < 3000)
                    {
                        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报文

                                bACKResult = CBaseMethods.MyBase.IsPLCAck((Int16[])data, ackTelegram);
                                if (bACKResult)
                                {
                                    //更新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 strTelegram = sb.ToString();
                                    CLOGException.Trace(wareHouse, "AbstractActionLib.CTelegramSender.DealSendTelegram", "ACK错误,报文:" + strTelegram);
                                }
                            }
                        }
                        Thread.Sleep(50);//4000
                        nMillisecond += 50;
                    }
                    if (bACKResult)
                    {
                        break;
                    }
                }
            }

            if (!bSendResult || !bACKResult)
            {
                CTelegramHandler telegramHandler = new CTelegramHandler(wareHouse, data);

                ThreadPool.QueueUserWorkItem(new WaitCallback(telegramHandler.Init));
            }
        }