其他分享
首页 > 其他分享> > Datalogic 雕刻机

Datalogic 雕刻机

作者:互联网

选型需要考虑:

1. 材料

2. 材质

3. 内容

4. 范围 选择镜头

5. 厚度

6. 效率

7. 效果

 

脚本代码:

  1 // COM 串口设置
  2 const N_COM = "COM2";
  3 
  4 const N_PATH_APP = "D:\\232_load_file\\";
  5 const N_PATH_XLP = N_PATH_APP+"xlp\\";
  6 
  7 const N_PATH_DEBUG = N_PATH_APP+"laser_debug\\laser_debug"
  8 var N_DEBUG = false;
  9 
 10 var bLoaded = false;
 11 var bStopLaser = false;
 12 
 13 function onQueryStart()
 14 {
 15     // 收到开始雕刻信号
 16     if (!bLoaded)
 17     {
 18         debug_print("onQueryStart - Not Load");
 19         return;
 20     }
 21     if (bMarking)
 22     {
 23         debug_print("onQueryStart - Marking");
 24         return;
 25     }
 26     if (N_DEBUG)
 27     {
 28         if (bStopLaser)
 29         {
 30             debug_print("onQueryStart - Test Stop");
 31             bStopLaser = false;
 32             return;
 33         }
 34         bStopLaser = false;
 35         n_StartTime = new Date();
 36         n_StartTime = new Date();
 37     }
 38     uxStatus.text = "开始激光雕刻... ";
 39     bMarking = true;
 40     h_Doc.execute(true, true);
 41 }
 42 function onLaserStop()
 43 {
 44     // 停止激光后
 45     debug_print("onLaserStop ... ");
 46     bMarking = false;
 47     uxStatus.text = "待命";
 48 }
 49 function onLaserStart()
 50 {
 51     // TODO
 52 }
 53 function onLaserEnd()
 54 {
 55     // 雕刻完成后
 56     if (N_DEBUG)
 57     {
 58         var d = new Date();
 59         d = new Date();
 60         var timeDiff = (d - n_StartTime) / 1000;
 61         if (timeDiff > 5)
 62         {
 63             var strOut = "onLaserEnd ... count=" + n_MarkCount + " ,delay = " + timeDiff.toString();
 64             debug_print(strOut);
 65         }
 66         n_MarkCount++;
 67         debug_print("onLaserEnd ... count=" + n_MarkCount + " ,time=" + timeDiff.toString());
 68     }
 69     var cmdOut;
 70     cmdOut = "<XE>";
 71     debug_print("232 Send="+cmdOut);
 72     h_port.writeBlock(cmdOut, cmdOut.length);
 73     uxStatus.text = "待命";
 74     System.collectGarbage();
 75     bMarking = false;
 76 }
 77 function onLaserError(error)
 78 {
 79     switch(error)
 80     {
 81     case System.DSP_IN_HANG:
 82         System.resetBoard();
 83         break;
 84     case System.DSP_ERROR_INIT:
 85         // This event is triggered each time the script engine starts
 86         // if the board was not properly loaded
 87         // MessageBox.critical( "Board initialization error", MessageBox.Ok );
 88         break;
 89         // TODO
 90     }
 91 }
 92 function main()
 93 {
 94     System.sigQueryStart.connect(onQueryStart);
 95     System.sigLaserStop.connect(onLaserStop);
 96     System.sigLaserStart.connect(onLaserStart);
 97     System.sigLaserEnd.connect(onLaserEnd);
 98     System["sigLaserError(int)"].connect(onLaserError);
 99     
100     // 系统初始化
101     System.sigClose.connect(onClose);
102 
103     bMarking = false;
104     n_StartTime = new Date();
105     n_StartTime = new Date();
106     n_MarkCount = 0;
107     
108     initCOM();
109     initIO();
110     initTimer();
111 
112     //Create preview dialogue
113     dialog = new Dialog("Laser Custom Application - 232 Load Text - 3.00",Dialog.D_NONE,false);
114     dialog.setFixedSize(600,600);
115     renderarea = new RenderArea();
116     dialog.add(renderarea);
117 
118     uxGroupFile = new GroupBox();
119     uxGroupFile.title = "图面文件";
120     dialog.add( uxGroupFile );
121     
122     uxFileName = new Label();
123     uxFileName.alignment = 0x02;    // align right    
124     uxFileName.text = ""
125     uxGroupFile.add( uxFileName );
126 
127     uxDebug = new CheckBox();
128     uxDebug.text = "记录日志";
129     uxDebug.checked = false;
130     uxDebug.sigStateChanged.connect(onStateChanged);
131     dialog.add( uxDebug );
132     
133     uxAim = new PushButton();
134     uxAim.text = "红光校准";
135     uxAim.sigPressed.connect(startAiming);
136     dialog.add(uxAim);
137 
138     uxStop = new PushButton();
139     uxStop.text = "停止激光";
140     uxStop.sigPressed.connect(stopLaser);
141     dialog.add(uxStop);
142     
143     uxMark = new PushButton();
144     uxMark.text = "开始雕刻";
145     uxMark.sigPressed.connect(startMark);
146     dialog.add(uxMark);
147 
148     uxStatus = new Label();
149     uxStatus.alignment = 0x02;    // align right
150     uxStatus.text = "待命";
151     dialog.add( uxStatus );
152 
153     initCOMUI();
154     initIOUI();
155     
156     dialog.show();
157 
158     debug_print( "Ready !" );
159     IoPort.setReady(true);
160 }
161 function onClose()
162 {
163       IoPort.setReady(false);
164 }
165 
166 // 加载图面文件
167 function onl oadPlain()
168 {
169     var file_name;
170     file_name = FileDialog.getOpenFileName("Laser Documents (*.xlp)", "Select XLP A");
171     if (file_name)
172     {
173         uxFileName.text = file_name;
174         loadPlain(uxFileName.text);
175     }
176 
177     file_name = FileDialog.getOpenFileName("Laser Documents (*.xlp)", "Select XLP B");
178     if (file_name)
179     {
180         uxFileName2.text = file_name;        
181     }
182 }
183 function loadPlain(file_name)
184 {
185     // 1.21 加载xlp文件
186     h_Doc = new LaserDoc;
187     if (h_Doc.load(file_name))
188     {
189         renderarea.preview(h_Doc);
190         bLoaded = true;
191         n_MarkCount = 0;
192         return true;
193     }
194     else
195     {
196         // MessageBox.information( "加载错误!-"+file_name, MessageBox.Ok );        
197         debug_print("Error load XLP- " + file_name + " !");
198         bLoaded = false;
199         return false;
200     }
201 }
202 
203 // 雕刻控制
204 function startMark()
205 {
206     // 2.1 触发激光雕刻
207     onQueryStart();
208 }
209 function stopLaser()
210 {
211     System.stopLaser();
212 }
213 function startAiming()
214 {
215     if (System.isLaserBusy())    return;
216     h_Doc.execute(true, false);
217     uxStatus.text = "红光校准";
218 }
219 
220 // 232 COM 通讯处理
221 function initCOM()
222 {
223     N_CHAR_SYNC = "";//"UUUU";
224     N_CHAR_START = "<";
225     N_CHAR_END = ">";
226     n_RxBuffer = "";
227 
228     // COM init
229     h_port = new ComPort;
230     h_port["readyRead()"].connect(onReadyRead);
231     h_port.portName = N_COM;
232     h_port.baudRate = ComPort.BAUD9600;
233     h_port.flowControl = ComPort.FLOW_OFF;
234     h_port.parity = ComPort.PAR_NONE;
235     h_port.dataBits = ComPort.DATA_8;
236     h_port.stopBits = ComPort.STOP_1;
237     var result = h_port.open(ComPort.ReadWrite);
238     debug_print("Open - "+N_COM+"="+result);
239     if (!result)    debug_print( "open_error-"+N_COM, MessageBox.Ok );
240 }
241 function initCOMUI()
242 {
243     uxStatusCOM = new Label();
244     uxStatusCOM.alignment = 0x02;    // align right
245     uxStatusCOM.text = "COM = "+N_COM+",9600,8,N,1,N";
246     dialog.add( uxStatusCOM );
247 }
248 function onReadyRead()
249 {
250     // 接收数据
251     debug_print( "onReadyRead = " );
252     var msg = h_port.readBlock();
253     var str = "";
254     for (var i=0; i<msg.length; i++)
255     {
256         str += String.fromCharCode(msg[i]);
257     }
258     for (var i=0; i<n_RxBuffer.length; i++)
259     {
260         debug_print("n_RxBuffer="+i+"="+n_RxBuffer.charCodeAt(i));
261     }
262     // h_port.writeBlock(str, str.length);
263     n_RxBuffer += str;
264     debug_print(n_RxBuffer);
265     var cmds = n_RxBuffer.split(N_CHAR_START);
266     debug_print("cmds="+cmds);
267     n_RxBuffer = "";
268     if (cmds.length <1)    return;
269     for (var i=1; i<cmds.length; i++)
270     {
271         var end = cmds[i].indexOf(N_CHAR_END);
272         if (end > 0)
273         {
274             cmds[i] = cmds[i].substring(0, end);
275             decodeCommand(cmds[i]);
276         }
277         else if (i == (cmds.length-1))
278         {
279             n_RxBuffer = N_CHAR_START + cmds[i];
280         }
281     }
282 }
283 function decodeCommand(cmd)
284 {
285     // 解读指令
286     cmd = cmd.replace(N_CHAR_END,"");
287     debug_print("decodeCommand="+cmd);
288     switch (cmd[0])
289     {
290     case 'X':
291         // 开始雕刻
292         onQueryStart();
293         var cmdOut;
294         cmdOut = N_CHAR_SYNC + N_CHAR_START + "X" + N_CHAR_END;
295         debug_print("232 Send="+cmdOut);
296         h_port.writeBlock(cmdOut, cmdOut.length);
297         break;
298     case 'P':
299         // 停止雕刻
300         bStopLaser = true;
301         System.stopLaser();
302         var cmdOut;
303         cmdOut = N_CHAR_SYNC + N_CHAR_START + "P" + N_CHAR_END;
304         debug_print("232 Send="+cmdOut);
305         h_port.writeBlock(cmdOut, cmdOut.length);
306         break;
307     case 'L':
308         // 选泽图面文件
309         cmd = cmd.substring(1, cmd.length);
310         var fullPath = N_PATH_XLP + cmd + ".xlp";
311         var infile = new File(fullPath);
312         debug_print("L="+fullPath);
313         uxStatus.text = "232 选择 XLP = " + fullPath;
314         uxFileName.text = fullPath;
315         var result = loadPlain(uxFileName.text);
316         var cmdOut;
317         if (result)
318         {
319             cmdOut = N_CHAR_SYNC + N_CHAR_START + "L" + cmd + N_CHAR_END;
320             debug_print("232 Send="+cmdOut);
321             h_port.writeBlock(cmdOut, cmdOut.length);
322         }
323         break;
324     case 'D':
325         // 修改文字
326         cmd = cmd.substring(1, cmd.length);
327         var para = cmd.split(",");
328         if (para.length >= 2)
329         {
330             debug_print("D="+cmd);
331             uxStatus.text = "232 修改文字ID"+para[0].toString()+"为"+para[1].toString();
332             var h_Obj = h_Doc.getLaserObject(para[0].toString());
333             h_Obj.text = para[1].toString();
334             h_Obj.update();
335             renderarea.preview(h_Doc);
336             var cmdOut;
337             cmdOut = N_CHAR_SYNC + N_CHAR_START + "D" + para[0].toString() + "," + para[1].toString() + N_CHAR_END;
338             debug_print("232 Send="+cmdOut);
339             h_port.writeBlock(cmdOut, cmdOut.length);
340         }
341         break;
342     case 'O':
343         // 偏移XYA
344         cmd = cmd.substring(1, cmd.length);
345         var para = cmd.split(",");
346         if (para.length >= 4)
347         {
348             h_Obj = h_Doc.getLaserObject(para[0].toString());
349             offX = parseFloat(para[1]);
350             offY = parseFloat(para[2]);
351             offA = parseFloat(para[3]);
352             if ((offX!=NaN) && (offX!=NaN) && (offX!=NaN))
353             {
354                 uxStatus.text = "232 修改ID"+para[0].toString()+",X="+offX.toString()+",Y="+offY.toString()+",A="+offA.toString();
355                 // 获取现时角度
356                 var oldAng = Math.acos(h_Obj.x11) * 180 / Math.PI;
357                 if ( h_Obj.x12 < 0 )    oldAng = 360 - oldAng;
358                 // MessageBox.critical( oldAng, MessageBox.Ok );
359                 offA = offA - oldAng;
360                 // 移动对象
361                 h_Obj.moveTo(offX, offY);
362                 // 旋转对象
363                 h_Obj.rotate(offA);
364                 // print("x11="+h_Obj.x11.toString()+",x12="+h_Obj.x12.toString()+",x21="+h_Obj.x21.toString()+",x22="+h_Obj.x22.toString());
365                 renderarea.preview(h_Doc);
366                 print("O=offX="+offX+",offY="+offY+"offA="+offA);
367                 var cmdOut;
368                 cmdOut = N_CHAR_SYNC + N_CHAR_START + "O" + para[0] + "," + para[1] + "," + para[2] + "," + para[3] + N_CHAR_END;
369                 print("232 Send="+cmdOut);
370                 h_port.writeBlock(cmdOut, cmdOut.length);
371             }
372         }
373         break;
374     case 'T':
375         test1();
376         break;
377     }
378 }
379 
380 // 测试 IO 触发
381 function initIO()
382 {    
383     if (!N_DEBUG)    return;
384     // reset IO port
385     IoPort.setReady(false);
386 }
387 function initIOUI()
388 {
389     if (!N_DEBUG)    return;
390 
391     uxGroupTest = new GroupBox();
392     uxGroupTest.title = "测试";
393     dialog.add( uxGroupTest );
394 
395     uxtest1 = new PushButton();
396     uxtest1.text = "测试选择1";
397     uxtest1.sigPressed.connect(test1);
398     uxGroupTest.add(uxtest1);
399 }
400 function test1()
401 {
402     if (!N_DEBUG)    return;
403     debug_print("test1...");
404     IoPort.setReady(true);
405     // reset test input pin
406     timerTest1 = System.setTimer(100);
407 }
408 
409 // 定时器设置
410 function initTimer()
411 {
412     if (!N_DEBUG)    return;
413     System["sigTimer(int)"].connect(onTimer);
414 }
415 function onTimer(ID)
416 {
417     System.killTimer(ID);
418     switch (ID)
419     {
420     case(timerTest1):
421         debug_print("timerTest1...");
422         IoPort.setReady(false);
423         break; 
424     }
425     System.collectGarbage();
426 }
427 
428 // 除错日志
429 function onStateChanged()
430 {
431     if (uxDebug.checked)
432     {
433         N_DEBUG = true;
434     }
435     else
436     {
437         N_DEBUG = false;
438     }
439 }
440 function debug_print(message)
441 {
442     if (!N_DEBUG)    return;
443 
444     var d = new Date();
445     d = new Date();    
446 
447     var yyyy = d.getFullYear().toString();
448     var mm =  (d.getMonth()+1).toString();
449     var dd = d.getDate().toString();
450     var hh = d.getHours().toString();
451     var mi = d.getMinutes().toString();
452     var ss = d.getSeconds().toString();
453     var ms = d.getMilliseconds().toString();
454     if (dd.length < 2)    dd = "0" + dd; 
455     if (hh.length < 2)    hh = "0" + hh; 
456     if (mi.length < 2)    mi = "0" + mi; 
457     if (ss.length < 2)    ss = "0" + ss; 
458     if (ms.length < 2)    ms = "0" + ms; 
459     if (ms.length < 3)    ms = "0" + ms;
460 
461     var day = yyyy + "-" + mm + "-" + dd;
462     var time = hh + ":" + mi + ":" + ss + "." + ms;
463     var s = day + " " + time + "\t" + message;
464 
465     var pathFile = N_PATH_DEBUG + "-" + day + ".txt";
466     appendTextToFile(pathFile, s);
467     // print(s);
468 }
469 function appendTextToFile(file_name, text)
470 {
471     var full_path_txt = file_name;
472     var outfile = new File(full_path_txt);
473     var result = false;
474     var strOut = text + "\r\n";
475     
476     outfile.open(File.WriteOnly| File.Append);
477     outfile.writeLine(strOut, strOut.length);
478     outfile.close();
479 
480     return result;
481 }
482 
483 折叠/显示

 

打印测试效果如下:

 

 

 

标签:cmdOut,Datalogic,debug,print,toString,雕刻机,var,new
来源: https://www.cnblogs.com/wang_xy/p/12050610.html