领胜LDS 键盘AOI检测项目
xcd
2020-06-30 aaae1139f2bb3a55910fff0aa907b3ba6395deea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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;
 
namespace Bro.Device.Gocator
{
    [Device("Gocator", "Gocator激光扫描仪", EnumHelper.DeviceAttributeType.Device)]
    public class GocatorDriver : CameraBase
    {
        #region CameraBase
        public override IOperationConfig GetOperationConfigFromDevice()
        {
            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()
        {
            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)
        {
            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()
        {
            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()
        {
        }
 
        protected override void Resume()
        {
        }
        #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;
        }
    }
}