Beispiel #1
0
        /// <summary>
        /// 释放资源,继承IDisposable接口
        /// </summary>
        /// <param name="bDisposing"></param>
        protected void Dispose(bool bDisposing)
        {
            try
            {
                if (!m_isDisposed)
                {
                    if (bDisposing)
                    {
                        isStart = false;
                        //释放托管资源
                        m_commModule.CallbackTelegramEvent -= new CallbackTelegramEventHandler(TelegramMgmt);
                        m_commModule.Dispose();
                        DisposeICCardReader();

                        if (queueThread != null && queueThread.ThreadState == ThreadState.Running)
                        {
                            queueThread.Abort();
                            CLOGException.Trace(m_nPLCID, "WorkFlowLib", "Stop Thread(TelegramQueue) successfully");
                        }
                    }

                    this.m_isDisposed = true;
                }
            }
            catch
            { }
        }
Beispiel #2
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));
            }
        }