From 1f2fa3f54d4a7b5a2f601c4c947a06476342e3bd Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期五, 22 一月 2021 09:23:06 +0800
Subject: [PATCH] 1. gocator驱动将system.stop移到system.ReceiveData之后 2. 修改测量完成后状态切换逻辑,避免安全光线等干扰 3. 添加设备异常输出日志记录 4. 产品结果输出时做硬盘文件记录

---
 src/Bro.Device.Gocator/GocatorDriver.cs |  134 ++++++++++++++++++++++++++++++++------------
 1 files changed, 96 insertions(+), 38 deletions(-)

diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs
index 8e0bef5..ba3546f 100644
--- a/src/Bro.Device.Gocator/GocatorDriver.cs
+++ b/src/Bro.Device.Gocator/GocatorDriver.cs
@@ -66,6 +66,8 @@
             }
 
             HandleGoData(dataSet);
+
+            dataSet.Dispose();
         }
 
         private void HandleGoData(GoDataSet dataSet, GoImageSet imgSet = null)
@@ -88,9 +90,13 @@
                             long bufferSize = width * height;
                             IntPtr bufferPointer = surfaceMsg.Data;
 
+
                             float zoomFactor = (float)((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution);
                             int zoomHeight = (int)(zoomFactor * height);
-                            IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * 2);
+
+                            //IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * 2);
+                            //IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * 4);
+                            IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * (int)IIConfig.ByteNums);
 
                             //娌℃湁鎻掑�硷紝榛樿0
                             //Parallel.For(0, height, h =>
@@ -98,23 +104,45 @@
                             //      CopyMemory((IntPtr)((long)zoomPtr + width * 2 * Math.Floor(h * zoomFactor)), (IntPtr)((long)bufferPointer + width * 2 * h), width * 2);
                             //  });
 
-                            //浣跨敤涓婁竴琛屽師鏈夋暟鎹�
-                            Parallel.For(0, zoomHeight, h =>
+                            if (IIConfig.ByteNums == GocatorDataByteNums.Byte2)
                             {
-                                int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight);
+                                //浣跨敤涓婁竴琛屽師鏈夋暟鎹�
+                                Parallel.For(0, zoomHeight, h =>
+                                {
+                                    int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight);
 
-                                CopyMemory((IntPtr)((long)zoomPtr + width * 2 * h), (IntPtr)((long)bufferPointer + width * 2 * originHeightIndex), width * 2);
-                            });
+                                    CopyMemory((IntPtr)((long)zoomPtr + width * 2 * h), (IntPtr)((long)bufferPointer + width * 2 * originHeightIndex), width * 2);
+                                });
+                            }
+                            else
+                            {
+                                //浣跨敤涓婁竴琛屽師鏈夋暟鎹�
+                                Parallel.For(0, zoomHeight, h =>
+                                {
+                                    int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight);
+
+                                    byte[] rowBuffer = new byte[width * 4];
+                                    Parallel.For(0, width, w =>
+                                    {
+                                        rowBuffer[w * 4 + 2] = Marshal.ReadByte(bufferPointer, (int)(width * 2 * originHeightIndex + w * 2));
+                                        rowBuffer[w * 4 + 3] = Marshal.ReadByte(bufferPointer, (int)(width * 2 * originHeightIndex + w * 2 + 1));
+                                    });
+
+                                    Marshal.Copy(rowBuffer, 0, (IntPtr)((long)zoomPtr + width * 2 * h), rowBuffer.Length);
+                                });
+                            }
 
                             if (imgSet != null)
                             {
                                 imgSet.HImage = new HImage();
-                                imgSet.HImage.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
-                                //imgSet.HImage = imgSet.HImage.ZoomImageSize((int)width, zoomHeight, "constant");
+                                //imgSet.HImage.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
+                                //imgSet.HImage.GenImage1("int4", (int)width, zoomHeight, zoomPtr);
+                                imgSet.HImage.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "int4", (int)width, zoomHeight, zoomPtr);
 
-
-                                imgSet.HImage_2 = new HImage();
-                                imgSet.HImage_2.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
+                                imgSet.HImage_2 = imgSet.HImage.Clone();
+                                //imgSet.HImage_2.GenImage1("uint2", (int)width, zoomHeight, zoomPtr);
+                                //imgSet.HImage_2.GenImage1("int4", (int)width, zoomHeight, zoomPtr);
+                                //imgSet.HImage_2.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "int4", (int)width, zoomHeight, zoomPtr);
 
                                 LaserScanParam para = new LaserScanParam()
                                 {
@@ -183,28 +211,24 @@
                         //    }
                         //    break;
                 }
+
+                dataObj.Dispose();
             }
         }
 
         public override IImageSet Snapshot(IOperationConfig config)
         {
+            GocatorOperationConfig opConfig = config as GocatorOperationConfig;
             if (!IIConfig.IsAsyncMode)
             {
-                if (config is GocatorOperationConfig opConfig)
+                if (opConfig.IsOpenConnection)
                 {
-                    if (opConfig.IsOpenConnection)
-                    {
-                        system.Start();
-                    }
-                    else
-                    {
-                        system.Stop();
-                    }
+                    system.Start();
+                }
 
-                    if (!opConfig.IsSnapshotAction)
-                    {
-                        return null;
-                    }
+                if (!opConfig.IsSnapshotAction)
+                {
+                    return null;
                 }
             }
 
@@ -228,6 +252,11 @@
             }
             else
             {
+                if (!opConfig.IsSnapshotAction)
+                {
+                    return null;
+                }
+
                 try
                 {
                     dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
@@ -239,7 +268,19 @@
                 }
             }
 
+            if (!IIConfig.IsAsyncMode)
+            {
+                if (!opConfig.IsOpenConnection)
+                {
+                    system.Stop();
+                    sensor.Flush();
+                }
+            }
+
             HandleGoData(dataSet, imgSet);
+
+            dataSet.Dispose();
+
             if (imgSet.HImage == null)
             {
                 LogAsync(DateTime.Now, $"{Name}鏈兘鑾峰彇HImage鍥惧儚", "");
@@ -262,7 +303,12 @@
 
                 if (!string.IsNullOrWhiteSpace(opConfig.JobName) && _currentJob != opConfig.JobName)
                 {
-                    _currentJob = sensor.DefaultJob = opConfig.JobName;
+                    LogAsync(DateTime.Now, $"{Name}鍒囨崲褰撳墠浠诲姟涓簕opConfig.JobName}", "");
+                    sensor.CopyFile(_currentJob, "_live.job");
+
+                    bool isChanged = false;
+                    sensor.LoadedJob(ref _currentJob, ref isChanged);
+                    LogAsync(DateTime.Now, $"{Name}褰撳墠浠诲姟{_currentJob}", "");
                 }
 
                 sensor.Flush();
@@ -318,18 +364,17 @@
                 _currentExposure = (float)sensor.Setup.GetExposure(GoRole.Main);
             }
 
-            _currentJob = sensor.DefaultJob;
-            if (!string.IsNullOrWhiteSpace(IIConfig.DefaultJob) && _currentJob != IIConfig.DefaultJob)
-            {
-                //_currentJob = sensor.DefaultJob = IIConfig.DefaultJob;
-                string currentJob = IIConfig.DefaultJob;
-                bool isChanged = false;
-                sensor.LoadedJob(ref currentJob, ref isChanged);
+            bool isChanged = false;
+            sensor.LoadedJob(ref _currentJob, ref isChanged);
+            LogAsync(DateTime.Now, $"{Name}褰撳墠浠诲姟{_currentJob}", "");
 
-                if (!isChanged)
-                {
-                    throw new ProcessException($"{Name}鏈垚鍔熷垏鎹㈣嚦浠诲姟{IIConfig.DefaultJob}");
-                }
+            if (!string.IsNullOrWhiteSpace(IIConfig.DefaultJob) && IIConfig.DefaultJob != _currentJob)
+            {
+                sensor.CopyFile(IIConfig.DefaultJob, "_live.job");
+                //_currentJob = IIConfig.DefaultJob;
+
+                sensor.LoadedJob(ref _currentJob, ref isChanged);
+                LogAsync(DateTime.Now, $"{Name}褰撳墠浠诲姟{_currentJob}", "");
             }
 
             sensor.Flush();
@@ -414,6 +459,8 @@
                     _snapHandle.Set();
                 }
             }
+
+            dataSet.Dispose();
         }
 
         #region 閲嶅啓鍥剧墖淇濆瓨鎿嶄綔
@@ -424,7 +471,7 @@
                  OnImageUpdated?.Invoke(this, showImage, imgSetId);
              });
 
-            GoImageSet set = _imageSetList.FirstOrDefault(u => u.Id == imgSetId) as GoImageSet;
+            GoImageSet set = _imageSetList[imgSetId] as GoImageSet;
 
             if (set == null)
                 return;
@@ -465,7 +512,7 @@
         {
             await Task.Run(() =>
             {
-                GoImageSet set = _imageSetList.FirstOrDefault(u => u.Id == imgSetId) as GoImageSet;
+                GoImageSet set = _imageSetList[imgSetId] as GoImageSet;
                 if (set == null)
                     return;
 
@@ -495,6 +542,17 @@
                 }
             });
         }
+
+        //public override void ClearImageSet(IImageSet set)
+        //{
+        //    if (set.IsOriginSaved && set.IsFitSaved && set.IsAddtionalSaved)
+        //    {
+        //        (set as GoImageSet).HImage_2?.Dispose();
+        //        (set as GoImageSet).HImage_2 = null;
+        //    }
+
+        //    base.ClearImageSet(set);
+        //}
         #endregion
     }
 

--
Gitblit v1.8.0