From 8a3ab64a65da226636743be07c4bee63f50da25d Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期六, 06 三月 2021 11:35:20 +0800 Subject: [PATCH] 1. 解决软件异常崩溃问题 --- src/Bro.Device.Gocator/GocatorDriver.cs | 256 ++++++++++++++++++++++++++++----------------------- 1 files changed, 141 insertions(+), 115 deletions(-) diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs index 99263dc..ffec5c6 100644 --- a/src/Bro.Device.Gocator/GocatorDriver.cs +++ b/src/Bro.Device.Gocator/GocatorDriver.cs @@ -91,13 +91,12 @@ 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); + //IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * (int)IIConfig.ByteNums); //娌℃湁鎻掑�硷紝榛樿0 //Parallel.For(0, height, h => @@ -105,47 +104,52 @@ // CopyMemory((IntPtr)((long)zoomPtr + width * 2 * Math.Floor(h * zoomFactor)), (IntPtr)((long)bufferPointer + width * 2 * h), width * 2); // }); - if (IIConfig.ByteNums == GocatorDataByteNums.Byte2) + //if (IIConfig.ByteNums == GocatorDataByteNums.Byte2) + //{ + //浣跨敤涓婁竴琛屽師鏈夋暟鎹� + Parallel.For(0, zoomHeight, h => { - //浣跨敤涓婁竴琛屽師鏈夋暟鎹� - Parallel.For(0, zoomHeight, h => - { - int originHeightIndex = (int)Math.Floor((double)height * h / (double)zoomHeight); + 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); + }); + //} + //else + //{ + // //浣跨敤涓婁竴琛屽師鏈夋暟鎹� + // 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); - }); - } - 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)); + // }); - 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("int4", (int)width, zoomHeight, zoomPtr); - imgSet.HImage.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "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.GenImage1("int2", (int)width, zoomHeight, zoomPtr); + + //imgSet.HImage = imgSet.HImage.ConvertImageType("int4"); + //imgSet.HImage = imgSet.HImage.ScaleImage(1.0, short.MaxValue + 1.0); + //imgSet.HImage = imgSet.HImage.ConvertImageType("uint2"); + imgSet.HImage_2 = new HImage(); + imgSet.HImage_2.GenImage1("int2", (int)width, zoomHeight, zoomPtr); //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(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() { @@ -162,6 +166,12 @@ //imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant"); } + //var showImage = imgSet.HImage_2.ConvertSignedHImageTo16GrayBitmap(); + //showImage.Save($@"D:\SI\{DateTime.Now.ToString("yyyyMMddHHmmss")}.tif", System.Drawing.Imaging.ImageFormat.Tiff); + + //SaveOriginImage(null, showImage, imgSet.Id); + + //Marshal.FreeHGlobal(zoomPtr); Generate16GrayImageByPointer((int)width, zoomHeight, zoomPtr, imgSet?.Id).ContinueWith(t => { Marshal.FreeHGlobal(zoomPtr); @@ -227,34 +237,7 @@ { if (opConfig.IsOpenConnection) { - 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, $"浼犳劅鍣ㄥ惎鍔ㄦ垚鍔�", ""); + StartSensor(); } if (!opConfig.IsSnapshotAction) @@ -277,10 +260,12 @@ if (!_snapHandle.WaitOne(IIConfig.SnapshotTimeout)) { LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚瓒呮椂", ""); - return null; + return imgSet; } dataSet = _currentData; + + //dataSet = system.ReceiveData(IIConfig.SnapshotTimeout); } } else @@ -322,38 +307,9 @@ { 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, $"浼犳劅鍣ㄥ叧闂垚鍔�", ""); + StopSensor(); } } - - sensor.Flush(); if (dataSet != null) { @@ -367,7 +323,72 @@ } } + sensor.Flush(); return imgSet; + } + + private void StopSensor() + { + 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, $"浼犳劅鍣ㄥ叧闂垚鍔�", ""); + } + + private void StartSensor() + { + 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, $"浼犳劅鍣ㄥ惎鍔ㄦ垚鍔�", ""); } float _currentExposure = 0; @@ -433,6 +454,7 @@ system.EnableData(true); + if (IIConfig.IsAsyncMode) { system.SetDataHandler(onData); @@ -465,22 +487,26 @@ { base.Start(); - if (sensor.State != GoState.Ready) - { - system.Start(); - } + //if (sensor.State != GoState.Ready) + //{ + // system.Start(); + //} if (IIConfig.IsAsyncMode) { - sensor.Start(); + //sensor.Start(); + + StartSensor(); } - else - { - //if (sensor.State == GoState.Ready) - { - sensor.Stop(); - } - } + //else + //{ + // //if (sensor.State == GoState.Ready) + // //{ + // // sensor.Stop(); + // //} + + // StopSensor(); + //} } protected override void Stop() @@ -488,7 +514,9 @@ base.Stop(); if (IIConfig.IsAsyncMode) - system.Stop(); + { + StopSensor(); + } if (IIConfig.IsUseAccelerator) { @@ -517,7 +545,7 @@ } volatile bool _snapFlag = false; - readonly ManualResetEvent _snapHandle = new ManualResetEvent(false); + readonly AutoResetEvent _snapHandle = new AutoResetEvent(false); GoDataSet _currentData = null; /// <summary> @@ -526,6 +554,7 @@ /// <param name="data"></param> private void onData(KObject data) { + LogAsync(DateTime.Now, $"鎵弿鍥惧儚杈撳嚭", ""); GoDataSet dataSet = (GoDataSet)data; if (IIConfig.IsHardwareTrigger) @@ -546,11 +575,13 @@ } else { - if (_snapFlag) + //if (_snapFlag) { _snapFlag = false; _currentData = dataSet.Clone<GoDataSet>(); + + _currentData = dataSet; _snapHandle.Set(); } } @@ -571,8 +602,6 @@ if (set == null) return; - set.Image = map; - await Task.Run(() => { lock (set.SaveLock) @@ -586,6 +615,8 @@ //SaveImageByNameAndType(map, set.Id, set.ImageSaveOption.ImageFormat, imgDir); string filePath = Path.Combine(imgDir, $"{set.Id}.tif"); set.HImage_2.WriteImage("tiff", 0, filePath); + set.HImage_2.Dispose(); + set.HImage_2 = null; } catch (Exception ex) { @@ -616,11 +647,6 @@ if (set.ImageSaveOption.AddtionalSaveType.Contains(prefix) && !set.IsAddtionalSaved) { string imgDir = CheckImageDirectory(set.ImageSaveOption.ImageSaveSubDirectory, prefix); - - while (set.Image == null) - { - Thread.Sleep(50); - } try { @@ -657,10 +683,10 @@ public override void Dispose() { - base.Dispose(); + //HImage_2?.Dispose(); + //HImage_2 = null; - HImage_2?.Dispose(); - HImage_2 = null; + base.Dispose(); } } } -- Gitblit v1.8.0