From b5e7fa2db4ac36fdb7bf9fd330d12a72ade9d483 Mon Sep 17 00:00:00 2001 From: patrick.xu <patrick.xu@broconcentric.com> Date: 星期日, 24 一月 2021 15:07:26 +0800 Subject: [PATCH] 0124 --- src/Bro.Device.Gocator/GocatorDriver.cs | 270 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 236 insertions(+), 34 deletions(-) diff --git a/src/Bro.Device.Gocator/GocatorDriver.cs b/src/Bro.Device.Gocator/GocatorDriver.cs index 39fb4a2..fb7e04f 100644 --- a/src/Bro.Device.Gocator/GocatorDriver.cs +++ b/src/Bro.Device.Gocator/GocatorDriver.cs @@ -1,11 +1,13 @@ 锘縰sing Bro.Common.Base; using Bro.Common.Helper; using Bro.Common.Interface; +using Bro.Common.Model; using HalconDotNet; using Lmi3d.GoSdk; using Lmi3d.GoSdk.Messages; using Lmi3d.Zen; using Lmi3d.Zen.Io; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; @@ -64,6 +66,8 @@ } HandleGoData(dataSet); + + dataSet.Dispose(); } private void HandleGoData(GoDataSet dataSet, GoImageSet imgSet = null) @@ -86,41 +90,172 @@ 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 * 4); + IntPtr zoomPtr = Marshal.AllocHGlobal(zoomHeight * (int)width * (int)IIConfig.ByteNums); + + //娌℃湁鎻掑�硷紝榛樿0 + //Parallel.For(0, height, h => + // { + // CopyMemory((IntPtr)((long)zoomPtr + width * 2 * Math.Floor(h * zoomFactor)), (IntPtr)((long)bufferPointer + width * 2 * h), width * 2); + // }); + + if (IIConfig.ByteNums == GocatorDataByteNums.Byte2) + { + //浣跨敤涓婁竴琛屽師鏈夋暟鎹� + 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)); + }); + + 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, (int)height, bufferPointer); + //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, (int)height, bufferPointer); + imgSet.HImage_2.GenImage1(IIConfig.ByteNums == GocatorDataByteNums.Byte2 ? "uint2" : "int4", (int)width, zoomHeight, zoomPtr); + + LaserScanParam para = new LaserScanParam() + { + Resolution_X = surfaceMsg.XResolution, + Resolution_Y = surfaceMsg.YResolution, + Resolution_Z = surfaceMsg.ZResolution, + Offset_X = surfaceMsg.XOffset, + Offset_Y = surfaceMsg.YOffset, + Offset_Z = surfaceMsg.ZOffset, + Angle_Z = surfaceMsg.ZAngle, + }; + imgSet.ImageData = JsonConvert.SerializeObject(para); + + //imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant"); } - Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id); + Generate16GrayImageByPointer((int)width, zoomHeight, zoomPtr, imgSet?.Id).ContinueWith(t => + { + Marshal.FreeHGlobal(zoomPtr); + }); + + //int zoomHeight = (int)(((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution) * (double)surfaceMsg.Length); + + //if (imgSet != null) + //{ + // imgSet.HImage = new HImage(); + // imgSet.HImage.GenImage1("uint2", (int)width, (int)height, bufferPointer); + // imgSet.HImage = imgSet.HImage.ZoomImageSize((int)width, zoomHeight, "constant"); + + + // imgSet.HImage_2 = new HImage(); + // imgSet.HImage_2.GenImage1("uint2", (int)width, (int)height, bufferPointer); + + // imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant"); + //} + + //Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id); } break; + //case GoDataMessageType.SurfaceIntensity: + // { + // GoSurfaceIntensityMsg surfaceMsg = (GoSurfaceIntensityMsg)dataObj; + // long width = surfaceMsg.Width; + // long height = surfaceMsg.Length; + // long bufferSize = width * height; + // IntPtr bufferPointer = surfaceMsg.Data; + + // //int zoomHeight = (surfaceMsg.YResolution * (int)surfaceMsg.Length * 10) / surfaceMsg.XResolution; + + // int zoomHeight = (int)(((double)surfaceMsg.YResolution / (double)surfaceMsg.XResolution) * (double)surfaceMsg.Length); + + // if (imgSet != null) + // { + // imgSet.HImage = new HImage(); + // imgSet.HImage.GenImage1("uint2", (int)width, (int)height, bufferPointer); + // imgSet.HImage = imgSet.HImage.ZoomImageSize((int)width, zoomHeight, "constant"); + + + // imgSet.HImage_2 = new HImage(); + // imgSet.HImage_2.GenImage1("uint2", (int)width, (int)height, bufferPointer); + + // imgSet.HImage_2 = imgSet.HImage_2.ZoomImageSize((int)width, zoomHeight, "constant"); + // } + + // Generate16GrayImageByPointer((int)width, (int)height, bufferPointer, imgSet?.Id); + // } + // 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) + while (sensor.State != GoState.Ready) { - system.Start(); - } - else - { - system.Stop(); + Thread.Sleep(10); } - if (!opConfig.IsSnapshotAction) + do { - return null; - } + try + { + system.Start(); + if (sensor.State != GoState.Running) + { + Thread.Sleep(100); + } + else + { + break; + } + } + catch (Exception ex) + { + LogAsync(DateTime.Now, $"寮�鍚紓甯革細{ex.GetExceptionMessage()}", ""); + } + } while (true); + + LogAsync(DateTime.Now, $"浼犳劅鍣ㄥ惎鍔ㄦ垚鍔�", ""); + } + + if (!opConfig.IsSnapshotAction) + { + return null; } } @@ -144,6 +279,11 @@ } else { + if (!opConfig.IsSnapshotAction) + { + return null; + } + try { dataSet = system.ReceiveData(IIConfig.SnapshotTimeout); @@ -151,14 +291,57 @@ catch (Exception ex) { LogAsync(DateTime.Now, $"{Name}鑾峰彇鍥惧儚寮傚父", ex.GetExceptionMessage()); - return null; + imgSet.HImage = null; + dataSet = null; + //return imgSet; } } - HandleGoData(dataSet, imgSet); - if (imgSet.HImage == null) + if (!IIConfig.IsAsyncMode) { - LogAsync(DateTime.Now, $"{Name}鏈兘鑾峰彇HImage鍥惧儚", ""); + if (!opConfig.IsOpenConnection) + { + while (sensor.State == GoState.Busy) + { + Thread.Sleep(100); + } + + do + { + try + { + system.Stop(); + + if (sensor.State != GoState.Ready) + { + Thread.Sleep(100); + } + else + { + break; + } + } + catch (Exception ex) + { + LogAsync(DateTime.Now, $"鍏抽棴寮傚父锛歿ex.GetExceptionMessage()}", ""); + } + } while (true); + + sensor.Flush(); + LogAsync(DateTime.Now, $"浼犳劅鍣ㄥ叧闂垚鍔�", ""); + } + } + + if (dataSet != null) + { + HandleGoData(dataSet, imgSet); + + dataSet.Dispose(); + + if (imgSet.HImage == null) + { + LogAsync(DateTime.Now, $"{Name}鏈兘鑾峰彇HImage鍥惧儚", ""); + } } return imgSet; @@ -178,7 +361,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(); @@ -234,18 +422,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(); @@ -254,6 +441,8 @@ protected override void Start() { base.Start(); + + //system.Start(); if (IIConfig.IsAsyncMode) system.Start(); @@ -330,17 +519,19 @@ _snapHandle.Set(); } } + + dataSet.Dispose(); } #region 閲嶅啓鍥剧墖淇濆瓨鎿嶄綔 public override async void SaveOriginImage(Bitmap map, Bitmap showImage, string imgSetId) { Task.Run(() => - { - OnImageUpdated?.Invoke(this, showImage, imgSetId); - }); + { + 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; @@ -381,7 +572,7 @@ { await Task.Run(() => { - GoImageSet set = _imageSetList.FirstOrDefault(u => u.Id == imgSetId) as GoImageSet; + GoImageSet set = _imageSetList[imgSetId] as GoImageSet; if (set == null) return; @@ -411,6 +602,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