From 5918194fccdb2a2303e713b8d2f3335243b9e2ef Mon Sep 17 00:00:00 2001 From: wells.liu <wells.liu@broconcentric.com> Date: 星期二, 07 七月 2020 14:53:13 +0800 Subject: [PATCH] 板卡驱动 --- src/Bro.Device.GTSCard/GTSCardDriver.cs | 1211 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 1,052 insertions(+), 159 deletions(-) diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs index a51429e..c2cc8e6 100644 --- a/src/Bro.Device.GTSCard/GTSCardDriver.cs +++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs @@ -1,89 +1,815 @@ 锘縰sing Bro.Common.Base; +using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using System; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.Drawing; +using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; +using static Bro.Common.Helper.EnumHelper; namespace Bro.Device.GTSCard { - public class GTSCardDriver : DeviceBase,IMonitor, IMotion + [Device("GTSCard", "鍥洪珮鏉垮崱", EnumHelper.DeviceAttributeType.Device)] + public class GTSCardDriver : MotionCardBase { - public event Action<DateTime, string, IDevice, MonitorSet> OnMonitorInvoke; - public event Action<DateTime, IDevice, WarningSet> OnMonitorAlarm; + // 寮傚父浜嬩欢 + public Action<Exception> OnExceptionRaised; - public List<AxisInfo> GetCurrentAxisInfo(params string[] axisName) + public GTSCardInitialConfig IIConfig + { + get + { + return InitialConfig as GTSCardInitialConfig; + } + } + + static object moveLock = new object(); + /// <summary> + /// 鏄惁澶嶄綅鏍囧織 + /// </summary> + bool _isResetting = false; + + /// <summary> + /// 杩愬姩杞寸珛鍗虫殏鍋� + /// </summary> + Dictionary<int, ManualResetEvent> axisImmediatePauseHandleDict = new Dictionary<int, ManualResetEvent>(); + + Dictionary<int, CancellationTokenSource> axisMoveCancelDict = new Dictionary<int, CancellationTokenSource>(); + + public void SetResetFlag(bool isReset) + { + _isResetting = isReset; + } + + public override List<AxisInfo> GetCurrentAxisInfo(params string[] axisName) { throw new NotImplementedException(); } + + #region DeviceBase protected override void Init() { - throw new NotImplementedException(); + InitialMotionCard(); + axisImmediatePauseHandleDict = IConfig.AxisSettings.ToDictionary(a => a.AxisIndex, a => new ManualResetEvent(true)); + axisMoveCancelDict = IConfig.AxisSettings.ToDictionary(a => a.AxisIndex, a => new CancellationTokenSource()); + + axisMoveCancelDict.Values.ToList().ForEach(c => + { + c = new CancellationTokenSource(); + }); } + ManualResetEvent _pauseHandle = new ManualResetEvent(true); protected override void Pause() { - throw new NotImplementedException(); + } protected override void Resume() { - throw new NotImplementedException(); + } protected override void Start() { - throw new NotImplementedException(); + AllAxisOn(); } protected override void Stop() { - throw new NotImplementedException(); + AllMoveStop(); + AllAxisOff(); } /// <summary> - /// 鐐逛綅鍒扮偣浣� 杩愬姩 + /// 璁惧 杩愯锛堟墽琛� 鏉垮崱绯诲垪鐨勬搷浣滅殑 闆嗗悎锛� /// </summary> - /// <param name="cardNum">鍗″彿</param> - /// <param name="axisNum">杞村彿</param> - /// <param name="prfPosition">瑙勫垝浣嶇疆,鍗曚綅姣背</param> - /// <param name="prfVelocity">瑙勫垝閫熷害,鍗曚綅绫虫瘡绉�</param> - public void P2P(short cardNum, short axisNum, int prfPosition, int prfVelocity) + /// <param name="config"></param> + /// <returns></returns> + public override ResponseMessage Run(IOperationConfig config) { - GTSCardAPI.TTrapPrm trapprm; - GTSCardAPI.GT_PrfTrap(cardNum, axisNum); - GTSCardAPI.GT_GetTrapPrm(cardNum, axisNum, out trapprm); - trapprm.acc = GTSCardParameter.P2PAcc; - trapprm.dec = GTSCardParameter.P2PDec; + ResponseMessage responseMessage = new ResponseMessage(); + if (config is MotionCardOperationConfigBase motionCardOperationConfig) + { + _pauseHandle.WaitOne(); + foreach (var operationSet in motionCardOperationConfig.OperationCollection) + { + responseMessage = RunOperationSet(operationSet); + if (!responseMessage.Result) + { + return responseMessage; + } + } + } + return responseMessage; + } + + /// <summary> + /// 鎵ц 涓�涓郴鍒楃殑鎿嶄綔 + /// </summary> + private ResponseMessage RunOperationSet(MotionCardOperationSet operationSet) + { + ResponseMessage responseMessage = new ResponseMessage(); + // 1.棰勬鏌� + if (CurrentState == DeviceState.DSOpen) + { + foreach (var preCheck in operationSet.PreCheckIOCollection) + { + int timeout = operationSet.PreCheckIOTimeout; + IOValue? ioData = null; + while (CurrentState == DeviceState.DSOpen) + { + Thread.Sleep(10); + ioData = MonitorValues.FirstOrDefault(u => u.IONum == preCheck.IOItem.IONum && u.IOType == preCheck.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 + timeout -= 10; + if (preCheck.CheckValue == ioData || (operationSet.PreCheckIOTimeout > 0 && timeout < 0)) + { + break; + } + } + + if (preCheck.CheckValue != ioData) + { + responseMessage.Result = false; + responseMessage.Message = $"棰勬鏌ヤ笉閫氳繃锛岄厤缃細{preCheck.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}"; + return responseMessage; + } + } + } + + // 2.鏉垮崱杩愬姩 + if (CurrentState == DeviceState.DSOpen) + { + responseMessage = MoveToPoint(operationSet.MotionOperationCollection); + if (!responseMessage.Result) + { + return responseMessage; + } + } + + + // 3.IO杈撳嚭 涓嶉渶瑕佽秴鏃� + if (CurrentState == DeviceState.DSOpen) + { + foreach (var ioOutput in operationSet.IOOutputCollection) + { + var ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioOutput.IOItem.IONum && u.IOType == ioOutput.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 + if (ioOutput.CheckValue != ioData) + { + responseMessage.Result = false; + responseMessage.Message = $"IO杈撳嚭涓嶉�氳繃锛岄厤缃細{ioOutput.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}"; + return responseMessage; + } + } + } + + // 4.IO纭 + if (CurrentState == DeviceState.DSOpen) + { + foreach (var ioConfirm in operationSet.IOConfirmCollection) + { + int timeout = operationSet.IOConfirmTimeout; + IOValue? ioData = null; + while (CurrentState == DeviceState.DSOpen) + { + Thread.Sleep(10); + ioData = MonitorValues.FirstOrDefault(u => u.IONum == ioConfirm.IOItem.IONum && u.IOType == ioConfirm.IOItem.IOType)?.Value;//IO 鏄紑銆佸叧 浠嶮onitorValues 鑾峰彇 + timeout -= 10; + if (ioConfirm.CheckValue == ioData || (operationSet.IOConfirmTimeout > 0 && timeout < 0)) + { + break; + } + } + + if (ioConfirm.CheckValue != ioData) + { + responseMessage.Result = false; + responseMessage.Message = $"IO纭涓嶉�氳繃锛岄厤缃細{ioConfirm.GetDisplayText()}锛屽綋鍓嶅�硷細{ioData}"; + return responseMessage; + } + } + } + + return responseMessage; + } + #endregion + + #region ImmediatePause + /// <summary> + /// 鍚姩绔嬪嵆鏆傚仠 + /// </summary> + public void SetImmediatePause() + { + if (!_isResetting) + { + var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); + + immediatePauseAxis.ForEach(async axisIndex => + { + axisImmediatePauseHandleDict[axisIndex].Reset(); + + axisMoveCancelDict[axisIndex].Cancel(); + + await MoveStop(axisIndex, 0); + }); + } + } + + /// <summary> + /// 鎭㈠绔嬪嵆鏆傚仠 + /// </summary> + public void ResetImmediatePause() + { + var immediatePauseAxis = IConfig.AxisSettings.Where(u => u.IsImmediatePause).Select(u => u.AxisIndex).ToList(); + + immediatePauseAxis.ForEach(axisIndex => + { + axisMoveCancelDict[axisIndex] = new CancellationTokenSource(); + + axisImmediatePauseHandleDict[axisIndex].Set(); + }); + } + #endregion + + #region GTSCard + + /// <summary> + /// Load Motion Card parameter from file + /// </summary> + /// <param name="fileName">Invalid Parameter</param> + /// <returns></returns> + public void InitialMotionCard() + { + var res = GTSCardAPI.GT_Open((short)IConfig.CardNum, 0, 1); //鎵撳紑杩愬姩鎺у埗鍣ㄣ�傚弬鏁板繀椤讳负锛�0,1锛夛紝涓嶈兘淇敼銆� + res += GTSCardAPI.GT_LoadConfig((short)IConfig.CardNum, IConfig.InitialConfigFilePath); + res += GTSCardAPI.GT_ClrSts(0, 1, 8); + if (res != (short)GTSRetCode.GRCRunOK) + { + throw new Exception("鏉垮崱杞藉叆閰嶇疆鏂囦欢寮傚父锛岄敊璇爜锛�" + res); + } + } + + + public bool AllAxisOn() + { + List<Task<bool>> taskList = new List<Task<bool>>(); ; + // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱寮�鍚� + IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum => + { + var task = AxisOnAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex); + taskList.Add(task); + }); + Task.WaitAll(taskList.ToArray()); + var resultOK = taskList.All(u => u.GetAwaiter().GetResult()); + return resultOK; + } + + public bool AllAxisOff() + { + List<Task<bool>> taskList = new List<Task<bool>>(); ; + // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱鍏抽棴 + IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum => + { + var task = AxisOffAsync((short)IConfig.CardNum, (short)axisNum.AxisIndex); + taskList.Add(task); + }); + Task.WaitAll(taskList.ToArray()); + var resultOK = taskList.All(u => u.GetAwaiter().GetResult()); + return resultOK; + } + + /// <summary> + /// 鍗曚釜杞村紑鍚� + /// </summary> + /// <returns></returns> + public async Task<bool> AxisOnAsync(short cardNum, short axisNum) + { + return await Task.Run(() => + { + var ret = GTSCardAPI.GT_AxisOn(cardNum, axisNum); + return ret == (short)GTSRetCode.GRCRunOK; + }); + } + + /// <summary> + /// 鍗曚釜杞村叧闂� + /// </summary> + /// <returns></returns> + public async Task<bool> AxisOffAsync(short cardNum, short axisNum) + { + return await Task.Run(() => + { + var ret = GTSCardAPI.GT_AxisOff(cardNum, axisNum); + return ret == (short)GTSRetCode.GRCRunOK; + }); + } + + /// <summary> + /// 鐐逛綅鍒扮偣浣嶈繍鍔� + /// </summary> + /// <param name="item">杩愬姩瀵硅薄</param> + /// <returns>杩愬姩鎺у埗+鍋滄鍒ゆ柇</returns> + public override ResponseMessage MoveToPoint(IOperationConfig opConfig) + { + ResponseMessage responseMessage = new ResponseMessage(); + if (opConfig is MotionOperationCollection gtsOperationCollection) + { + _pauseHandle.WaitOne(); + List<Task<bool>> taskList = new List<Task<bool>>(); + foreach (var movingOp in gtsOperationCollection.MovingOps) + { + var task = SingleAxisMoving(movingOp); + taskList.Add(task); + } + + Task.WaitAll(taskList.ToArray()); + responseMessage.Result = taskList.All(u => u.GetAwaiter().GetResult()); + if (!responseMessage.Result) + { + responseMessage.Message = $"鐐逛綅杩愬姩寮傚父"; + } + } + return responseMessage; + } + + /// <summary> + /// 鐐瑰埌鐐硅繍鍔ㄨ缃弬鏁� + /// </summary> + /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param> + /// <returns></returns> + private bool SetAxisParam(MovingOption optionPara) + { + List<short> resultCode = new List<short>() { 0 }; + GTSCardAPI.TTrapPrm trapprm = new GTSCardAPI.TTrapPrm(); + resultCode.Add(GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)optionPara.AxisIndex)); + resultCode.Add(GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, out trapprm)); trapprm.smoothTime = 1; - GTSCardAPI.GT_SetTrapPrm(cardNum, axisNum, ref trapprm); - GTSCardAPI.GT_SetPos(cardNum, axisNum, prfPosition * GTSCardParameter.Dangliang); - GTSCardAPI.GT_SetVel(cardNum, axisNum, prfVelocity * GTSCardParameter.Dangliang); - GTSCardAPI.GT_Update(cardNum, 1 << (axisNum - 1)); + trapprm.acc = optionPara.VelocityPara.Acc != 0 ? optionPara.VelocityPara.Acc : 1; + trapprm.dec = optionPara.VelocityPara.Dec != 0 ? optionPara.VelocityPara.Dec : 1; + + resultCode.Add(GTSCardAPI.GT_SetTrapPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, ref trapprm)); + resultCode.Add(GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)optionPara.AxisIndex, optionPara.VelocityPara.Velocity * IConfig.AxisVelocityRatio)); + + var resultOK = resultCode.All(u => u == (short)GTSRetCode.GRCRunOK); + if (!resultOK) + { + throw new Exception("杞�" + optionPara.AxisIndex + "璁剧疆鍙傛暟寮傚父锛岄敊璇爜锛�" + string.Join(",", resultCode)); + } + return resultOK; } /// <summary> - /// Jog杩愬姩 + /// 鍗曚釜杞� 杩愬姩(鐐瑰埌鐐� jog 鍥為浂...锛� /// </summary> - /// <param name="cardNum"></param> - /// <param name="axisNum"></param> - /// <param name="velocity">瑙勫垝閫熷害锛屽崟浣嶇背姣忕</param> - public void Jog(short cardNum, short axisNum, double velocity) + /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param> + public async Task<bool> SingleAxisMoving(MovingOption optionPara) { - GTSCardAPI.TJogPrm jogprm = new GTSCardAPI.TJogPrm(); - short rtn = GTSCardAPI.GT_PrfJog(cardNum, axisNum); - jogprm.acc = 1; - jogprm.dec = 1; - GTSCardAPI.GT_SetJogPrm(cardNum, axisNum, ref jogprm);//璁剧疆jog杩愬姩鍙傛暟 - GTSCardAPI.GT_SetVel(cardNum, axisNum, velocity);//璁剧疆鐩爣閫熷害 - GTSCardAPI.GT_Update(cardNum, 1 << (axisNum - 1));//鏇存柊杞磋繍鍔� + return await Task.Run(() => + { + bool isSuccessAndStop = false; + try + { + if (IConfig.AxisSettings.FirstOrDefault(a => a.AxisIndex == optionPara.AxisIndex)?.IsAxisEnabled ?? false) + { + axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + + string _position = ""; + string motionType = optionPara.MoveMode == EnumHelper.MotionMode.Normal ? (optionPara.IsAbsolute ? "Abs" : "Rel") : optionPara.MoveMode.ToString(); + + _position = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff},{optionPara.AxisIndex},{motionType},{GetPosition(optionPara.AxisIndex)},{GetPrfPosition(optionPara.AxisIndex)},{optionPara.Destination},"; + + switch (optionPara.MoveMode) + { + case MotionMode.Normal: + { + if (_isResetting) + { + LogAsync(DateTime.Now, "澶嶄綅涓惎鍔ㄨ繍鍔ㄥ紓甯�", optionPara.AxisIndex + "鍚姩杩愬姩寮傚父"); + return false; + } + + if (optionPara.IsAbsolute) + { + isSuccessAndStop = P2PMoveAbs(optionPara); + } + else + { + isSuccessAndStop = P2PMoveRel(optionPara); + } + + } + break; + case MotionMode.FindOri: + { + isSuccessAndStop = GoHome(optionPara); + } + break; + case MotionMode.Jog: + { + isSuccessAndStop = JogMove(optionPara); + } + break; + } + _position += $"{GetPosition(optionPara.AxisIndex)},"; + _position += $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}"; + LogAsync(DateTime.Now, "", _position); + } + } + catch (Exception ex) + { + isSuccessAndStop = false; + LogAsync(DateTime.Now, $"杞磠optionPara.AxisIndex}杩愬姩寮傚父", ex.GetExceptionMessage()); + } + return isSuccessAndStop; + }); } /// <summary> - /// 璇诲彇杈撳叆 + /// 鑾峰彇瑙勫垝浣嶇疆锛堣鍘荤殑浣嶇疆锛� + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <returns></returns> + public double GetPrfPosition(int axisNum) + { + double position = 0; + double prfpos = 0; uint pclock = 0; + var ret = GTSCardAPI.GT_GetPrfPos((short)IConfig.CardNum, (short)axisNum, out prfpos, 1, out pclock); + if (ret != (short)GTSRetCode.GRCRunOK) + { + throw new Exception("杞�" + axisNum + "鑾峰彇瑙勫垝浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); + } + position = prfpos / IConfig.AxisVelocityRatio; + return position; + } + + /// <summary> + /// 鑾峰彇鐩墠褰撳墠浣嶇疆 + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <returns></returns> + public double GetPosition(int axisNum) + { + lock (moveLock) + { + double position = 0; + int pPos = 0; + var ret = GTSCardAPI.GT_GetPos((short)IConfig.CardNum, (short)axisNum, out pPos); + if (ret != (short)GTSRetCode.GRCRunOK) + { + throw new Exception("杞�" + axisNum + "鑾峰彇鐩爣浣嶇疆寮傚父锛岄敊璇爜锛�" + ret); + } + position = pPos / IConfig.AxisVelocityRatio; + return position; + } + } + + /// <summary> + /// 鑾峰彇瑙勫垝閫熷害 + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <returns></returns> + public double GetPrfVelocity(int axisNum) + { + double prfVel = 0; + uint pclock = 0; + var ret = GTSCardAPI.GT_GetPrfVel((short)IConfig.CardNum, (short)axisNum, out prfVel, 1, out pclock); + if (ret != (short)GTSRetCode.GRCRunOK) + { + throw new Exception("杞�" + axisNum + "鑾峰彇瑙勫垝閫熷害寮傚父锛岄敊璇爜锛�" + ret); + } + prfVel = prfVel / IConfig.AxisVelocityRatio; + return prfVel; + } + + /// <summary> + /// 鑾峰彇褰撳墠閫熷害 + /// </summary> + /// <param name="axisNum">Axis number</param> + /// <returns></returns> + public double GetVelocity(int axisNum) + { + double vel = 0; + var ret = GTSCardAPI.GT_GetVel((short)IConfig.CardNum, (short)axisNum, out vel); + if (ret != (short)GTSRetCode.GRCRunOK) + { + throw new Exception("杞�" + axisNum + "鑾峰彇褰撳墠閫熷害寮傚父锛岄敊璇爜锛�" + ret); + } + vel = vel / IConfig.AxisVelocityRatio; + return vel; + } + + /// <summary> + /// Set Single Axis Do Jog Move + /// </summary> + /// <param name="axisNum">AxisNo</param> + /// <param name="nDirection">Motion Direction 0: Negative, 1: Positive</param> + /// <param name="nMaxVel">max velocity</param> + /// <returns></returns> + public bool JogMove(MovingOption optionPara) + { + try + { + GTSCardAPI.TJogPrm jogprm = new GTSCardAPI.TJogPrm(); + short ret = 0; + int repeatTime = 100; + do + { + ret = GTSCardAPI.GT_PrfJog((short)IConfig.CardNum, (short)optionPara.AxisIndex); + jogprm.acc = optionPara.VelocityPara.Acc; + jogprm.dec = optionPara.VelocityPara.Dec; + ret = GTSCardAPI.GT_SetJogPrm((short)IConfig.CardNum, (short)optionPara.AxisIndex, ref jogprm);//璁剧疆jog杩愬姩鍙傛暟 + ret = GTSCardAPI.GT_SetVel((short)IConfig.CardNum, (short)optionPara.AxisIndex, optionPara.VelocityPara.Velocity);//璁剧疆鐩爣閫熷害 + ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杞磋繍鍔� + + if (ret != (short)GTSRetCode.GRCRunOK) + { + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "JogMove寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime); + Thread.Sleep(10); + } + repeatTime--; + } while (ret != (short)GTSRetCode.GRCRunOK && repeatTime > 0); + return (ret == (short)GTSRetCode.GRCRunOK); + } + catch (Exception ex) + { + AllMoveStop(true); + OnExceptionRaised?.Invoke(ex); + return false; + } + } + + /// <summary> + /// 鐩稿浣嶇疆杩愬姩 + /// </summary> + /// <param name="axisNum">AxisNo</param> + /// <param name="nDistance">run distance</param> + /// <returns></returns> + public bool P2PMoveRel(MovingOption optionPara) + { + try + { + if (_isResetting) + { + LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + } + + int repeatTime = 30; + while (CurrentState != EnumHelper.DeviceState.DSOpen && repeatTime > 0) + { + Thread.Sleep(10); + repeatTime--; + } + + if (CurrentState == EnumHelper.DeviceState.DSExcept) + { + LogAsync(DateTime.Now, "鏉垮崱寮傚父鐘舵��", "杞�" + optionPara.AxisIndex + "璇曞浘寮傚父鐘舵�佽繍鍔�"); + return false; + } + + if (CurrentState != EnumHelper.DeviceState.DSOpen) + { + LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); + throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + } + + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); + short ret = 0; + bool isSuccessSetAxisParam = false; + int currentPosition = (int)GetPosition(optionPara.AxisIndex); + int dPosition = optionPara.Destination + currentPosition; + int timeout = optionPara.MovingTimeout; + while (CurrentState == DeviceState.DSOpen) + { + //璁剧疆 杩愬姩鍙傛暟 + isSuccessSetAxisParam = SetAxisParam(optionPara); + ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(dPosition * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆 + ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩 + + if (ret != (short)GTSRetCode.GRCRunOK) + { + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime); + Thread.Sleep(50); + } + timeout -= 50; + if ((ret == (short)GTSRetCode.GRCRunOK && isSuccessSetAxisParam) || (optionPara.MovingTimeout > 0 && timeout < 0)) + { + break; + } + } + + //杩愬姩寮�濮嬪悗 妫�鏌ヨ繍鍔ㄦ槸鍚﹀仠姝� + bool isStop = false; + repeatTime = 1000; + do + { + isStop = IsStop((short)optionPara.AxisIndex); + Thread.Sleep(50); + repeatTime--; + } while (!isStop && repeatTime > 0); + + return (ret == (short)GTSRetCode.GRCRunOK) && isStop; + } + catch (Exception ex) + { + AllMoveStop(true); + OnExceptionRaised?.Invoke(ex); + return false; + } + } + + /// <summary> + /// 缁濆浣嶇疆杩愬姩 + /// </summary> + /// <param name="optionPara">杩愬姩鍙傛暟瀵硅薄</param> + public bool P2PMoveAbs(MovingOption optionPara) + { + try + { + axisImmediatePauseHandleDict[optionPara.AxisIndex].WaitOne(); + + if (_isResetting) + { + LogAsync(DateTime.Now, "澶嶄綅杩囩▼寮傚父", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄥ浣嶈繃绋嬩腑杩愬姩"); + } + int repeatTime = 30; + while (CurrentState != EnumHelper.DeviceState.DSOpen && repeatTime > 0) + { + Thread.Sleep(10); + repeatTime--; + } + if (CurrentState == EnumHelper.DeviceState.DSExcept) + { + LogAsync(DateTime.Now, "鏉垮崱寮傚父鐘舵��", "杞�" + optionPara.AxisIndex + "璇曞浘寮傚父鐘舵�佽繍鍔�"); + return false; + } + + if (CurrentState != EnumHelper.DeviceState.DSOpen) + { + LogAsync(DateTime.Now, "闈炴甯哥姸鎬佸紓甯�", "杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�"); + throw new Exception("杞�" + optionPara.AxisIndex + "璇曞浘鍦ㄩ潪姝e父鐘舵�佽繍鍔�", null); + } + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "寮�濮嬭繍鍔�", "鐩爣鍧愭爣锛�" + optionPara.Destination); + short ret = 0; + bool isSuccessSetAxisParam = false; + int timeout = optionPara.MovingTimeout; + while (CurrentState == DeviceState.DSOpen) + { + //璁剧疆 杩愬姩鍙傛暟 + isSuccessSetAxisParam = SetAxisParam(optionPara); + ret = GTSCardAPI.GT_SetPrfPos((short)IConfig.CardNum, (short)optionPara.AxisIndex, (int)(optionPara.Destination * IConfig.AxisVelocityRatio));// 璁剧疆瑙勫垝浣嶇疆 + ret = GTSCardAPI.GT_Update((short)IConfig.CardNum, 1 << (optionPara.AxisIndex - 1));//鏇存柊杩愬姩 + + if (ret != (short)GTSRetCode.GRCRunOK) + { + LogAsync(DateTime.Now, "杞�" + optionPara.AxisIndex + "APS_absolute_move寮傚父", "閿欒鐮侊細" + ret + ";" + "閲嶈瘯娆℃暟锛�" + repeatTime); + Thread.Sleep(50); + } + timeout -= 50; + if ((ret == (short)GTSRetCode.GRCRunOK && isSuccessSetAxisParam) || (optionPara.MovingTimeout > 0 && timeout < 0)) + { + break; + } + } + + bool isStop = false; + repeatTime = 1000; + do + { + isStop = IsStop((short)optionPara.AxisIndex); + Thread.Sleep(50); + repeatTime--; + } while (!isStop && repeatTime > 0); + + return (ret == (short)GTSRetCode.GRCRunOK) && isStop; + } + catch (Exception ex) + { + AllMoveStop(true); + OnExceptionRaised?.Invoke(ex); + return false; + } + } + + /// <summary> + /// 鏌愪釜杞磋繍鍔ㄥ仠姝� + /// </summary> + /// <param name="axisNum">axisNo</param> + /// <param name="option">0琛ㄧず骞虫粦鍋滄锛�1琛ㄧず绱ф�ュ仠姝�</param> + /// <returns></returns> + public async Task<bool> MoveStop(int axisNum, int option) + { + return await Task.Run(() => + { + bool isStop = false; + if (option == 1) + { + StateChange(EnumHelper.DeviceState.DSExcept); + } + var ret = GTSCardAPI.GT_Stop((short)IConfig.CardNum, 1 << (axisNum - 1), option); + if (ret != (short)GTSRetCode.GRCRunOK) + { + LogAsync(DateTime.Now, "杞�" + axisNum + "杩愬姩鍋滄寮傚父", "閿欒鐮侊細" + ret); + throw new Exception("杞�" + axisNum + "杩愬姩鍋滄寮傚父锛岄敊璇爜锛�" + ret); + } + else + { + LogAsync(DateTime.Now, "杞�" + axisNum + "杩愬姩鍋滄", ""); + } + int repeatTime = 100; + do + { + isStop = IsStop((short)axisNum); + Thread.Sleep(10); + repeatTime--; + } while (!isStop && repeatTime > 0); + + return (ret == (short)GTSRetCode.GRCRunOK) && isStop; + }); + } + + /// <summary> + /// 鎵�鏈夊紑鍚殑杞村仠姝� + /// </summary> + /// <param name="emergencyStop"></param> + public void AllMoveStop(bool emergencyStop = false) + { + int option = emergencyStop ? 1 : 0; + List<Task<bool>> taskList = new List<Task<bool>>(); ; + // 濡傛灉鏄涓酱鐨勮繍鍔� 绛夋瘡涓酱杩愬姩缁撴潫 + IConfig.AxisSettings.Where(a => a.IsAxisEnabled).ToList().ForEach(axisNum => + { + var task = MoveStop(axisNum.AxisIndex, option); + taskList.Add(task); + }); + Task.WaitAll(taskList.ToArray()); + var resultOK = taskList.All(u => u.GetAwaiter().GetResult()); + } + + /// <summary> + /// 鍥炲師鐐� + /// </summary> + /// <param name="movingOption">鍗″彿</param> + /// <param name="axisn">杞村彿</param> + /// <param name="homests">杞村洖鍘熺偣鐘舵��</param> + public bool GoHome(MovingOption movingOption) + { + try + { + GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, 1); + GTSCardAPI.THomePrm thomeprm; + GTSCardAPI.THomeStatus homests; + short rtn = GTSCardAPI.GT_GetHomePrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out thomeprm); + thomeprm.mode = movingOption.GoHomePara.HomeMode;//鍥為浂鏂瑰紡 + thomeprm.moveDir = movingOption.GoHomePara.HomeDir;//鍥為浂鏂瑰悜 + thomeprm.edge = movingOption.GoHomePara.Edge; + thomeprm.velHigh = movingOption.GoHomePara.HighVelocity; + thomeprm.velLow = movingOption.GoHomePara.LowVelocity; + thomeprm.acc = movingOption.VelocityPara.Acc; + thomeprm.dec = movingOption.VelocityPara.Dec; + thomeprm.searchHomeDistance = movingOption.GoHomePara.SearchHomeDistance;//鎼滄悳璺濈 + thomeprm.homeOffset = movingOption.GoHomePara.HomeOffset; //鍋忕Щ璺濈 + thomeprm.escapeStep = movingOption.GoHomePara.EscapeStep; + rtn = GTSCardAPI.GT_GoHome((short)IConfig.CardNum, (short)movingOption.AxisIndex, ref thomeprm); //鍚姩鍥為浂 + + bool isStop = false; + int repeatTime = 1000; + do + { + Thread.Sleep(10); + GTSCardAPI.GT_GetHomeStatus((short)IConfig.CardNum, (short)movingOption.AxisIndex, out homests); + + isStop = homests.run == 0; + if (isStop && homests.error == 0) + { + Thread.Sleep(200); + GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, 1); + } + repeatTime--; + } while (!isStop && repeatTime > 0); + + return isStop; + } + catch (Exception ex) + { + AllMoveStop(true); + OnExceptionRaised?.Invoke(ex); + return false; + } + } + + /// <summary> + /// 璇诲彇IO杈撳叆 /// </summary> /// <param name="cardNum">鍗″彿</param> /// <param name="index">杈撳叆鍙�</param> @@ -92,162 +818,329 @@ { int value; GTSCardAPI.GT_GetDi(cardNum, GTSCardAPI.MC_GPI, out value); - if ((value & 1 << index) == 0) return true;//鏈夎緭鍏ヨ繑鍥瀟rue + if ((value & (1 << index)) == 0) return true;//鏈夎緭鍏ヨ繑鍥瀟rue else return false; //鏃犺緭鍏ヨ繑鍥瀎alse } /// <summary> - /// 杈撳嚭 + /// 璇诲彇IO杈撳嚭 /// </summary> - /// <param name="cardNumo">鍗″彿</param> - /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param> - /// <param name="value">0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴</param> - public void WriteOut(short cardNumo, short index, bool value) - { - short outNum = (short)(index % 100 + 1); - switch (value) - { - case true: - { - GTSCardAPI.GT_SetDoBit(cardNumo, GTSCardAPI.MC_GPO, outNum, 0);//鎸変綅杈撳嚭锛�0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴 - } - break; - case false: - { - GTSCardAPI.GT_SetDoBit(cardNumo, GTSCardAPI.MC_GPO, outNum, 1);//鎸変綅杈撳嚭锛�0琛ㄧず杈撳嚭锛�1琛ㄧず鍏抽棴 - } - break; - } - } - - /// <summary> - /// 鍋滄 鏌愪釜杞� - /// </summary> - /// <param name="cardNum"></param> - /// <param name="axisNum"></param> - /// <param name="option">0琛ㄧず骞虫粦鍋滄锛�1琛ㄧず绱ф�ュ仠姝�</param> - public void Stop(short cardNum, short axisNum, short option) - { - GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), option); - } - - /// <summary> - /// 鍋滄 鏌愪釜杞� - /// </summary> - /// <param name="cardNum"></param> - /// <param name="axisNum">杞村彿</param> - /// <param name="value">鍋滄鏂瑰紡锛宖alse琛ㄧず骞虫粦鍋滄锛宼rue琛ㄧず绱ф�ュ仠姝�</param> - public void Stop(short cardNum, short axisNum, bool value) - { - switch (value) - { - case false: - { - GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 0); - } - break; - case true: - { - GTSCardAPI.GT_Stop(cardNum, 1 << (axisNum - 1), 1 << (axisNum - 1)); - - } - break; - } - } - - /// <summary> - /// 鍋滄 鍏ㄩ儴杞� - /// </summary> - /// <param name="cardNum"></param> - /// <param name="value">鍋滄鏂瑰紡锛宖alse琛ㄧず骞虫粦鍋滄锛宼rue琛ㄧず绱ф�ュ仠姝�</param> - public void StopAll(short cardNum, bool value) - { - for (short i = 1; i <= GTSCardParameter.AxisCount; i++) - { - Stop(cardNum, i, value); - } - } - - /// <summary> - /// IO杈撳嚭 - /// </summary> - /// <param name="cardNum">鍗″彿</param> - /// <param name="mdl">妯″潡鍙�</param> - /// <param name="index">IO杈撳嚭</param> - /// <param name="value">true琛ㄧず杈撳嚭锛宖alse琛ㄧず鏃犺緭鍑�</param> - public void MC_WriteDigitalOutput(short cardNum, short mdl, short index, bool value) - { - if (value) - { - GTSCardAPI.GT_SetExtIoBit(cardNum, mdl, index, 0); - } - else - { - GTSCardAPI.GT_SetExtIoBit(cardNum, mdl, index, 1); - } - } - - /// <summary> - /// 璇诲彇IO杈撳嚭鐘舵�� - /// </summary> - /// <param name="cardNumo"></param> - /// <param name="index"></param> + /// <param name="index">io绱㈠紩</param> /// <returns></returns> - public bool GetDoSts(short cardNumo, short index) + public bool GetDoSts(short index) { - short outNum = 0; int outSts; - outNum = (short)(index % 100); - GTSCardAPI.GT_GetDo(cardNumo, GTSCardAPI.MC_GPO, out outSts); + short outNum = (short)(index % 100); + GTSCardAPI.GT_GetDo((short)IConfig.CardNum, GTSCardAPI.MC_GPO, out outSts); if ((outSts & (1 << outNum)) == 0) return true; else return false; } - static object lockObj = new object(); - - /// <summary> - /// 璇诲彇褰撳墠鍊� + /// 鎸変綅璁剧疆鏁板瓧 IO 杈撳嚭鐘舵�� /// </summary> - /// <param name="cardNum">鍗″彿</param> - /// <param name="axisNum">杞村彿</param> - /// <returns>杩斿洖褰撳墠鍊硷紝鍗曚綅姣背</returns> - public double GetPosMM(short cardNum, short axisNum) + /// <param name="index">杈撳嚭鍙�,杩斿洖1-16</param> + /// <param name="value">false琛ㄧず杈撳嚭锛宼rue琛ㄧず鍏抽棴</param> + public override void WriteOutput(short index, IOValue value) { - lock (lockObj) + short outNum = (short)(index % 100 + 1); + if ((int)value <= 1) { - double prfpos = 0; uint pclock = 0; - GTSCardAPI.GT_GetPrfPos(cardNum, axisNum, out prfpos, 1, out pclock); - return prfpos / GTSCardParameter.Dangliang; + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)value); + } + else + { + var currentValue = (int)MonitorValues.FirstOrDefault(u => u.IONum == outNum && u.IOType == IOType.OUTPUT).Value; + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPO, outNum, (short)(currentValue == 1 ? 0 : 1)); } } /// <summary> /// 璇诲彇杞寸姸鎬侊紝鍒ゆ柇鐢垫満鏄惁鍋滄 /// </summary> - /// <param name="cardNum"></param> - /// <param name="axisNum"></param> + /// <param name="cardNum">鏉垮崱鍙�</param> + /// <param name="axisNum">杞村彿</param> /// <returns></returns> - public bool IsStop(short cardNum, short axisNum) + public bool IsStop(short axisNum) { - lock (lockObj) + int sts = GetAxisStatus(axisNum); + if ((sts & 0x400) == 0) return true;//鍋滄杩斿洖true + else return false; //杩愯涓繑鍥瀎alse + } + + /// <summary> + /// 璇诲彇杞寸姸鎬� + /// </summary> + /// <param name="cardNum">鏉垮崱鍙�</param> + /// <param name="axisNum">杞村彿</param> + /// <returns></returns> + public int GetAxisStatus(int axisNum) + { + lock (moveLock) { int sts = 0; uint pclock = 0; - GTSCardAPI.GT_GetSts(cardNum, axisNum, out sts, 1, out pclock); - if ((sts & 0x400) == 0) return true;//鍋滄杩斿洖true - else return false; //杩愯涓繑鍥瀎alse + GTSCardAPI.GT_GetSts((short)IConfig.CardNum, (short)axisNum, out sts, 1, out pclock); + return sts; } } + #endregion - public void Monitor() + #region IMonitor + + public List<AxisMovingStatus> GetAxisMovingStatus() { - throw new NotImplementedException(); + List<AxisMovingStatus> axisMovingStatusesList = new List<AxisMovingStatus>(); + foreach (var axisConfig in IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled)) + { + AxisMovingStatus axisMovingStatus = new AxisMovingStatus(); + axisMovingStatus.AxisIndex = axisConfig.AxisIndex; + axisMovingStatus.AxisName = axisConfig.AxisName; + axisMovingStatus.CurPosition = Convert.ToInt32(GetPosition(axisMovingStatus.AxisIndex)); + axisMovingStatus.Destination = Convert.ToInt32(GetPrfPosition(axisMovingStatus.AxisIndex)); + axisMovingStatus.CurVelocity = GetVelocity(axisMovingStatus.AxisIndex); + axisMovingStatus.PrfVelocity = GetPrfVelocity(axisMovingStatus.AxisIndex); + axisMovingStatus.AxisStatus = GetAxisStatus(axisMovingStatus.AxisIndex); + + axisMovingStatusesList.Add(axisMovingStatus); + + } + + return axisMovingStatusesList; } - public void ResetAlarm() + + public List<IOItem> GetMonitorValues() { - throw new NotImplementedException(); + var result = new List<IOItem>(); + //璇诲彇IO杈撳叆 + int inValue; + GTSCardAPI.GT_GetDi((short)IConfig.CardNum, GTSCardAPI.MC_GPI, out inValue); + //璇诲彇IO杈撳嚭 + int outValue; + GTSCardAPI.GT_GetDo((short)IConfig.CardNum, GTSCardAPI.MC_GPO, out outValue); + + //瑙f瀽缁撴灉 + for (var index = 1; index <= 16; index++) + { + IOItem inItem = new IOItem() + { + IONum = index, + Value = (inValue & (1 << index)) == 0 ? IOValue.TRUE : IOValue.FALSE, + IOType = IOType.INPUT + }; + IOItem outItem = new IOItem() + { + IONum = index, + Value = (outValue & (1 << index)) == 0 ? IOValue.TRUE : IOValue.FALSE, + IOType = IOType.OUTPUT + }; + result.Add(inItem); + result.Add(outItem); + } + + return result; } + + public async override void Monitor() + { + await Task.Run(() => + { + while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit) + { + try + { + if (!IConfig.IsEnableMonitor) + return; + var newValues = GetMonitorValues(); + var newAxisMovingStatus = GetAxisMovingStatus(); + if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0) + continue; + + Stopwatch sw = new Stopwatch(); + sw.Start(); + if (MonitorValues.Count == newValues.Count) + { + var tempNew = newValues.DeepSerializeClone();//clone + var tempOld = MonitorValues.DeepSerializeClone(); + MonitorCheckAndInvoke(tempNew, tempOld); + } + + AxisMovingOptionValues = new List<AxisMovingStatus>(newAxisMovingStatus); + MonitorValues = new List<IOItem>(newValues); + sw.Stop(); + + if (sw.ElapsedMilliseconds > 20) + { + LogAsync(DateTime.Now, $"{this.Name}杞鏃堕棿锛歿sw.ElapsedMilliseconds}", ""); + } + + if (IConfig.MonitorInterval > 0) + { + Thread.Sleep(IConfig.MonitorInterval); + } + + } + catch (Exception ex) + { + if (CurrentState == DeviceState.DSOpen) + { + LogAsync(DateTime.Now, $"{this.Name}鐩戝惉寮傚父", ex.GetExceptionMessage()); + } + } + } + }); + } + + public override void OnMethodInvoked(IAsyncResult ar) + { + MotionCardMonitorSet monitorSet = ar.AsyncState as MotionCardMonitorSet; + ProcessResponse resValues = monitorSet.Response; + if (resValues.ResultValue == (int)ReplyValue.IGNORE) + { + return; + } + + Stopwatch sw = new Stopwatch(); + sw.Start(); + // 灏嗘寚瀹欼OItem鍐欏叆鏉垮崱 + foreach (var replyIOData in monitorSet.ReplyIODatas) + { + //鍐欏叆IO杈撳嚭 + if (replyIOData.IOType == IOType.OUTPUT) + { + GTSCardAPI.GT_SetDoBit((short)IConfig.CardNum, GTSCardAPI.MC_GPI, (short)replyIOData.IONum, (short)replyIOData.Value); + } + // in鍙涓嶈兘鍐� + } + sw.Stop(); + LogAsync(DateTime.Now, $"{Name}鍙嶉瀹屾垚锛岃�楁椂{sw.ElapsedMilliseconds}ms", $"{resValues.GetDisplayText()}"); + } + + protected void MonitorCheckAndInvoke(List<IOItem> tempNew, List<IOItem> tempOld) + { + #region 璀︽姤淇℃伅 + Parallel.ForEach(IConfig.WarningSetCollection, wSet => + { + MotionCardWarningSet warningSet = wSet as MotionCardWarningSet; + + bool isOn = (((int)((tempNew.FirstOrDefault(u => u.IONum == warningSet.TriggerIndex && u.IOType == warningSet.WarningIOModel)?.Value)) >> warningSet.TriggerIndex) & 1) == (warningSet.TriggerValue ? 1 : 0); + + if (warningSet.CurrentStatus != isOn) + { + warningSet.CurrentStatus = isOn; + warningSet.TriggerTime = DateTime.Now; + SaveAlarmCSVAsync(DateTime.Now, this.Name, warningSet); + ExcuteMonitorAlarm(DateTime.Now, this, warningSet); + } + }); + #endregion + + #region 鐩戝惉淇℃伅 + Parallel.ForEach(IConfig.MonitorSetCollection, mSet => + { + MotionCardMonitorSet monitorSet = mSet as MotionCardMonitorSet; + if (monitorSet.TriggerIndex < 0 || monitorSet.TriggerIndex > tempNew.Count) + { + return; + } + + var newIOItem = tempNew.FirstOrDefault(u => u.IONum == monitorSet.TriggerIndex); + var oldIOItem = tempOld.FirstOrDefault(u => u.IONum == monitorSet.TriggerIndex); + + if (newIOItem?.Value != oldIOItem?.Value) + { + if (monitorSet.TriggerValue == -999 || (int)newIOItem.Value == monitorSet.TriggerValue) + { + if (monitorSet.OpConfig == null) + { + monitorSet.OpConfig = new OperationConfigBase(); + } + + //monitorSet.OpConfig.InputPara = monitorSet.InputDataIndex.ConvertAll(index => + //{ + // return tempNew[index].Value; + //}).ToList(); + + ExcuteMonitorInvoke(DateTime.Now, monitorSet.InvokeDevice, this, monitorSet); + } + } + }); + #endregion + } + + public override void ResetAlarm() + { + int axis_sts; + var axisSettings = IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled); + GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)axisSettings.Count); + foreach (var axisSetting in axisSettings) + { + axis_sts = GetAxisStatus((short)axisSetting.AxisIndex); + if ((axis_sts & 0x200) == 0) + { + var rst = GTSCardAPI.GT_AxisOn((short)IConfig.CardNum, (short)axisSetting.AxisIndex); + } + // 浣嶇疆璇烽浂 + GTSCardAPI.GT_ZeroPos((short)IConfig.CardNum, 1, (short)axisSettings.Count); + // 姝f瀬闄愭姤璀� + if ((axis_sts & 0x20) != 0) + { + // 璐熷悜绉诲姩 + MovingOption movingOption = new MovingOption(); + movingOption.AxisIndex = (short)axisSetting.AxisIndex; + movingOption.Destination = -50; // 璐熷悜绉诲姩 + movingOption.VelocityPara.Velocity = 50; + P2PMoveAbs(movingOption); + } + + // 璐熸瀬闄愭姤璀� + if ((axis_sts & 0x40) != 0) + { + // 姝e悜绉诲姩 + MovingOption movingOption = new MovingOption(); + movingOption.AxisIndex = (short)axisSetting.AxisIndex; + movingOption.Destination = 50; // 璐熷悜绉诲姩 + movingOption.VelocityPara.Velocity = 50; + P2PMoveAbs(movingOption); + } + } + + // 娓呴櫎鐘舵�� + GTSCardAPI.GT_ClrSts((short)IConfig.CardNum, 1, (short)IConfig.AxisSettings.FindAll(u => u.IsAxisEnabled).Count); + } + + object _alarmLock = new object(); + private async void SaveAlarmCSVAsync(DateTime now, string plcName, IWarningSet ws) + { + await Task.Run(() => + { + lock (_alarmLock) + { + DirectoryInfo dir = new DirectoryInfo(this.IConfig.LogPath); + if (!dir.Exists) + { + dir.Create(); + } + + string path = Path.Combine(IConfig.LogPath, $"Alarm_{Name}_{now.ToString("yyyyMMdd")}.csv"); + bool fileExist = File.Exists(path); + + using (StreamWriter writer = new StreamWriter(path, true, System.Text.Encoding.UTF8)) + { + if (!fileExist) + { + writer.WriteLine("Time,Source,AlarmCode,AlarmDescription,AlarmStatus"); + } + + writer.WriteLine($"{now.ToString("HH:mm:ss.fff")},{plcName},{ws.WarningCode},{ws.WarningDescription},{(ws.CurrentStatus ? "鎶ヨ" : "鍋滄")}"); + + writer.Flush(); + writer.Close(); + } + } + }); + } + #endregion } } -- Gitblit v1.8.0