From b3aa95bab5796caaf28a06922abf111ae55e37df Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期六, 20 二月 2021 10:58:39 +0800 Subject: [PATCH] xlsx文件异常处理 --- src/Bro.Device.Gocator/GocatorDriver.cs | 269 +++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 195 insertions(+), 74 deletions(-) diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs index 0d22a6f..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; @@ -94,8 +95,9 @@ 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 => @@ -103,40 +105,47 @@ // 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); - ////浣跨敤涓婁竴琛屽師鏈夋暟鎹� - //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)); + }); - // 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); - //}); + 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.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 = imgSet.HImage.Clone(); + //imgSet.HImage_2.GenImage1("uint2", (int)width, zoomHeight, zoomPtr); + //imgSet.HImage_2.GenImage1("int4", (int)width, zoomHeight, zoomPtr); imgSet.HImage_2 = new HImage(); - 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() { @@ -210,25 +219,47 @@ } } + [MethodImpl(MethodImplOptions.Synchronized)] 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) + while (sensor.State != GoState.Ready) { - system.Start(); - } - else - { - system.Stop(); + Thread.Sleep(10); } - if (!opConfig.IsSnapshotAction) + do { - return null; - } + 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) + { + return null; } } @@ -238,38 +269,102 @@ 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 { - try + if (!opConfig.IsSnapshotAction) { - dataSet = system.ReceiveData(IIConfig.SnapshotTimeout); - } - catch (Exception ex) - { - LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage()); return null; + } + + int reTryTime = 3; + do + { + 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) + { + 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; @@ -289,7 +384,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(); @@ -345,18 +445,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(); @@ -366,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() @@ -525,21 +638,29 @@ }); } - 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; - } + //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); - } + // base.ClearImageSet(set); + //} #endregion } public class GoImageSet : ImageSet { public HImage HImage_2 { get; set; } + + public override void Dispose() + { + base.Dispose(); + + HImage_2?.Dispose(); + HImage_2 = null; + } } } -- Gitblit v1.8.0