0% found this document useful (0 votes)
12 views

Code Visual

The document contains code for a GUI application that controls a PLC device. It defines functions for handling button clicks and mouse events to set devices on the PLC. It also contains code to display messages on the GUI window and connect to the PLC.

Uploaded by

Lê Vũ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Code Visual

The document contains code for a GUI application that controls a PLC device. It defines functions for handling button clicks and mouse events to set devices on the PLC. It also contains code to display messages on the GUI window and connect to the PLC.

Uploaded by

Lê Vũ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.

cs 1
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using HalconDotNet; // add thư viện kết nối với halcon
10 using System.Windows.Forms;
11 using ActUtlTypeLib; // add thư viện kết nối với plc
12
13 namespace TEST_6122021
14 {
15 public partial class Form1 : Form
16 {
17
18 HWindow HWIN;
19 HTuple hv_WidthPN, hv_HeightPN;
20 public ActUtlType mx_plc = new ActUtlType(); // khởi tạo cổng kết nối
plc
21
22 HObject ho_SearchImage = null, ho_FoundComponents2 = null;
23 HObject ho_Circle2 = null, ho_Cross2 = null;
24
25
26 double [] ROW;
27 double [] COLUMN;
28
29 int count = 0;
30 int temp = 0;
31 // Local control variables
32
33 HTuple hv_AcqHandle = new HTuple(), hv_WindowHandle = new HTuple();
34 HTuple hv_ComponentModel2 = new HTuple(), hv_ModelStart2 = new HTuple
();
35 HTuple hv_ModelEnd2 = new HTuple(), hv_Score_2 = new HTuple();
36 HTuple hv_RowComp2 = new HTuple(), hv_ColumnComp2 = new HTuple();
37 HTuple hv_AngleComp2 = new HTuple(), hv_ScoreComp2 = new HTuple();
38 HTuple hv_ModelComp2 = new HTuple(), hv_Match2 = new HTuple();
39 HTuple hv_RowCompInst2 = new HTuple(), hv_ColumnCompInst2 = new HTuple
();
40 HTuple hv_toadohang = new HTuple(), hv_toadocot = new HTuple();
41 private void hWindowControl1_HMouseMove( object sender, HMouseEventArgs
e)
42 {
43
44 }
45
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 2
46 private void button4_MouseDown(object sender, MouseEventArgs e)
47 {
48 mx_plc.SetDevice( "M3000", 1);
49 }
50
51 private void button4_MouseUp(object sender, MouseEventArgs e)
52 {
53 mx_plc.SetDevice( "M3000", 0);
54 }
55
56 private void button5_MouseDown(object sender, MouseEventArgs e)
57 {
58 mx_plc.SetDevice( "M3100", 1);
59 }
60
61 private void button5_MouseUp(object sender, MouseEventArgs e)
62 {
63 mx_plc.SetDevice( "M3100", 0);
64 }
65
66 private void button6_MouseDown(object sender, MouseEventArgs e)
67 {
68 mx_plc.SetDevice( "M3010", 1);
69 }
70
71 private void button6_MouseUp(object sender, MouseEventArgs e)
72 {
73 mx_plc.SetDevice( "M3010", 0);
74 }
75
76 private void button7_MouseUp(object sender, MouseEventArgs e)
77 {
78 mx_plc.SetDevice( "M3112", 0);
79 }
80
81 private void button7_MouseDown(object sender, MouseEventArgs e)
82 {
83 mx_plc.SetDevice( "M3112", 1);
84 }
85
86 private void button4_Click(object sender, EventArgs e)
87 {
88
89 }
90
91 private void button5_Click(object sender, EventArgs e)
92 {
93
94 }
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 3
95
96 private void button6_Click(object sender, EventArgs e)
97 {
98
99 }
100
101 private void button7_Click(object sender, EventArgs e)
102 {
103
104 }
105
106 private void label3_Click(object sender, EventArgs e)
107 {
108
109 }
110
111 private void label4_Click(object sender, EventArgs e)
112 {
113
114 }
115
116 private void button6_Click_1(object sender, EventArgs e)
117 {
118
119 }
120
121 private void textBox2_TextChanged(object sender, EventArgs e)
122 {
123
124 }
125
126 private void label2_Click(object sender, EventArgs e)
127 {
128
129 }
130
131 private void button9_MouseUp(object sender, MouseEventArgs e)
132 {
133 mx_plc.SetDevice( "M0", 0);
134 }
135
136 private void button9_MouseDown(object sender, MouseEventArgs e)
137 {
138 mx_plc.SetDevice( "M0", 1);
139 }
140
141 private void button3_MouseUp(object sender, MouseEventArgs e)
142 {
143 mx_plc.SetDevice( "m10", 0);
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 4
144 }
145
146 private void button3_MouseDown(object sender, MouseEventArgs e)
147 {
148 mx_plc.SetDevice( "m10", 1);
149
150 }
151
152 private void button10_Click(object sender, EventArgs e)
153 {
154
155 }
156
157 private void button9_Click(object sender, EventArgs e)
158 {
159
160 }
161
162 private void button8_Click(object sender, EventArgs e)
163 {
164
165 }
166
167 private void button8_MouseDown(object sender, MouseEventArgs e) ///
button start
168 {
169 mx_plc.SetDevice( "m10", 1);
170 // mx_plc.SetDevice("m10", 0);
171 }
172
173 private void button8_MouseUp(object sender, MouseEventArgs e)
174 {
175 mx_plc.SetDevice( "m10", 0);
176 }
177
178 private void button10_MouseDown(object sender, MouseEventArgs e) ///
button ve goc
179 {
180 mx_plc.SetDevice( "m12", 1);
181 }
182
183 private void button8_Click_1(object sender, EventArgs e)
184 {
185
186 }
187
188 private void button10_MouseUp(object sender, MouseEventArgs e)
189 {
190 mx_plc.SetDevice( "m12", 0);
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 5
191 }
192
193 HTuple hv_AngleCompInst2 = new HTuple(), hv_ScoreCompInst2 = new HTuple
();
194
195
196 private void Form1_Load(object sender, EventArgs e)
197 {
198 HWIN = hWindowControl1.HalconWindow; // tạo khung hiện ảnh halcon
199 Control.CheckForIllegalCrossThreadCalls = false; //
200 mx_plc.ActLogicalStationNumber = 1; // port trên mx component
201 mx_plc.Open(); // mở kết nối plc
202 }
203
204 private void button2_Click(object sender, EventArgs e) /// button
connection
205 {
206 connection();
207 connection_plc();
208 }
209
210
211 public void disp_message(HTuple hv_WindowHandle, HTuple hv_String,
HTuple hv_CoordSystem,
212 HTuple hv_Row, HTuple hv_Column, HTuple
hv_Color, HTuple hv_Box)
213 {
214
215 HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new
HTuple();
216 HTuple hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
217 HTuple hv_Column_COPY_INP_TMP = new HTuple(hv_Column);
218 HTuple hv_CoordSystem_COPY_INP_TMP = new HTuple(hv_CoordSystem);
219 HTuple hv_Row_COPY_INP_TMP = new HTuple(hv_Row);
220
221 // Initialize local and output iconic variables
222 try
223 {
224 if ((int)((new HTuple(hv_Row_COPY_INP_TMP.TupleEqual( new HTuple
()))).TupleOr(
225 new HTuple(hv_Column_COPY_INP_TMP.TupleEqual( new HTuple
())))) != 0)
226 {
227
228 hv_Color_COPY_INP_TMP.Dispose();
229 hv_Column_COPY_INP_TMP.Dispose();
230 hv_CoordSystem_COPY_INP_TMP.Dispose();
231 hv_Row_COPY_INP_TMP.Dispose();
232 hv_GenParamName.Dispose();
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 6
233 hv_GenParamValue.Dispose();
234
235 return ;
236 }
237 if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1)) ) != 0)
238 {
239 hv_Row_COPY_INP_TMP.Dispose();
240 hv_Row_COPY_INP_TMP = 12;
241 }
242 if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1)) ) !=
0)
243 {
244 hv_Column_COPY_INP_TMP.Dispose();
245 hv_Column_COPY_INP_TMP = 12;
246 }
247 //
248 //Convert the parameter Box to generic parameters.
249 hv_GenParamName.Dispose();
250 hv_GenParamName = new HTuple();
251 hv_GenParamValue.Dispose();
252 hv_GenParamValue = new HTuple();
253 if ((int)(new HTuple((new HTuple(hv_Box.TupleLength
())).TupleGreater(0))) != 0)
254 {
255 if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual
("false"))) != 0)
256 {
257 //Display no box
258 using (HDevDisposeHelper dh = new HDevDisposeHelper())
259 {
260 {
261 HTuple
262 ExpTmpLocalVar_GenParamName =
hv_GenParamName.TupleConcat(
263 "box" );
264 hv_GenParamName.Dispose();
265 hv_GenParamName = ExpTmpLocalVar_GenParamName;
266 }
267 }
268 using (HDevDisposeHelper dh = new HDevDisposeHelper())
269 {
270 {
271 HTuple
272 ExpTmpLocalVar_GenParamValue =
hv_GenParamValue.TupleConcat(
273 "false" );
274 hv_GenParamValue.Dispose();
275 hv_GenParamValue =
ExpTmpLocalVar_GenParamValue;
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 7
276 }
277 }
278 }
279 else if ((int)(new HTuple(((hv_Box.TupleSelect
(0))).TupleNotEqual(
280 "true" ))) != 0)
281 {
282 //Set a color other than the default.
283 using (HDevDisposeHelper dh = new HDevDisposeHelper())
284 {
285 {
286 HTuple
287 ExpTmpLocalVar_GenParamName =
hv_GenParamName.TupleConcat(
288 "box_color" );
289 hv_GenParamName.Dispose();
290 hv_GenParamName = ExpTmpLocalVar_GenParamName;
291 }
292 }
293 using (HDevDisposeHelper dh = new HDevDisposeHelper())
294 {
295 {
296 HTuple
297 ExpTmpLocalVar_GenParamValue =
hv_GenParamValue.TupleConcat(
298 hv_Box.TupleSelect(0));
299 hv_GenParamValue.Dispose();
300 hv_GenParamValue =
ExpTmpLocalVar_GenParamValue;
301 }
302 }
303 }
304 }
305 if ((int)(new HTuple((new HTuple(hv_Box.TupleLength
())).TupleGreater(1))) != 0)
306 {
307 if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual
("false"))) != 0)
308 {
309 //Display no shadow.
310 using (HDevDisposeHelper dh = new HDevDisposeHelper())
311 {
312 {
313 HTuple
314 ExpTmpLocalVar_GenParamName =
hv_GenParamName.TupleConcat(
315 "shadow" );
316 hv_GenParamName.Dispose();
317 hv_GenParamName = ExpTmpLocalVar_GenParamName;
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 8
318 }
319 }
320 using (HDevDisposeHelper dh = new HDevDisposeHelper())
321 {
322 {
323 HTuple
324 ExpTmpLocalVar_GenParamValue =
hv_GenParamValue.TupleConcat(
325 "false" );
326 hv_GenParamValue.Dispose();
327 hv_GenParamValue =
ExpTmpLocalVar_GenParamValue;
328 }
329 }
330 }
331 else if ((int)(new HTuple(((hv_Box.TupleSelect
(1))).TupleNotEqual(
332 "true" ))) != 0)
333 {
334 //Set a shadow color other than the default.
335 using (HDevDisposeHelper dh = new HDevDisposeHelper())
336 {
337 {
338 HTuple
339 ExpTmpLocalVar_GenParamName =
hv_GenParamName.TupleConcat(
340 "shadow_color" );
341 hv_GenParamName.Dispose();
342 hv_GenParamName = ExpTmpLocalVar_GenParamName;
343 }
344 }
345 using (HDevDisposeHelper dh = new HDevDisposeHelper())
346 {
347 {
348 HTuple
349 ExpTmpLocalVar_GenParamValue =
hv_GenParamValue.TupleConcat(
350 hv_Box.TupleSelect(1));
351 hv_GenParamValue.Dispose();
352 hv_GenParamValue =
ExpTmpLocalVar_GenParamValue;
353 }
354 }
355 }
356 }
357 //Restore default CoordSystem behavior.
358 if ((int)(new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual
("window"))) != 0)
359 {
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 9
360 hv_CoordSystem_COPY_INP_TMP.Dispose();
361 hv_CoordSystem_COPY_INP_TMP = "image";
362 }
363 //
364 if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual( ""))) !=
0)
365 {
366 //disp_text does not accept an empty string for Color.
367 hv_Color_COPY_INP_TMP.Dispose();
368 hv_Color_COPY_INP_TMP = new HTuple();
369 }
370 //
371 HOperatorSet.DispText(hv_WindowHandle, hv_String,
hv_CoordSystem_COPY_INP_TMP,
372 hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP,
hv_Color_COPY_INP_TMP, hv_GenParamName,
373 hv_GenParamValue);
374
375 hv_Color_COPY_INP_TMP.Dispose();
376 hv_Column_COPY_INP_TMP.Dispose();
377 hv_CoordSystem_COPY_INP_TMP.Dispose();
378 hv_Row_COPY_INP_TMP.Dispose();
379 hv_GenParamName.Dispose();
380 hv_GenParamValue.Dispose();
381
382 return;
383 }
384 catch (HalconException HDevExpDefaultException)
385 {
386
387 hv_Color_COPY_INP_TMP.Dispose();
388 hv_Column_COPY_INP_TMP.Dispose();
389 hv_CoordSystem_COPY_INP_TMP.Dispose();
390 hv_Row_COPY_INP_TMP.Dispose();
391 hv_GenParamName.Dispose();
392 hv_GenParamValue.Dispose();
393
394 throw HDevExpDefaultException;
395 }
396 }
397
398
399
400 public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size,
HTuple hv_Font,
401 HTuple hv_Bold, HTuple hv_Slant)
402 {
403
404
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 10
405 HTuple hv_OS = new HTuple(), hv_Fonts = new HTuple();
406 HTuple hv_Style = new HTuple(), hv_Exception = new HTuple();
407 HTuple hv_AvailableFonts = new HTuple(), hv_Fdx = new HTuple();
408 HTuple hv_Indices = new HTuple();
409 HTuple hv_Font_COPY_INP_TMP = new HTuple(hv_Font);
410 HTuple hv_Size_COPY_INP_TMP = new HTuple(hv_Size);
411
412 hv_OS.Dispose();
413 HOperatorSet.GetSystem("operating_system", out hv_OS);
414 if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual( new
HTuple()))).TupleOr(
415 new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1))) ) != 0)
416 {
417 hv_Size_COPY_INP_TMP.Dispose();
418 hv_Size_COPY_INP_TMP = 16;
419 }
420 if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual
("Win"))) != 0)
421 {
422 //Restore previous behaviour
423 using (HDevDisposeHelper dh = new HDevDisposeHelper())
424 {
425 {
426 HTuple
427 ExpTmpLocalVar_Size = ((1.13677 *
hv_Size_COPY_INP_TMP)).TupleInt()
428 ;
429 hv_Size_COPY_INP_TMP.Dispose();
430 hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
431 }
432 }
433 }
434 else
435 {
436 using (HDevDisposeHelper dh = new HDevDisposeHelper())
437 {
438 {
439 HTuple
440 ExpTmpLocalVar_Size =
hv_Size_COPY_INP_TMP.TupleInt()
441 ;
442 hv_Size_COPY_INP_TMP.Dispose();
443 hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
444 }
445 }
446 }
447 if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual
("Courier"))) != 0)
448 {
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 11
449 hv_Fonts.Dispose();
450 hv_Fonts = new HTuple();
451 hv_Fonts[0] = "Courier";
452 hv_Fonts[1] = "Courier 10 Pitch";
453 hv_Fonts[2] = "Courier New";
454 hv_Fonts[3] = "CourierNew";
455 hv_Fonts[4] = "Liberation Mono";
456 }
457 else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual
("mono"))) != 0)
458 {
459 hv_Fonts.Dispose();
460 hv_Fonts = new HTuple();
461 hv_Fonts[0] = "Consolas";
462 hv_Fonts[1] = "Menlo";
463 hv_Fonts[2] = "Courier";
464 hv_Fonts[3] = "Courier 10 Pitch";
465 hv_Fonts[4] = "FreeMono";
466 hv_Fonts[5] = "Liberation Mono";
467 }
468 else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual
("sans"))) != 0)
469 {
470 hv_Fonts.Dispose();
471 hv_Fonts = new HTuple();
472 hv_Fonts[0] = "Luxi Sans";
473 hv_Fonts[1] = "DejaVu Sans";
474 hv_Fonts[2] = "FreeSans";
475 hv_Fonts[3] = "Arial";
476 hv_Fonts[4] = "Liberation Sans";
477 }
478 else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual
("serif"))) != 0)
479 {
480 hv_Fonts.Dispose();
481 hv_Fonts = new HTuple();
482 hv_Fonts[0] = "Times New Roman";
483 hv_Fonts[1] = "Luxi Serif";
484 hv_Fonts[2] = "DejaVu Serif";
485 hv_Fonts[3] = "FreeSerif";
486 hv_Fonts[4] = "Utopia";
487 hv_Fonts[5] = "Liberation Serif";
488 }
489 else
490 {
491 hv_Fonts.Dispose();
492 hv_Fonts = new HTuple(hv_Font_COPY_INP_TMP);
493 }
494 hv_Style.Dispose();
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 12
495 hv_Style = "";
496 if ((int)(new HTuple(hv_Bold.TupleEqual("true"))) != 0)
497 {
498 using (HDevDisposeHelper dh = new HDevDisposeHelper())
499 {
500 {
501 HTuple
502 ExpTmpLocalVar_Style = hv_Style + "Bold";
503 hv_Style.Dispose();
504 hv_Style = ExpTmpLocalVar_Style;
505 }
506 }
507 }
508 else if ((int)(new HTuple(hv_Bold.TupleNotEqual("false"))) !=
0)
509 {
510 hv_Exception.Dispose();
511 hv_Exception = "Wrong value of control parameter Bold" ;
512 throw new HalconException(hv_Exception);
513 }
514 if ((int)(new HTuple(hv_Slant.TupleEqual("true"))) != 0)
515 {
516 using (HDevDisposeHelper dh = new HDevDisposeHelper())
517 {
518 {
519 HTuple
520 ExpTmpLocalVar_Style = hv_Style + "Italic";
521 hv_Style.Dispose();
522 hv_Style = ExpTmpLocalVar_Style;
523 }
524 }
525 }
526 else if ((int)(new HTuple(hv_Slant.TupleNotEqual("false"))) !=
0)
527 {
528 hv_Exception.Dispose();
529 hv_Exception = "Wrong value of control parameter Slant" ;
530 throw new HalconException(hv_Exception);
531 }
532 if ((int)(new HTuple(hv_Style.TupleEqual(""))) != 0)
533 {
534 hv_Style.Dispose();
535 hv_Style = "Normal";
536 }
537 hv_AvailableFonts.Dispose();
538 HOperatorSet.QueryFont(hv_WindowHandle, out hv_AvailableFonts);
539 hv_Font_COPY_INP_TMP.Dispose();
540 hv_Font_COPY_INP_TMP = "";
541 for (hv_Fdx = 0; (int)hv_Fdx <= (int)((new HTuple
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 13
(hv_Fonts.TupleLength())) - 1); hv_Fdx = (int)hv_Fdx + 1)
542 {
543 hv_Indices.Dispose();
544 using (HDevDisposeHelper dh = new HDevDisposeHelper())
545 {
546 hv_Indices = hv_AvailableFonts.TupleFind(
547 hv_Fonts.TupleSelect(hv_Fdx));
548 }
549 if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength
())).TupleGreater(
550 0)) ) != 0)
551 {
552 if ((int)(new HTuple(((hv_Indices.TupleSelect
(0))).TupleGreaterEqual(0))) != 0)
553 {
554 hv_Font_COPY_INP_TMP.Dispose();
555 using (HDevDisposeHelper dh = new HDevDisposeHelper
())
556 {
557 hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(
558 hv_Fdx);
559 }
560 break ;
561 }
562 }
563 }
564 if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( ""))) !=
0)
565 {
566 throw new HalconException("Wrong value of control parameter
Font");
567 }
568 using (HDevDisposeHelper dh = new HDevDisposeHelper())
569 {
570 {
571 HTuple
572 ExpTmpLocalVar_Font = (((hv_Font_COPY_INP_TMP + "-")
+ hv_Style) + "-") + hv_Size_COPY_INP_TMP;
573 hv_Font_COPY_INP_TMP.Dispose();
574 hv_Font_COPY_INP_TMP = ExpTmpLocalVar_Font;
575 }
576 }
577 HOperatorSet.SetFont(hv_WindowHandle, hv_Font_COPY_INP_TMP);
578
579 hv_Font_COPY_INP_TMP.Dispose();
580 hv_Size_COPY_INP_TMP.Dispose();
581 hv_OS.Dispose();
582 hv_Fonts.Dispose();
583 hv_Style.Dispose();
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 14
584 hv_Exception.Dispose();
585 hv_AvailableFonts.Dispose();
586 hv_Fdx.Dispose();
587 hv_Indices.Dispose();
588
589 return;
590 }
591 catch (HalconException HDevExpDefaultException)
592 {
593
594 hv_Font_COPY_INP_TMP.Dispose();
595 hv_Size_COPY_INP_TMP.Dispose();
596 hv_OS.Dispose();
597 hv_Fonts.Dispose();
598 hv_Style.Dispose();
599 hv_Exception.Dispose();
600 hv_AvailableFonts.Dispose();
601 hv_Fdx.Dispose();
602 hv_Indices.Dispose();
603
604 throw HDevExpDefaultException;
605 }
606 }
607
608 private void button3_Click(object sender, EventArgs e) /// button go
online
609 {
610 hv_toadohang.Dispose();
611 hv_toadohang = new HTuple();
612 hv_toadocot.Dispose();
613 hv_toadocot = new HTuple();
614
615
616 int count = 0;
617 while (true)
618
619 {
620
621
622 detect();
623
624 textBox1.Text = hv_RowCompInst2.ToString();
625 double a = Convert.ToDouble(textBox1.Text);
626 int b = Convert.ToInt32(a);
627 mx_plc.SetDevice( "D20", b);
628
629
630 textBox2.Text = hv_ColumnCompInst2.ToString();
631 Double c = Convert.ToDouble(textBox2.Text);
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 15
632 int d = Convert.ToInt32(c);
633 mx_plc.SetDevice( "D104", d);
634
635 textBox3.Text = hv_Match2.ToString();
636 Double nb = Convert.ToDouble(textBox3.Text);
637 int f = Convert.ToInt32(nb);
638 mx_plc.SetDevice( "D50", f);
639
640
641
642 if (f == 0)
643 {
644 break ;
645 }
646 else
647 {
648 count++;
649 mx_plc.SetDevice( "D50", count);
650 }
651
652 mx_plc.SetDevice( "m10", 1);
653 mx_plc.SetDevice( "m10", 0);
654
655 int TTK = 0;
656
657
658 mx_plc.SetDevice( "m1238", 0);
659 while (true)
660 {
661 mx_plc.GetDevice( "M1236", out TTK);
662
663 if (TTK == 1)
664 {
665 mx_plc.SetDevice( "m1238", 1);
666 break ;
667 }
668 }
669
670 }
671
672
673
674
675
676 }
677
678
679
680
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 16
681
682
683
684
685 public void detect()
686 {
687 ho_SearchImage.Dispose();
688 HOperatorSet.GrabImage(out ho_SearchImage, hv_AcqHandle); // lấy
hình trực tiếp từ camera
689 HDevWindowStack.Push(HWIN);
690 HDevWindowStack.SetActive(HWIN);
691
692 //set_display_font(hv_WindowHandle, 30, "mono", "true", "false");
693 if (HDevWindowStack.IsOpen())
694 {
695 HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
696 }
697 if (HDevWindowStack.IsOpen())
698 {
699 HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12);
700 }
701
702 HOperatorSet.GetImageSize(ho_SearchImage, out hv_WidthPN, out
hv_HeightPN);
703
704 if (HDevWindowStack.IsOpen())
705 {
706 using (HDevDisposeHelper dh = new HDevDisposeHelper())
707 {
708 HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0,
hv_HeightPN - 1, hv_WidthPN - 1);
709 }
710 }
711 if (HDevWindowStack.IsOpen())
712 {
713 HOperatorSet.DispObj(ho_SearchImage, HDevWindowStack.GetActive
());
714 }
715 //tim vat mau bang thu vien Halcon
716 using (HDevDisposeHelper dh = new HDevDisposeHelper())
717 {
718 hv_ModelStart2.Dispose(); hv_ModelEnd2.Dispose();
hv_Score_2.Dispose(); hv_RowComp2.Dispose();
hv_ColumnComp2.Dispose(); hv_AngleComp2.Dispose();
hv_ScoreComp2.Dispose(); hv_ModelComp2.Dispose();
719 HOperatorSet.FindComponentModel(ho_SearchImage,
hv_ComponentModel2, 0, -((new HTuple(120)).TupleRad()
720 ), (new HTuple(120)).TupleRad(), 0.2, 10, 0.5,
"stop_search", "search_from_best",
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 17
721 "from_all", 0.2, "least_squares_high", 0, 0.9, out
hv_ModelStart2, out hv_ModelEnd2,
722 out hv_Score_2, out hv_RowComp2, out hv_ColumnComp2, out
hv_AngleComp2,
723 out hv_ScoreComp2, out hv_ModelComp2);
724 }
725 ///////////////////
726 for (hv_Match2 = 0; (int)hv_Match2 <= (int)((new HTuple
(hv_ModelStart2.TupleLength()
727 )) - 1); hv_Match2 = (int)hv_Match2 + 1)
728 {
729 ho_FoundComponents2.Dispose(); hv_RowCompInst2.Dispose();
hv_ColumnCompInst2.Dispose(); hv_AngleCompInst2.Dispose();
hv_ScoreCompInst2.Dispose();
730 HOperatorSet.GetFoundComponentModel(out ho_FoundComponents2,
hv_ComponentModel2,
731 hv_ModelStart2, hv_ModelEnd2, hv_RowComp2, hv_ColumnComp2,
hv_AngleComp2,
732 hv_ScoreComp2, hv_ModelComp2, hv_Match2, "false", out
hv_RowCompInst2,
733 out hv_ColumnCompInst2, out hv_AngleCompInst2, out
hv_ScoreCompInst2);
734 //if (HDevWindowStack.IsOpen())
735 //{
736 // HOperatorSet.DispObj(ho_FoundComponents2,
HDevWindowStack.GetActive()
737 // );
738 //}
739
740 if (HDevWindowStack.IsOpen())
741 {
742 HOperatorSet.SetColor(HDevWindowStack.GetActive(),
"green");
743 }
744 if (HDevWindowStack.IsOpen())
745 {
746 HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 5);
747 }
748 using (HDevDisposeHelper dh = new HDevDisposeHelper())
749 {
750 ho_Circle2.Dispose();
751 HOperatorSet.GenCircle(out ho_Circle2, hv_RowCompInst2,
hv_ColumnCompInst2,
752 300);
753 }
754 if (HDevWindowStack.IsOpen())
755 {
756 HOperatorSet.DispObj(ho_Circle2, HDevWindowStack.GetActive
());
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 18
757 }
758 using (HDevDisposeHelper dh = new HDevDisposeHelper())
759 {
760 ho_Cross2.Dispose();
761 HOperatorSet.GenCrossContourXld(out ho_Cross2,
hv_RowCompInst2, hv_ColumnCompInst2,
762 20, hv_AngleCompInst2 + ((new HTuple(45)).TupleRad()));
763 }
764 if (HDevWindowStack.IsOpen())
765 {
766 HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
767 }
768 if (HDevWindowStack.IsOpen())
769 {
770 HOperatorSet.DispObj(ho_Cross2, HDevWindowStack.GetActive
());
771 }
772
773 if (hv_toadohang == null)
774 hv_toadohang = new HTuple();
775 hv_toadohang[hv_Match2] = hv_RowCompInst2;
776 if (hv_toadocot == null)
777 hv_toadocot = new HTuple();
778 hv_toadocot[hv_Match2] = hv_ColumnCompInst2;
779
780 //// hien thi chu
781
782 HOperatorSet.SetTposition(HWIN, hv_RowCompInst2,
hv_ColumnCompInst2);
783 using (HDevDisposeHelper dh = new HDevDisposeHelper())
784 {
785 HOperatorSet.WriteString(HWIN, hv_RowCompInst2);
786 }
787
788 HOperatorSet.SetTposition(HWIN, hv_RowCompInst2 + 100,
hv_ColumnCompInst2);
789 using (HDevDisposeHelper dh = new HDevDisposeHelper())
790 {
791 HOperatorSet.WriteString(HWIN, hv_ColumnCompInst2);
792 }
793
794
795
796
797 }
798 }
799
800 public void connection_plc()
801 {
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 19
802 try
803 {
804 mx_plc.Close();
805 mx_plc.Open();
806
807 }
808 catch (Exception)
809 {
810
811 }
812
813 }
814 public void connection ()
815 {
816
817 HOperatorSet.CloseFramegrabber(hv_AcqHandle);
818 try
819 {
820 hv_AcqHandle.Dispose();
821 HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0,
"progressive",
822 -1, "default", -1, "false", "default", "default", 0, -1,
out hv_AcqHandle);
823
824 HOperatorSet.SetFramegrabberParam(hv_AcqHandle,
"AcquisitionFrameRateEnable" ,
825 1);
826 HOperatorSet.SetFramegrabberParam(hv_AcqHandle,
"AcquisitionFrameRateAbs" ,
827 100.0);
828 HOperatorSet.SetFramegrabberParam(hv_AcqHandle,
"ExposureTimeAbs", 35000);
829
830 // HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
831 button2.BackColor = Color.Green;
832 button2.Text = "CONNECTED";
833 }
834 catch (Exception)
835 {
836 button2.BackColor = Color.Red;
837
838 }
839
840
841 }
842 private void button1_Click(object sender, EventArgs e) //// button take
image
843 {
844
..._61220211\TEST_6122021\TEST_6122021\TEST_6122021\Form1.cs 20
845
846 ho_SearchImage.Dispose();
847 HOperatorSet.GrabImage(out ho_SearchImage, hv_AcqHandle);
848 HDevWindowStack.Push(HWIN);
849 HDevWindowStack.SetActive(HWIN);
850
851 HOperatorSet.GetImageSize(ho_SearchImage, out hv_WidthPN, out
hv_HeightPN);
852
853 if (HDevWindowStack.IsOpen())
854 {
855 using (HDevDisposeHelper dh = new HDevDisposeHelper())
856 {
857 HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0,
hv_HeightPN - 1, hv_WidthPN - 1);
858 }
859 }
860 if (HDevWindowStack.IsOpen())
861 {
862 HOperatorSet.DispObj(ho_SearchImage, HDevWindowStack.GetActive
());
863 }
864 }
865
866
867
868
869
870 public Form1()
871 {
872 InitializeComponent();
873 // Initialize local and output iconic variables
874 HOperatorSet.GenEmptyObj(out ho_SearchImage);
875 HOperatorSet.GenEmptyObj(out ho_FoundComponents2);
876 HOperatorSet.GenEmptyObj(out ho_Circle2);
877 HOperatorSet.GenEmptyObj(out ho_Cross2);
878
879 //////////////////////
880 //doc du lieu da train:
881 hv_ComponentModel2.Dispose();
882 HOperatorSet.ReadComponentModel("C:/Users/Admin/Desktop/ĐỒ ÁN TỐT
NGHIỆP/VISION TOOL HINH TRON/HINHTRON/HINHTRON/
HINHTRON_BLACK.cbm", out hv_ComponentModel2);
883
884
885
886 }
887 }
888 }

You might also like