Exemple #1
0
        public void Send(SecsMessageBase msg, SecondarySecsMessageEventHandler callback)
        {
            msg.DeviceId = m_DeviceId;

            bool isPrimary = msg.NeedReply && ((msg.Function & 0x01) == 0x01);

            if (isPrimary)
            {
                //get new transaction id
                msg.TransactionId = GetNextTransactionId();

                //register T3
                lock (m_Locker)
                {
                    if (m_SecsTransaction.ContainsKey(msg.TransactionId))
                    {
                        m_SecsTransaction.Remove(msg.TransactionId);
                    }
                    m_SecsTransaction.Add(msg.TransactionId, msg);
                }

                if (callback != null)
                {
                    if (c_CustomSecondaryMessageCallback.ContainsKey(msg.TransactionId))
                    {
                        c_CustomSecondaryMessageCallback.Remove(msg.TransactionId);
                    }
                    c_CustomSecondaryMessageCallback.Add(msg.TransactionId, callback);
                }
            }


            //TraceSmlLog(msg, DirectionType.Sent);
            OnSending(msg, isPrimary);
            byte[] data = m_Parser.GetBytes(msg);
            ProtectedSend(data);
            //OnSent(msg);
            TraceSmlLog(msg, DirectionType.Sent);
        }
Exemple #2
0
        /// <summary>
        /// This function is called from inherited class
        /// </summary>
        /// <param name="data">SecsMessage in byte[]</param>
        protected void ProcessSecsMessageBytes(byte[] data)
        {
            SecsMessageBase msg = null;

            try
            {
                msg = m_Parser.ToSecsMessage(data);
            }
            catch (Exception ex)
            {
                //fire out event conversion error
                PoseEvent_ConversionErrored(new ConversionErrorEventArgs(ex, data));
                return;
            }

            TraceSmlLog(msg, DirectionType.Recv);

            bool isSecondary = (msg.Function & 0x01) == 0x00;

            OnReceiving(msg, isSecondary);
            //should put some flag for example :Cancel or abort
            OnReceived(msg);

            if (isSecondary)
            {
                SecsMessageBase priMsg = null;

                lock (m_Locker)
                {
                    if (m_SecsTransaction.ContainsKey(msg.TransactionId))
                    {
                        //get primary message
                        priMsg = m_SecsTransaction[msg.TransactionId];
                        //unregister transaction
                        m_SecsTransaction.Remove(msg.TransactionId);
                    }
                }

                if (priMsg != null)
                {
                    if (msg.Function == 0)
                    {
                        //primary transaction was abort
                        AbortPrimaryTransaction(msg);
                    }
                    else
                    {
                        //custom callback checking
                        if (c_CustomSecondaryMessageCallback.ContainsKey(priMsg.TransactionId))
                        {
                            //get callback from dictionary
                            SecondarySecsMessageEventHandler callback = c_CustomSecondaryMessageCallback[priMsg.TransactionId];
                            //remove callnack from dictionary
                            c_CustomSecondaryMessageCallback.Remove(priMsg.TransactionId);
                            //execute callback
                            callback(this, new SecondarySecsMessageEventArgs(priMsg, msg));
                        }
                        else
                        {
                            //should post as asynchronouse
                            PostEvent_ReceivedSecondaryMessage(new SecondarySecsMessageEventArgs(priMsg, msg));
                        }
                    }
                }
                else
                {
                    AbortUnknowTransaction(msg);
                }
            }
            else
            {
                //should post as asynchronouse
                PostEvent_ReceivedPrimaryMessage(new PrimarySecsMessageEventArgs(msg));
            }
        }