/// <summary> /// /// </summary> /// <param name="RemotHost">远程IP地址</param> /// <param name="index">本机端唯一识别地址</param> public LockClient(string RemotHost, int RemotPort, int LocalPort, T index) { Connect = false; this.remotPort = RemotPort; Index = index; remotHost = RemotHost; lockUdp = new Udp(LocalPort); lockUdp.GetStringArgs += lockUdp_GetStringArgs; lockUdp.Open(); thConnectRemot = new Thread(() => Conn()); thConnectRemot.IsBackground = true; thConnectRemot.Start(); }
Channel[] lockList; //每一路互锁 public LockMultiChannleMain(int MainPort, int AllChannelCount) { AllChannel = new List <Channel>(); nowStatueList = new GetStatueList[AllChannelCount]; testValue = new string[AllChannelCount]; lockList = new Channel[AllChannelCount]; testingValue = new string[AllChannelCount]; for (int i = 0; i < AllChannelCount; i++) { nowStatueList[i] = GetStatueList.收到请求; testValue[i] = ""; lockList[i] = new Channel(); testingValue[i] = ""; } lockUdp = new Udp(MainPort); lockUdp.GetStringArgs += lockUpd_GetStringArgs; lockUdp.Open(); }
private void lockUdp_GetStringArgs(Udp sender, ReciveString reciveArgs) { string value = reciveArgs.Value; string[] buff = value.Split(new string[] { "~~~" }, StringSplitOptions.None); LockMain <T> .GetStatueList statue = (LockMain <T> .GetStatueList)Num.ToInt(buff[0]); switch (statue) { case LockMain <T> .GetStatueList.收到请求: nowStatueList = SetStatueList.请求; getRemotValue = true; break; case LockMain <T> .GetStatueList.等待: SetRemotInfo(statue, "远程主机已收到请求,请等待分配执行"); break; case LockMain <T> .GetStatueList.允许执行互锁要求: SetRemotInfo(statue, "远程主机已允许执行互锁前的开关切换"); isPlease = false; switch (nowStatueList) { case SetStatueList.请求: if (SwitchOpen != null) { SwitchOpen(); } nowStatueList = SetStatueList.执行要求完毕; break; } break; case LockMain <T> .GetStatueList.正在执行互锁动作: SetRemotInfo(statue, "远程主机正在执行互锁动作"); if (buff.Length > 1) { if (GetRemotHostTestingValue != null) { GetRemotHostTestingValue(buff[1]); } } break; case LockMain <T> .GetStatueList.互锁动作执行完毕: if (isStart) { isStart = false; SetRemotInfo(statue, "远程操作完成"); if (GetRemotHostTestValue != null) { GetRemotHostTestValue(buff[1]); } if (SwitchClose != null) { SwitchClose(); } } break; case LockMain <T> .GetStatueList.互锁请求正常结束: SetRemotInfo(statue, "远程主机已收到请求,互锁请求正常结束"); nowStatueList = SetStatueList.除; isStop = false; isPlease = false; isStart = false; break; case LockMain <T> .GetStatueList.除请求成功: SetRemotInfo(statue, "远程主机已收到请求,互锁请求已删除"); nowStatueList = SetStatueList.除; isDel = false; isPlease = false; isStart = false; isStop = false; break; } }
private void lockUpd_GetStringArgs(Udp sender, ReciveString reciveArgs) { string value = reciveArgs.Value; string[] buff = value.Split(new string[] { "~~~" }, StringSplitOptions.None); T index = (T)(Object)(buff[0]); LockClient <T> .SetStatueList statue = (LockClient <T> .SetStatueList)Num.ToInt(buff[1]); switch (statue) { case LockClient <T> .SetStatueList.连接测试: lockUdp.Write(string.Format("{0}", (int)GetStatueList.收到请求), reciveArgs.RemoteHost, reciveArgs.RemotePort); break; case LockClient <T> .SetStatueList.请求: if (!lockList.Contains(index)) { lockList.Add(index); } if (lockList.Count > 0) { if ((string)(object)lockList[0] == (string)(object)index) { lockUdp.Write(string.Format("{0}", (int)GetStatueList.允许执行互锁要求), reciveArgs.RemoteHost, reciveArgs.RemotePort); } else { lockUdp.Write(string.Format("{0}", (int)GetStatueList.等待), reciveArgs.RemoteHost, reciveArgs.RemotePort); } } else { Error.Add("有添加的请求,但数组列为0", Environment.StackTrace); } break; case LockClient <T> .SetStatueList.执行要求完毕: if (lockList.Count > 0) { if ((string)(object)lockList[0] != (string)(object)index) { Error.Add(string.Format("动作完成的工位和允许执行的工位不是同一个,大问题。允许工位为:{0},执行工位为:{1}", lockList[0], index), Environment.StackTrace); return; } switch (nowStatueList) { case GetStatueList.收到请求: if (AllTestNeedOk != null) { AllTestNeedOk(); } nowStatueList = GetStatueList.正在执行互锁动作; lockUdp.Write(string.Format("{0}", (int)GetStatueList.正在执行互锁动作), reciveArgs.RemoteHost, reciveArgs.RemotePort); break; case GetStatueList.正在执行互锁动作: lockUdp.Write(string.Format("{0}~~~{1}", (int)GetStatueList.正在执行互锁动作, testingValue), reciveArgs.RemoteHost, reciveArgs.RemotePort); break; case GetStatueList.互锁动作执行完毕: lockUdp.Write(string.Format("{0}~~~{1}", (int)GetStatueList.互锁动作执行完毕, testValue), reciveArgs.RemoteHost, reciveArgs.RemotePort); break; } } else { Error.Add("有返回执行的工位请求,但数组列为0", Environment.StackTrace); } break; case LockClient <T> .SetStatueList.动作完毕取消请求: lockUdp.Write(string.Format("{0}", (int)GetStatueList.互锁请求正常结束), reciveArgs.RemoteHost, reciveArgs.RemotePort); if (lockList.Count > 0) { if ((string)(object)lockList[0] == (string)(object)index) { nowStatueList = GetStatueList.收到请求; testValue = ""; testingValue = ""; lockList.Remove(index); } else { Error.Add(string.Format("动作完成的工位和完成请求的工位不是同一个,大问题。允许工位为:{0},执行完成工位为:{1}", lockList[0], index), Environment.StackTrace); return; } } else { Error.Add("有返回完毕的工位请求,但数组列为0", Environment.StackTrace); } break; case LockClient <T> .SetStatueList.除: if (lockList.Count > 0) { if ((string)(object)lockList[0] == (string)(object)index) { nowStatueList = GetStatueList.收到请求; testValue = ""; testingValue = ""; if (AllTestCancel != null) { AllTestCancel(); } } } if (lockList.Contains(index)) { lockList.Remove(index); } lockUdp.Write(string.Format("{0}", (int)GetStatueList.除请求成功), reciveArgs.RemoteHost, reciveArgs.RemotePort); break; } }
public LockMain(int MainPort) { lockUdp = new Udp(MainPort); lockUdp.GetStringArgs += lockUpd_GetStringArgs; lockUdp.Open(); }