From 6f92829951f3ec228b18df8c157239573edd9cb4 Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期三, 09 九月 2020 15:20:53 +0800
Subject: [PATCH] 1. 取消ImageSet中的HImage释放动作,转移到业务流程中 2. 拆分板卡的IO监听和轴状态,轴位置监听

---
 src/Bro.Device.GTSCard/GTSCardDriver.cs |  112 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 95 insertions(+), 17 deletions(-)

diff --git a/src/Bro.Device.GTSCard/GTSCardDriver.cs b/src/Bro.Device.GTSCard/GTSCardDriver.cs
index 573f480..a721273 100644
--- a/src/Bro.Device.GTSCard/GTSCardDriver.cs
+++ b/src/Bro.Device.GTSCard/GTSCardDriver.cs
@@ -99,6 +99,9 @@
         {
             AllAxisOn();
 
+            MonitorPosition();
+            MonitorAxisStatus();
+
             base.Start();
         }
 
@@ -894,11 +897,9 @@
                 short capture = 0;
 
                 GTSCardAPI.TTrapPrm trapPrm;
-                //鎼滅储璺濈 闃舵1
-                // 鍚姩Home鎹曡幏
-                short sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
+
                 // 鍒囨崲鍒扮偣浣嶈繍鍔ㄦā寮�
-                sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex);
+                short sRtn = GTSCardAPI.GT_PrfTrap((short)IConfig.CardNum, (short)movingOption.AxisIndex);
                 // 璇诲彇鐐逛綅妯″紡杩愬姩鍙傛暟
                 sRtn = GTSCardAPI.GT_GetTrapPrm((short)IConfig.CardNum, (short)movingOption.AxisIndex, out trapPrm);
                 trapPrm.acc = movingOption.VelocityPara.Acc;
@@ -912,6 +913,10 @@
                 {
                     PositionReset(movingOption.AxisIndex, 1);
                     ClearStatus(movingOption.AxisIndex, 1);
+
+                    //鎼滅储璺濈 闃舵1
+                    // 鍚姩Home鎹曡幏
+                    sRtn = GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
 
                     // 璁剧疆鐐逛綅妯″紡鐩爣浣嶇疆锛屽嵆鍘熺偣鎼滅储璺濈
                     sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, homeDirection ? 99999999 : -99999999);
@@ -942,8 +947,6 @@
 
                     if (isStop && capture != 1)
                     {
-                        ClearStatus((short)movingOption.AxisIndex, 1);
-
                         if (((axisStatus.AxisStatus & 0x20) != 0) || ((axisStatus.AxisStatus & 0x40) != 0))
                         {
                             //movingOption.GoHomePara.HomeDir = (short)(movingOption.GoHomePara.HomeDir == 1 ? -1 : 1);
@@ -954,7 +957,6 @@
                                 isRightLimitReached = true;
                             }
 
-
                             //璐熼檺浣�
                             if ((axisStatus.AxisStatus & 0x40) != 0 && goHomePara.IsCaptureDirPositive)
                             {
@@ -963,6 +965,8 @@
 
                             homeDirection = !homeDirection;
                         }
+
+                        ClearStatus(movingOption.AxisIndex, 1);
                     }
 
                     if (capture == 1)
@@ -970,6 +974,7 @@
                         if (!isRightLimitReached)
                         {
                             capture = 0;
+                            ClearStatus(movingOption.AxisIndex, 1);
                             //GTSCardAPI.GT_SetCaptureMode((short)IConfig.CardNum, (short)movingOption.AxisIndex, GTSCardAPI.CAPTURE_HOME);
                             continue;
                         }
@@ -977,6 +982,7 @@
                         //鍏坰top
                         MoveStop((short)movingOption.AxisIndex, 0);
                         ClearStatus((short)movingOption.AxisIndex, 1);
+
                         //宸茬粡鎹曡幏鍒癏ome鎵嶅彲浠ュ洖闆� 闃舵2
                         // 杩愬姩鍒�"鎹曡幏浣嶇疆+鍋忕Щ閲�"
                         sRtn = GTSCardAPI.GT_SetPos((short)IConfig.CardNum, (short)movingOption.AxisIndex, pos + goHomePara.HomeOffset);
@@ -1128,7 +1134,6 @@
             return axisMovingStatusesList;
         }
 
-
         public List<IOItem> GetMonitorValues()
         {
             var result = new List<IOItem>();
@@ -1161,6 +1166,79 @@
             return result;
         }
 
+        SpinWait _positionWait = new SpinWait();
+        private async void MonitorPosition()
+        {
+            await Task.Run(() =>
+            {
+                while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit)
+                {
+                    try
+                    {
+                        if (!IConfig.IsEnableMonitor)
+                            return;
+
+                        AxisStatusList.ForEach(a =>
+                        {
+                            int curPosition = (int)GetPrfPosition(a.AxisIndex);
+
+                            if (a.CurPosition != curPosition)
+                            {
+                                a.CurPosition = curPosition;
+                                AxisPositionChanged(a.AxisIndex, curPosition);
+                            }
+                        });
+
+                        _positionWait.SpinOnce();
+                    }
+                    catch (Exception ex)
+                    {
+                        if (CurrentState == DeviceState.DSOpen)
+                        {
+                            LogAsync(DateTime.Now, $"{this.Name}鐩戝惉杞翠俊鎭紓甯�", ex.GetExceptionMessage());
+                        }
+                    }
+                }
+            });
+        }
+
+        SpinWait _statusWait = new SpinWait();
+        private async void MonitorAxisStatus()
+        {
+            await Task.Run(() =>
+            {
+                while (CurrentState != EnumHelper.DeviceState.DSClose && CurrentState != EnumHelper.DeviceState.DSExcept && CurrentState != EnumHelper.DeviceState.DSUninit)
+                {
+                    try
+                    {
+                        if (!IConfig.IsEnableMonitor)
+                            return;
+
+                        AxisStatusList.ForEach(a =>
+                        {
+                            int curStatus = GetAxisStatus(a.AxisIndex);
+
+                            if (a.AxisStatus != curStatus)
+                            {
+                                int temp = a.AxisStatus;
+                                a.AxisStatus = curStatus;
+                                AxisStatusChanged(a.AxisIndex, temp, curStatus);
+                            }
+                        });
+
+                        _statusWait.SpinOnce();
+                    }
+                    catch (Exception ex)
+                    {
+                        if (CurrentState == DeviceState.DSOpen)
+                        {
+                            LogAsync(DateTime.Now, $"{this.Name}鐩戝惉杞翠俊鎭紓甯�", ex.GetExceptionMessage());
+                        }
+                    }
+                }
+            });
+        }
+
         public async override void Monitor()
         {
             await Task.Run(() =>
@@ -1173,9 +1251,9 @@
                             return;
 
                         var newValues = GetMonitorValues();
-                        var newAxisMovingStatus = GetAxisMovingStatus();
+                        //var newAxisMovingStatus = GetAxisMovingStatus();
 
-                        if (newValues == null || newValues.Count == 0 || newAxisMovingStatus == null || newAxisMovingStatus.Count == 0)
+                        if (newValues == null || newValues.Count == 0 )
                             continue;
 
                         //Stopwatch sw = new Stopwatch();
@@ -1189,14 +1267,14 @@
                             MonitorCheckAndInvoke(tempNew, tempOld);
                         }
 
-                        if (AxisStatusList.Count == newAxisMovingStatus.Count)
-                        {
-                            var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone
-                            var tempOld = new List<AxisMovingStatus>(AxisStatusList);
-                            AxisStatusCheck(tempNew, tempOld);
-                        }
+                        //if (AxisStatusList.Count == newAxisMovingStatus.Count)
+                        //{
+                        //    var tempNew = new List<AxisMovingStatus>(newAxisMovingStatus);//clone
+                        //    var tempOld = new List<AxisMovingStatus>(AxisStatusList);
+                        //    AxisStatusCheck(tempNew, tempOld);
+                        //}
 
-                        AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus);
+                        //AxisStatusList = new List<AxisMovingStatus>(newAxisMovingStatus);
                         MonitorValues = new List<IOItem>(newValues);
                         //sw.Stop();
 

--
Gitblit v1.8.0