xcd
2020-03-29 95d9d4a9d26323c51087a056c25f88180f1e3c45
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Net;
 
namespace HalconTools
{
    public class LkSensorWrapper
    {
        public SerialportWrapper comPort;
        public const string READ_BOTH_HEAD_CMD = "M0";
        public const string READ_HEAD_A_CMD = "M1";
        public const string READ_HEAD_B_CMD = "M2";
        public const string CHANGE_PROGRAM_CMD = "PW,";
        public const string START_SYNCSTORING_CMD = "AS";
        public const string STOP_SYNCSTORING_CMD = "AP";
        public const string CLEAR_STORING_CMD = "AQ";
        public const string READ_STORING_CMD = "AO";
        public const int ONE_HEAD_RESULT_LEN = 12;
        public const int TWO_HEAD_RESULT_LEN = 21;
        public const int FIVE_RESULT_LEN = 48;
        public const int EIGHT_RESULT_LEN = 75;
        public const double TIMEOUT_SPAN = 3E7;
 
        public LkSensorWrapper()
        {
            comPort = new SerialportWrapper();
            comPort.Open(0, 9600, false);
        }
 
        public LkSensorWrapper(int index)
        {
            comPort = new SerialportWrapper();
            comPort.Open(index, 9600, false);
        }
 
        public LkSensorWrapper(string comName)
        {
            comPort = new SerialportWrapper();
            switch (comName)
            {
                case "COM1":
                    comPort.Open(0, 9600, false);
                    break;
 
                case "COM2":
                    comPort.Open(1, 9600, false);
                    break;
 
                case "COM3":
                    comPort.Open(2, 9600, false);
                    break;
 
                case "COM4":
                    comPort.Open(3, 9600, false);
                    break;
 
                case "COM5":
                    comPort.Open(4, 9600, false);
                    break;
            }
        }
 
        public void ComReadHead(int index, ref float value)
        {
            String measureCommand = "";
 
            if (1 == index)
            {
                measureCommand = READ_HEAD_A_CMD;
            }
            else if (2 == index)
            {
                measureCommand = READ_HEAD_B_CMD;
            }
 
            comPort.Send(measureCommand + "\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < ONE_HEAD_RESULT_LEN)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string[] LkOutput = comPort.builder.ToString().Split(',');
            comPort.Clear();
 
            if (measureCommand.Equals(LkOutput[0]))
            {
                if (LkOutput[1].Contains("FFFF"))
                {
                    value = float.NegativeInfinity;
                }
                else
                {
                    value = Convert.ToSingle(LkOutput[1]);
                }
            }
            else
            {
                value = float.PositiveInfinity;
            }
        }
 
        public void ComReadHeads(ref float valueA, ref float valueB)
        {
            string measureCommand = READ_BOTH_HEAD_CMD;
 
            comPort.Send(measureCommand + "\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < TWO_HEAD_RESULT_LEN)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string[] LkOutput = comPort.builder.ToString().Trim().Split(',');
 
            if (measureCommand.Equals(LkOutput[0]))
            {
                if (LkOutput[1].Contains("FFFF"))
                {
                    valueA = float.NegativeInfinity;
                }
                else
                {
                    valueA = Convert.ToSingle(LkOutput[1]);
                }
                if (LkOutput[2].Contains("FFFF"))
                {
                    valueB = float.NegativeInfinity;
                }
                else
                {
                    valueB = Convert.ToSingle(LkOutput[2]);
                }
 
            }
            else
            {
                valueA = float.PositiveInfinity;
                valueB = float.PositiveInfinity;
            }
        }
 
        public void ChoosePrgram(int progNo)
        {
            comPort.Send(CHANGE_PROGRAM_CMD + progNo.ToString() + "\r");
            comPort.Clear();
        }
 
        public void StartSyncStoring()
        {
            comPort.Send(START_SYNCSTORING_CMD + "\r");
            comPort.Clear();
        }
 
        public void StopSyncStoring()
        {
            comPort.Send(STOP_SYNCSTORING_CMD + "\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < 2)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string[] LkOutput = comPort.builder.ToString().Trim().Split(',');
 
            if (LkOutput[0].Contains(STOP_SYNCSTORING_CMD))
            {
                return;
            }
            else
            {
                return;
            }
        }
 
        public void ClearStoring()
        {
            comPort.Send(CLEAR_STORING_CMD + "\r");
            comPort.Clear();
        }
 
        public void ReadStoring(ref float valueA, ref float valueB)
        {
            string measureCommand = READ_STORING_CMD;
 
            comPort.Send(measureCommand + ",01\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < EIGHT_RESULT_LEN)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string[] LkOutput = comPort.builder.ToString().Trim().Split(',');
 
            if (measureCommand.Equals(LkOutput[0]))
            {
                if (LkOutput.Length < 2)
                    return;
 
                if (LkOutput[1].Contains("FFFF"))
                {
                    valueA = float.NegativeInfinity;
                }
                else
                {
                    valueA = Convert.ToSingle(LkOutput[1]);
                }
                if (LkOutput[2].Contains("FFFF"))
                {
                    valueB = float.NegativeInfinity;
                }
                else
                {
                    valueB = Convert.ToSingle(LkOutput[2]);
                }
 
            }
            else
            {
                valueA = float.PositiveInfinity;
                valueB = float.PositiveInfinity;
            }
        }
    }
 
    public class HlSensorWrapper
    {
        public SerialportWrapper comPort;
        public const string READ_BOTH_HEAD_CMD = "%EE#RMA5**";
        public const string READ_HEAD_A_CMD = "%EE#RMD3**";
        public const string READ_HEAD_B_CMD = "%EE#RMD4**";
        public const string READ_ONE_HEAD_REPLY = "%EE$RMD";
        public const string READ_TWO_HEADS_REPLY = "%EE$RMA";
        public const int ONE_HEAD_RESULT_LEN = 21;
        public const int TWO_HEAD_RESULT_LEN = 32;
        public const int HEAD_A_RESULT_STARTINDEX = 7;
        public const int HEAD_B_RESULT_STARTINDEX = 18;
        public const int HEAD_RESULT_LENGTH = 11;
        public const double TIMEOUT_SPAN = 3E7;
 
        public HlSensorWrapper()
        {
            comPort = new SerialportWrapper();
            comPort.Open(0, 115200, false);
        }
 
        public void ComReadHead(int index, ref float value)
        {
            String measureCommand = "";
 
            if (1 == index)
            {
                measureCommand = READ_HEAD_A_CMD;
            }
            else if (2 == index)
            {
                measureCommand = READ_HEAD_B_CMD;
            }
 
            comPort.Send(measureCommand + "\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < ONE_HEAD_RESULT_LEN)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string HlOutput = comPort.builder.ToString().Trim();
            comPort.Clear();
 
            if (HlOutput.Contains(READ_ONE_HEAD_REPLY))
            {
                if (HlOutput.Contains("FFFF"))
                {
                    value = float.NegativeInfinity;
                }
                else
                {
                    value = Convert.ToSingle(HlOutput.Substring(HEAD_A_RESULT_STARTINDEX, HEAD_RESULT_LENGTH));
                }
            }
            else
            {
                value = float.PositiveInfinity;
            }
        }
 
        public void ComReadHeads(ref float valueA, ref float valueB)
        {
            string measureCommand = READ_BOTH_HEAD_CMD;
 
            comPort.Send(measureCommand + "\r");
            comPort.Clear();
 
            DateTime startTime = DateTime.Now;
            while (comPort.builder.Length < TWO_HEAD_RESULT_LEN)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime.Ticks - startTime.Ticks > Convert.ToInt32(TIMEOUT_SPAN))
                {
                    break;
                }
            }
 
            string[] HlOutput = comPort.builder.ToString().Trim().Split(',');
 
            if (HlOutput[0].Contains(READ_TWO_HEADS_REPLY))
            {
                valueA = Convert.ToSingle(HlOutput[0].Substring(HEAD_A_RESULT_STARTINDEX, HEAD_RESULT_LENGTH));
                valueB = Convert.ToSingle(HlOutput[0].Substring(HEAD_B_RESULT_STARTINDEX, HEAD_RESULT_LENGTH));
            }
            else
            {
                valueA = float.PositiveInfinity;
                valueB = float.PositiveInfinity;
            }
        }
    }
 
    public class ZwSensorWrapper
    {
        public SocketTools st;
        public IPAddress remoteIpAddr;
        public int remotePort;
        public const string READ_HEAD_A_CMD = "M";
        public const string READ_ERROR_REPLY = "ER";
        public const string READ_INVALID_REPLY = "----";
        public const int ONE_HEAD_RESULT_LEN = 21;
        public const int TWO_HEAD_RESULT_LEN = 32;
        public const int HEAD_A_RESULT_STARTINDEX = 7;
        public const int HEAD_B_RESULT_STARTINDEX = 18;
        public const int HEAD_RESULT_LENGTH = 11;
        public const double TIMEOUT_SPAN = 3E7;
 
        public ZwSensorWrapper(string remoteIpAddr, int remotePort)
        {
            this.remoteIpAddr = IPAddress.Parse(remoteIpAddr);
            this.remotePort = remotePort;
 
            st = new SocketTools(9600, this.remotePort, this.remoteIpAddr, new System.Net.IPAddress(0));
        }
 
        public void ComReadHead(int index, ref float value)
        {
            string str = "";
 
            str = READ_HEAD_A_CMD + "\r";
 
            st.SendAndReceiveMessage(remoteIpAddr, 9600, ref str);
 
            if (!str.Contains(READ_ERROR_REPLY))
            {
                if (str.Contains(READ_INVALID_REPLY))
                {
                    value = float.NegativeInfinity;
                }
                else
                {
                    value = Convert.ToSingle(str) / 1000000;
                }
            }
            else
            {
                value = float.PositiveInfinity;
            }
        }
 
        public void ComReadHeads(ref float valueA, ref float valueB)
        {
            ComReadHead(0, ref valueA);
            valueB = valueA;
        }
 
    }
}