领胜LDS 键盘AOI检测项目
xcd
2020-06-30 aaae1139f2bb3a55910fff0aa907b3ba6395deea
src/Bro.Device.Gocator/GocatorDriver.cs
@@ -1,9 +1,14 @@
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Lmi3d.GoSdk;
using Lmi3d.GoSdk.Messages;
using Lmi3d.Zen;
using Lmi3d.Zen.Io;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -12,34 +17,236 @@
    [Device("Gocator", "Gocator激光扫描仪", EnumHelper.DeviceAttributeType.Device)]
    public class GocatorDriver : CameraBase
    {
        #region CameraBase
        public override IOperationConfig GetOperationConfigFromDevice()
        {
            throw new NotImplementedException();
            var opConfig = new GocatorOperationConfig();
            GoSetup setup = sensor.Setup;
            opConfig.Exposure = (float)setup.GetExposure(GoRole.Main);
            opConfig.JobName = sensor.DefaultJob;
            return opConfig;
        }
        public override void Snapshot()
        {
            throw new NotImplementedException();
            if (IIConfig.IsAsyncMode)
            {
            }
            else
            {
                GoDataSet dataSet = null;
                try
                {
                    dataSet = system.ReceiveData(IIConfig.SnapshotTimeout);
                }
                catch (Exception ex)
                {
                    LogAsync(DateTime.Now, $"{Name}获取图像异常", ex.GetExceptionMessage());
                    return;
                }
                HandleGoData(dataSet);
            }
        }
        private void HandleGoData(GoDataSet dataSet)
        {
            if (dataSet == null)
                return;
            for (UInt32 i = 0; i < dataSet.Count; i++)
            {
                GoDataMsg dataObj = (GoDataMsg)dataSet.Get(i);
                switch (dataObj.MessageType)
                {
                    //case GoDataMessageType.Stamp:
                    //    {
                    //        GoStampMsg stampMsg = (GoStampMsg)dataObj;
                    //        for (UInt32 j = 0; j < stampMsg.Count; j++)
                    //        {
                    //            GoStamp stamp = stampMsg.Get(j);
                    //            Console.WriteLine("Frame Index = {0}", stamp.FrameIndex);
                    //            Console.WriteLine("Time Stamp = {0}", stamp.Timestamp);
                    //            Console.WriteLine("Encoder Value = {0}", stamp.Encoder);
                    //        }
                    //    }
                    //    break;
                    case GoDataMessageType.Surface:
                        {
                            GoSurfaceMsg surfaceMsg = (GoSurfaceMsg)dataObj;
                            long width = surfaceMsg.Width;
                            long height = surfaceMsg.Length;
                            long bufferSize = width * height;
                            IntPtr bufferPointer = surfaceMsg.Data;
                            //Console.WriteLine("Whole Part Height Map received:");
                            //Console.WriteLine(" Buffer width: {0}", width);
                            //Console.WriteLine(" Buffer height: {0}", height);
                            //short[] ranges = new short[bufferSize];
                            //Marshal.Copy(bufferPointer, ranges, 0, ranges.Length);
                            Generate16GrayImageByPointer((int)(width / 1), (int)(height / 1), bufferPointer, "");
                        }
                        break;
                        //case GoDataMessageType.SurfaceIntensity:
                        //    {
                        //        GoSurfaceIntensityMsg surfaceMsg = (GoSurfaceIntensityMsg)dataObj;
                        //        long width = surfaceMsg.Width;
                        //        long height = surfaceMsg.Length;
                        //        long bufferSize = width * height;
                        //        IntPtr bufferPointeri = surfaceMsg.Data;
                        //        //Console.WriteLine("Whole Part Intensity Image received:");
                        //        //Console.WriteLine(" Buffer width: {0}", width);
                        //        //Console.WriteLine(" Buffer height: {0}", height);
                        //        //byte[] ranges = new byte[bufferSize];
                        //        //Marshal.Copy(bufferPointeri, ranges, 0, ranges.Length);
                        //        //GenerateGrayImageByPointer((int)width, (int)height, bufferPointeri, "");
                        //    }
                        //    break;
                }
            }
        }
        public override ImageSet Snapshot(IOperationConfig config)
        {
            ImageSet imgSet = base.Snapshot(config);
            return imgSet;
        }
        float _currentExposure = 0;
        string _currentJob = "";
        public override void UploadOperationConfig(IOperationConfig config)
        {
            throw new NotImplementedException();
            if (config is GocatorOperationConfig opConfig)
            {
                if (opConfig.Exposure > 0 && opConfig.Exposure != _currentExposure)
                {
                    sensor.Setup.SetExposure(GoRole.Main, opConfig.Exposure);
                    _currentExposure = opConfig.Exposure;
                }
                if (!string.IsNullOrWhiteSpace(opConfig.JobName) && _currentJob != opConfig.JobName)
                {
                    _currentJob = sensor.DefaultJob = opConfig.JobName;
                }
                sensor.Flush();
            }
        }
        protected override void Init()
        {
            throw new NotImplementedException();
            KApiLib.Construct();
            GoSdkLib.Construct();
            system = new GoSystem();
            if (IIConfig.IsUseAccelerator)
            {
                accelerator = new GoAccelerator();
            }
            KIpAddress ip = KIpAddress.Parse(IConfig.CameraIP);
            if (IIConfig.IsUseAccelerator)
            {
                accelerator.Start();
            }
            sensor = system.FindSensorByIpAddress(ip);
            if (IIConfig.IsUseAccelerator)
            {
                accelerator.Attach(sensor);
            }
            sensor.Connect();
            if (IIConfig.IsUseAccelerator)
            {
                sensor.Flush();
                accelerator.Start();
            }
            sensor.Setup.ScanMode = GoMode.Surface;
            system.EnableData(true);
            if (IIConfig.IsAsyncMode)
            {
                system.SetDataHandler(onData);
            }
            _currentExposure = (float)sensor.Setup.GetExposure(GoRole.Main);
            if (IIConfig.DefaultExposure > 0 && _currentExposure != IIConfig.DefaultExposure)
            {
                sensor.Setup.SetExposure(GoRole.Main, IIConfig.DefaultExposure);
                _currentExposure = (float)sensor.Setup.GetExposure(GoRole.Main);
            }
            _currentJob = sensor.DefaultJob;
            if (!string.IsNullOrWhiteSpace(IIConfig.DefaultJob) && _currentJob != IIConfig.DefaultJob)
            {
                _currentJob = sensor.DefaultJob = IIConfig.DefaultJob;
            }
            sensor.Flush();
        }
        protected override void Start()
        {
            base.Start();
            if (IIConfig.IsAsyncMode)
                system.Start();
        }
        protected override void Stop()
        {
            base.Stop();
            if (IIConfig.IsAsyncMode)
                system.Stop();
            if (IIConfig.IsUseAccelerator)
            {
                accelerator.Stop();
            }
            sensor.Disconnect();
        }
        protected override void Pause()
        {
            throw new NotImplementedException();
        }
        protected override void Resume()
        {
            throw new NotImplementedException();
        }
        #endregion
        GoSystem system = null;
        GoAccelerator accelerator = null;
        GoSensor sensor = null;
        public GocatorInitialConfig IIConfig
        {
            get => InitialConfig as GocatorInitialConfig;
        }
        /// <summary>
        /// 异步模式获取数据
        /// </summary>
        /// <param name="data"></param>
        private void onData(KObject data)
        {
            GoDataSet dataSet = (GoDataSet)data;
        }
    }
}