/// <summary>
        ///
        /// </summary>
        /// <param name="st"></param>
        /// <param name="cp"></param>
        internal static void Unbind(Station st, CommuniPort cp)
        {
            if (st == null)
            {
                throw new ArgumentNullException("st");
            }
            if (cp == null)
            {
                throw new ArgumentNullException("cp");
            }

            if (st.CommuniPort == null && cp.Station == null)
            {
                return;
            }

            Debug.Assert(
                st.CommuniPort != null &&
                cp.Station != null &&
                st.CommuniPort == cp &&
                cp.Station == st,
                "Unbind(Station, CommuniPort) assert fail");

            st.CommuniPort = null;
            cp.Station     = null;
        }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cp"></param>
 public void Add(CommuniPort cp)
 {
     BeforeAdd(cp);
     this.CommuniPorts.Add(cp);
     RegisterEvents(cp);
     OnCollectionChanged(ChangedType.Add, cp);
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        private void UnreginsterEvents(CommuniPort cp)
        {
            SocketCommuniPort sckcp = cp as SocketCommuniPort;

            sckcp.ClosedEvent   -= new EventHandler(sckcp_ClosedEvent);
            sckcp.ReceivedEvent -= new EventHandler(sckcp_ReceivedEvent);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="checkedTasks"></param>
        /// <param name="task"></param>
        /// <param name="cp"></param>
        private bool ExecuteTask(Task task, CommuniPort cp)
        {
            byte[] bytes        = task.GetSendBytes();
            bool   writeSuccess = cp.Write(bytes);

            if (writeSuccess)
            {
                // log send bytes: station - devicetype - bytes
                //
                LogSend(task, bytes);
                cp.Occupy(this.Timeout);
                task.LastExecute = DateTime.Now;

                // 2010-09-15
                //
                //OnExecuting(task);
                if (CommuniSoft.IsUseUISynchronizationContext)
                {
                    CommuniSoft.UISynchronizationContext.Post(
                        this.ExecutingCallback,
                        task);
                }
                else
                {
                    OnExecuting(task);
                }
            }
            return(writeSuccess);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="cp"></param>
 internal static void ClearBind(CommuniPort cp)
 {
     if (cp.Station != null)
     {
         Unbind(cp.Station, cp);
     }
 }
Example #6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="fromCommuniPort"></param>
 /// <param name="bytes"></param>
 public void Process(CommuniPort fromCommuniPort, byte[] bytes)
 {
     foreach (Opera op in this.Operas)
     {
         byte[][] bss = op.ReceivePart.DataFieldManager.Pick(bytes);
         if (bss != null)
         {
             foreach (byte[] bs in bss)
             {
                 ParseResult pr = op.ReceivePart.ToValues(bs);
                 if (pr.Success)
                 {
                     // process success event
                     //
                     //OnFDEvent(fromCommuniPort, op, pr);
                     if (CommuniSoft.IsUseUISynchronizationContext)
                     {
                         Xdgk.Communi.CommuniSoft.UISynchronizationContext.Post(
                             this.FDCallback, new object[] { fromCommuniPort, op, pr });
                     }
                     else
                     {
                         OnFDEvent(fromCommuniPort, op, pr);
                     }
                 }
             }
         }
     }
 }
Example #7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="fromCommuniPort"></param>
 /// <param name="op"></param>
 /// <param name="pr"></param>
 private void OnFDEvent(CommuniPort fromCommuniPort, Opera op, ParseResult pr)
 {
     if (FDEvent != null)
     {
         FDEventArgs e = new FDEventArgs(fromCommuniPort, op.DeviceType, op.Name, pr);
         FDEvent(this, e);
     }
 }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="state"></param>
        private void FDCallbackTarget(object state)
        {
            object[]    array = (object[])state;
            CommuniPort from  = (CommuniPort)array[0];
            Opera       op    = (Opera)array[1];
            ParseResult pr    = (ParseResult)array[2];

            OnFDEvent(from, op, pr);
        }
Example #9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="task"></param>
        public void DoitReceive(Task task)
        {
            // get match cp with task
            //
            CommuniPort cp = this.GetCommPort(task.Device);

            byte[] bytes = null;
            if (cp != null)
            {
                //cp.Read();
                //bytes = cp.ReceivedBytes;
                bytes = cp.Read();
            }
            //else
            //{
            //    //bytes = null;
            //    bytes = new byte[0];
            //}

            LogReceived(task, bytes);

            ParseResult           pr = task.ParseReceiced(bytes);
            TaskExecutedEventArgs e  = new TaskExecutedEventArgs(task, pr);

            // 2010-09-15
            //
            //OnExecuted(task, pr);
            if (CommuniSoft.IsUseUISynchronizationContext)
            {
                CommuniSoft.UISynchronizationContext.Post(
                    this.ExecutedCallback, e);
            }
            else
            {
                OnExecuted(e);
            }

            // process df
            //
            if ((bytes != null && bytes.Length > 0))
            {
                if (!pr.Success)
                {
                    this.CommuniSoft.FDManager.Process(cp, bytes);
                }
                else if (pr.Remain != null)
                {
                    this.CommuniSoft.FDManager.Process(cp, pr.Remain);
                }
            }

            if (!task.CanRemove)
            {
                this.Tasks.Add(task);
            }
        }
Example #10
0
        /// <summary>
        ///
        /// </summary>
        private void RegisterEvents(CommuniPort cp)
        {
            SocketCommuniPort sckcp = cp as SocketCommuniPort;

            if (sckcp != null)
            {
                sckcp.ClosedEvent   += new EventHandler(sckcp_ClosedEvent);
                sckcp.ReceivedEvent += new EventHandler(sckcp_ReceivedEvent);
            }
        }
Example #11
0
        public CommuniPortReceivedEventArgs(CommuniPort cp, byte[] bytes)
        {
            if (cp == null)
            {
                throw new ArgumentNullException("cp");
            }
            this._communiPort = cp;

            this._receivedBytes = bytes;
        }
Example #12
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cp"></param>
 public bool Remove(CommuniPort cp)
 {
     if (this.CommuniPorts.Remove(cp))
     {
         UnreginsterEvents(cp);
         OnCollectionChanged(ChangedType.Remove, cp);
         return(true);
     }
     return(false);
 }
Example #13
0
 /// <summary>
 /// 根据communiPort查找Station, 如找不到返回null
 /// </summary>
 /// <param name="communiPort"></param>
 /// <returns></returns>
 public Station FindStation(CommuniPort communiPort)
 {
     foreach (Station st in this.Stations)
     {
         if (communiPort.Match(st.CommuniType))
         {
             return(st);
         }
     }
     return(null);
 }
Example #14
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="cp"></param>
 private void OnCollectionChanged(ChangedType changedType, CommuniPort cp)
 {
     if (this.CollectionChanged != null)
     {
         CollectionChangedEventHandler temp = this.CollectionChanged;
         CollectionChangedEventArgs    e    = new CollectionChangedEventArgs(
             //ChangedType.Add,
             changedType,
             cp);
         temp(this, e);
     }
 }
Example #15
0
 /// <summary>
 /// check communiport upload data
 /// </summary>
 public void AAA()
 {
     for (int i = this.CommuniPorts.Count - 1; i >= 0; i--)
     {
         CommuniPort cp = this.CommuniPorts[i];
         if (!cp.IsOccupy)
         {
             byte[] bs = cp.Read();
             if (bs.Length > 0)
             {
                 this._communiSoft.FDManager.Process(cp, bs);
             }
         }
     }
 }
Example #16
0
        /// <summary>
        ///
        /// </summary>
        private void ProcessTasks()
        {
            TaskCollection checkedTasks = new TaskCollection();

            for (Task task = this.Tasks.Pick();
                 task != null;
                 task = this.Tasks.Pick())
            {
                bool canExecute     = false;
                bool executed       = false;
                bool executeSuccess = false;

                if (task.NeedExecute())
                {
                    CommuniPort cp = GetCommPort(task.Device);
                    canExecute = cp != null;
                    if (canExecute)
                    {
                        if (!cp.IsOccupy)
                        {
                            executeSuccess = ExecuteTask(task, cp);
                            executed       = true;
                        }
                    }
                    else
                    {
                        // 2010-09-15
                        //
                        //OnNotCommuniPortTask(task);
                        if (CommuniSoft.IsUseUISynchronizationContext)
                        {
                            CommuniSoft.UISynchronizationContext.Post(
                                this.NotCommuniPortTaskCallback, task);
                        }
                        else
                        {
                            this.OnNotCommuniPortTask(task);
                        }
                    }
                }

                AAA(checkedTasks, task, canExecute, executed, executeSuccess);
            }
            this._taskCollection = checkedTasks;
        }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="stations"></param>
        /// <param name="cp"></param>
        static internal bool Bind(StationCollection stations, CommuniPort cp)
        {
            if (stations == null)
            {
                throw new ArgumentNullException("stations");
            }

            if (cp == null)
            {
                throw new ArgumentNullException("cp");
            }

            if (cp is SocketCommuniPort)
            {
                return(Bind(stations, cp as SocketCommuniPort));
            }
            return(false);
        }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="st"></param>
        /// <param name="cp"></param>
        private static bool Bind(Station st, CommuniPort cp)
        {
            if (st == null)
            {
                throw new ArgumentNullException("st");
            }
            if (cp == null)
            {
                throw new ArgumentNullException("cp");
            }

            Debug.Assert(st.CommuniPort == null &&
                         cp.Station == null,
                         "Bind fail, st.CommuniPort or cp.Station not null");

            //Unbind(st, cp);
            st.CommuniPort = cp;
            cp.Station     = st;
            return(true);
        }
Example #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fromCommuniPort"></param>
        /// <param name="pr"></param>
        public FDEventArgs(CommuniPort fromCommuniPort, string deviceType, string operaName, ParseResult pr)
        {
            if (fromCommuniPort == null)
            {
                throw new ArgumentNullException("fromCommuniPort");
            }

            //if (opera == null)
            //throw new ArgumentNullException("opera");

            if (pr == null)
            {
                throw new ArgumentNullException("pr");
            }


            this._from       = fromCommuniPort;
            this._pr         = pr;
            this._deviceType = deviceType;
            this._operaName  = operaName;
        }
Example #20
0
 /// <summary>
 /// 删除集合中已经存在的,相同远程地址的SocketCommuniPort
 /// </summary>
 /// <param name="cp"></param>
 private void BeforeAdd(CommuniPort cp)
 {
     // TODO: allow same ip address option
     //
     if (cp is SocketCommuniPort)
     {
         SocketCommuniPort scp = cp as SocketCommuniPort;
         for (int i = this.CommuniPorts.Count - 1; i >= 0; i--)
         {
             CommuniPort cp2 = this.CommuniPorts[i];
             if (cp2 is SocketCommuniPort)
             {
                 SocketCommuniPort scp2  = cp2 as SocketCommuniPort;
                 IPEndPoint        ipep  = scp.RemoteEndPoint as IPEndPoint;
                 IPEndPoint        ipep2 = scp2.RemoteEndPoint as IPEndPoint;
                 if (ipep.Address.Equals(ipep2.Address))
                 {
                     Remove(cp2);
                 }
             }
         }
     }
 }