/// <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; }
/// <summary> /// /// </summary> /// <param name="cp"></param> public void Add(CommuniPort cp) { BeforeAdd(cp); this.CommuniPorts.Add(cp); RegisterEvents(cp); OnCollectionChanged(ChangedType.Add, cp); }
/// <summary> /// /// </summary> private void UnreginsterEvents(CommuniPort cp) { SocketCommuniPort sckcp = cp as SocketCommuniPort; sckcp.ClosedEvent -= new EventHandler(sckcp_ClosedEvent); sckcp.ReceivedEvent -= new EventHandler(sckcp_ReceivedEvent); }
/// <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); } }
/// <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); } } } } } }
/// <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); } }
/// <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); }
/// <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); } }
/// <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); } }
public CommuniPortReceivedEventArgs(CommuniPort cp, byte[] bytes) { if (cp == null) { throw new ArgumentNullException("cp"); } this._communiPort = cp; this._receivedBytes = bytes; }
/// <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); }
/// <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); }
/// <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); } }
/// <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); } } } }
/// <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; }
/// <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); }
/// <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); }
/// <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; }
/// <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); } } } } }