From 78289c64a16dd02cc9fec595bf758a9e30a30926 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期六, 20 二月 2021 10:51:11 +0800
Subject: [PATCH] gocator修改异步模式下图片获取操作

---
 src/Bro.Device.Gocator/GocatorDriver.cs |  147 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 121 insertions(+), 26 deletions(-)

diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs
index ba3546f..99263dc 100644
--- a/src/Bro.Device.Gocator/GocatorDriver.cs
+++ b/src/Bro.Device.Gocator/GocatorDriver.cs
@@ -13,6 +13,7 @@
 using System.Drawing;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading;
@@ -139,10 +140,12 @@
                                 //imgSet.HImage.GenImage1("int4", (int)width, zoomHeight, zoomPtr);
                                 imgSet.HImage.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "int4", (int)width, zoomHeight, zoomPtr);
 
-                                imgSet.HImage_2 = imgSet.HImage.Clone();
+                                //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);
+
+                                imgSet.HImage_2 = new HImage();
+                                imgSet.HImage_2.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "int4", (int)width, zoomHeight, zoomPtr);
 
                                 LaserScanParam para = new LaserScanParam()
                                 {
@@ -216,6 +219,7 @@
             }
         }
 
+        [MethodImpl(MethodImplOptions.Synchronized)]
         public override IImageSet Snapshot(IOperationConfig config)
         {
             GocatorOperationConfig opConfig = config as GocatorOperationConfig;
@@ -223,7 +227,34 @@
             {
                 if (opConfig.IsOpenConnection)
                 {
-                    system.Start();
+                    while (sensor.State != GoState.Ready)
+                    {
+                        Thread.Sleep(10);
+                    }
+
+                    do
+                    {
+                        try
+                        {
+                            sensor.Start();
+                            LogAsync(DateTime.Now, $"浼犳劅鍣ㄧ姸鎬侊細{sensor.State.ToString()}", "");
+                            if (sensor.State != GoState.Running)
+                            {
+                                Thread.Sleep(100);
+                            }
+                            else
+                            {
+                                break;
+                            }
+                        }
+                        catch (Exception ex)
+                        {
+                            LogAsync(DateTime.Now, $"寮�鍚紓甯革細{ex.GetExceptionMessage()}", "");
+                            Thread.Sleep(100);
+                        }
+                    } while (true);
+
+                    LogAsync(DateTime.Now, $"浼犳劅鍣ㄥ惎鍔ㄦ垚鍔�", "");
                 }
 
                 if (!opConfig.IsSnapshotAction)
@@ -238,16 +269,23 @@
             GoDataSet dataSet = null;
             if (IIConfig.IsAsyncMode)
             {
-                if (!IIConfig.IsHardwareTrigger)
+                if (opConfig.IsSnapshotAction)
                 {
-                    _snapFlag = true;
-                    if (!_snapHandle.WaitOne(IIConfig.SnapshotTimeout))
+                    if (!IIConfig.IsHardwareTrigger)
                     {
-                        LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚瓒呮椂", "");
-                        return null;
-                    }
+                        _snapFlag = true;
+                        if (!_snapHandle.WaitOne(IIConfig.SnapshotTimeout))
+                        {
+                            LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚瓒呮椂", "");
+                            return null;
+                        }
 
-                    dataSet = _currentData;
+                        dataSet = _currentData;
+                    }
+                }
+                else
+                {
+                    imgSet = null;
                 }
             }
             else
@@ -257,33 +295,76 @@
                     return null;
                 }
 
-                try
+                int reTryTime = 3;
+                do
                 {
-                    dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
-                }
-                catch (Exception ex)
-                {
-                    LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage());
-                    return null;
-                }
+                    try
+                    {
+                        dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
+                        reTryTime = 0;
+                    }
+                    catch (Exception ex)
+                    {
+                        reTryTime--;
+                        LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage());
+
+                        if (reTryTime <= 0)
+                        {
+                            imgSet.HImage = null;
+                            dataSet = null;
+                            system.ClearData();
+                        }
+                    }
+                } while (reTryTime > 0);
             }
 
             if (!IIConfig.IsAsyncMode)
             {
                 if (!opConfig.IsOpenConnection)
                 {
-                    system.Stop();
-                    sensor.Flush();
+                    while (sensor.State == GoState.Busy)
+                    {
+                        Thread.Sleep(100);
+                    }
+
+                    do
+                    {
+                        try
+                        {
+                            sensor.Stop();
+
+                            LogAsync(DateTime.Now, $"浼犳劅鍣ㄧ姸鎬侊細{sensor.State.ToString()}", "");
+                            if (sensor.State != GoState.Ready)
+                            {
+                                Thread.Sleep(100);
+                            }
+                            else
+                            {
+                                break;
+                            }
+                        }
+                        catch (Exception ex)
+                        {
+                            LogAsync(DateTime.Now, $"鍏抽棴寮傚父锛歿ex.GetExceptionMessage()}", "");
+                            Thread.Sleep(100);
+                        }
+                    } while (true);
+                    LogAsync(DateTime.Now, $"浼犳劅鍣ㄥ叧闂垚鍔�", "");
                 }
             }
 
-            HandleGoData(dataSet, imgSet);
+            sensor.Flush();
 
-            dataSet.Dispose();
-
-            if (imgSet.HImage == null)
+            if (dataSet != null)
             {
-                LogAsync(DateTime.Now, $"{Name}鏈兘鑾峰彇HImage鍥惧儚", "");
+                HandleGoData(dataSet, imgSet);
+
+                dataSet.Dispose();
+
+                if (imgSet.HImage == null)
+                {
+                    LogAsync(DateTime.Now, $"{Name}鏈兘鑾峰彇HImage鍥惧儚", "");
+                }
             }
 
             return imgSet;
@@ -384,8 +465,22 @@
         {
             base.Start();
 
-            if (IIConfig.IsAsyncMode)
+            if (sensor.State != GoState.Ready)
+            {
                 system.Start();
+            }
+
+            if (IIConfig.IsAsyncMode)
+            {
+                sensor.Start();
+            }
+            else
+            {
+                //if (sensor.State == GoState.Ready)
+                {
+                    sensor.Stop();
+                }
+            }
         }
 
         protected override void Stop()

--
Gitblit v1.8.0