using GummingCommon; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace GummingControl { public class ControlCheck { public static int WaitTimes = 20000;//等待次数 20000/5, 约一千次, 20S public static int HomeWaitTimes = 15000;//等待次数 15000/5, 约3千次, 15S public static int DIWaitTimes = 10000;//等待次数 5000/10, 约500次, 5S public static bool PMCInvoke(bool IgnoreError, string FuncInfo, object Input, Func DoAction) { bool st = false; int retryCount = 0; while (!st) { retryCount++; st = DoAction(); if (retryCount % 101 == 0) { string error = string.Format("{0}{1}次重试,", FuncInfo, retryCount); Global.WriteErrorLog(error); } if (retryCount > 3) { Thread.Sleep(5); } if (retryCount > Global.MaxRetryTimers) { object target = DoAction.Target; string parameters = string.Empty; if (target != null) { foreach (var field in target?.GetType()?.GetFields()) { parameters = string.Format("{0},FieldName:{1},FieldValue:{2}\r\n", parameters, field.Name, field.GetValue(target)); } } string error = string.Format("{0}硬件接口第{1}次重试后仍然不能连接硬件设备,错误详情:{2},请尝试重启系统进行连接", FuncInfo, retryCount, parameters); Global.WriteErrorLog(error); if (!IgnoreError) { //暂时不予处理 break; //throw new Exception(error); } } } return st; } public static bool WaitMainHomeDone(ushort nodeId, ushort slotId, int delay) { bool isDone = false; int waitTime = 0; if (delay <= 0) { delay = HomeWaitTimes; } DateTime dtStart = DateTime.Now; while (!isDone && waitTime < delay) { Thread.Sleep(5); //等待执行时间后自动关闭 ushort postion = DeviceControl.HomeStatus(nodeId, slotId); isDone = postion == 0; if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < delay; } public static bool WaitHomeDone(ushort nodeId, ushort slotId, int delay) { bool isDone = false; int waitTime = 0; if (delay <= 0) { delay = HomeWaitTimes; } DateTime dtStart = DateTime.Now; while (!isDone && waitTime < delay) { Thread.Sleep(5); //等待执行时间后自动关闭 isDone = DeviceControl.IsRealHomeStatus(nodeId, slotId); if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < delay; /* bool isDone = false; int waitTime = 0; while (!isDone && waitTime < HomeWaitTimes) { Thread.Sleep(5); //等待执行时间后自动关闭 ushort postion = DeviceControl.HomeStatus(nodeId, slotId); isDone = postion == 0; if (isDone) { return true; } waitTime = waitTime + 5; } return waitTime < HomeWaitTimes;*/ } public static bool WaitHPHomeDone(ushort nodeId, ushort slotId, int delay) { bool isDone = false; int waitTime = 0; if (delay <= 0) { delay = HomeWaitTimes; } DateTime dtStart = DateTime.Now; while (!isDone && waitTime < delay) { Thread.Sleep(5); //等待执行时间后自动关闭 isDone = DeviceControl.IsHPRealHomeStatus(nodeId, slotId); if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < delay; } public static bool IsHomeDone(ushort nodeId, ushort slotId) { /*ushort postion = DeviceControl.HomeStatus(nodeId, slotId); int postion2 = 0; DeviceControl.GetPosition(nodeId, slotId, ref postion2); return postion == 0 && Math.Abs(postion2) < 100;*/ return DeviceControl.IsRealHomeStatus(nodeId, slotId); } public static bool IsHPHomeDone(ushort nodeId, ushort slotId) { /*ushort postion = DeviceControl.HomeStatus(nodeId, slotId); int postion2 = 0; DeviceControl.GetPosition(nodeId, slotId, ref postion2); return postion == 0 && Math.Abs(postion2) < 100;*/ return DeviceControl.IsHPRealHomeStatus(nodeId, slotId); } public static bool IsMainHomeDone(ushort nodeId, ushort slotId) { ushort postion = DeviceControl.HomeStatus(nodeId, slotId); int postion2 = 0; DeviceControl.GetPosition(nodeId, slotId, ref postion2); return postion == 0 && Math.Abs(postion2) < 100; } public static bool WaitAxisDone(ushort nodeId) { bool isDone = false; int waitTime = 0; Helpering.Sleep(300);//等待电机运动 DateTime dtStart = DateTime.Now; while (!isDone && waitTime < WaitTimes) { Thread.Sleep(5); //等待执行时间后自动关闭 isDone = DeviceControl.CheckMotionDone(nodeId); if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < WaitTimes; } public static bool WaitDIDone(string refRecId, bool compareValue) { bool isDone = false; int waitTime = 0; var inputIO = Global.InputIOs.FirstOrDefault(q => q.RecId == refRecId); DateTime dtStart = DateTime.Now; while (!isDone && waitTime < DIWaitTimes) { Thread.Sleep(10); //等待执行时间后自动关闭 isDone = DeviceControl.GetPortStatus(inputIO.NodeId, inputIO.SlotId, inputIO.PortIndex, compareValue); if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < DIWaitTimes; } public static bool WaitDIDone(string refRecId, bool compareValue, int delay) { bool isDone = false; int waitTime = 0; var inputIO = Global.InputIOs.FirstOrDefault(q => q.RecId == refRecId); DateTime dtStart = DateTime.Now; if (delay <= 0) { delay = DIWaitTimes; } while (!isDone && waitTime < delay) { Thread.Sleep(10); //等待执行时间后自动关闭 isDone = DeviceControl.GetPortStatus(inputIO.NodeId, inputIO.SlotId, inputIO.PortIndex, compareValue); if (isDone) { return true; } waitTime = (int)(DateTime.Now - dtStart).TotalMilliseconds; } return waitTime < delay; } } }