-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathForm1.cs
906 lines (860 loc) · 31.8 KB
/
Form1.cs
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* 3dpBurner Sender. A GCODE sender for GRBL based devices.
This file is part of 3dpBurner Sender application.
Copyright (C) 2014-2015 Adrian V. J. (villamany) contact: [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Main form
using System;
using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
//using System.Threading;
using System.Management;//for serial port device names
using System.Runtime.InteropServices;
// for DllImportAttribute //For prevent entering standby/switching the display device off.
//Latest changes
//-A more precise remaining time
//-Code cleanup
//-Other minor bugs and improvements
namespace _3dpBurner
{
public partial class frm3dpBurner : Form
{
const string ver = "1.2";//app version
string rxString;
List<string> fileLines;
Int32 fileLinesCount;//for file streaming control
Int32 fileLinesSent;//for file streaming control
Int32 fileLinesConfirmed;//for file streaming control
const int grblBuferSize = 127;//rx bufer size of grbl on arduino
int bufFree = grblBuferSize;//actual suposed free bytes on grbl buffer
TimeSpan elapsed;//elapsed time from file burnin
TimeSpan remaining;//remaining time (estimated)
DateTime timeInit;//time start to burning file
bool transfer = false;//true whe transfer in progress
bool dataProcessing;//false when no data processing pending
bool jogging=false;//true when we are jogging
int GRBL_errCount = 0;// aux for grbl errors count for auto close port when more than some continuous errors ocurrs (prevents pplication hangs)
const int GRBL_errMax = 2;//Number of grbl response errors for auto close ports
bool connected;//for log the connected message
//Thread exception
private void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
ClosePort();
Exception ex = e.Exception;
MessageBox.Show(ex.Message, "Thread exception");
}
//Unhandled exception
private void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject != null)
{
ClosePort();
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show(ex.Message, "Application exception");
}
}
//For prevent entering standby/switching the display device off.
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
//Constructor
public frm3dpBurner()
{
InitializeComponent();
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
}
//log serial port dataReceive thread error message
public void logErrorThr(object sender, EventArgs e)
{
logError(mens,err);
}
//log a error message
private void logError(string message, Exception err)
{
string textmsg = "\r\n[ERROR]: " + message + ". ";
if (err != null) textmsg += err.Message;
textmsg += "\r\n";
rtbLog.AppendText(textmsg);
rtbLog.ScrollToCaret();
}
private void setTransferTrue()
{
transfer = true;
//prevent system idle
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED);
}
private void setTransferFalse()
{
transfer = false;
//restore system idle state
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
//Process the received data line
private void dataRx(object sender, EventArgs e)
{
//staus message?
if ((rxString.Length > 0) && (rxString[0] == '<'))
{
toolStripStatusLabel1.Text =rxString;
if (!connected)
{
rtbLog.AppendText("\r\n[CONNECTED]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
connected=true;
}
if (rxString.Contains("Run")) statusStrip1.BackColor = Color.DodgerBlue;
else
if (rxString.Contains("Idle")) statusStrip1.BackColor = Color.YellowGreen;
else
if (rxString.Contains("Alarm")) statusStrip1.BackColor = Color.DarkOrange;
else
if (rxString.Contains("Home")) statusStrip1.BackColor = Color.Violet;
else
if (rxString.Contains("Queue")) statusStrip1.BackColor = Color.FromArgb(255,255,10);
else
toolStripStatusLabel1.BackColor = SystemColors.Control;
dataProcessing = false;
return;
}
//If no transfer in progress
if (!transfer)
{
rtbLog.AppendText(rxString);//print received line response
rtbLog.AppendText("\r");
rtbLog.ScrollToCaret();
dataProcessing = false;
}
//jogging response?
if (jogging)
{
jogging = false;
sendLine("G90");//add a G90 absolute coords command
dataProcessing = false;
return;
}
//if no transfer, no any more to do
if (!transfer) return;
//line transfer response
if (rxString != "ok")//Add line-response only if not ok (if many lines the stream procces is slowly)
{
rtbLog.AppendText(fileLines[fileLinesConfirmed] + " >" + rxString+"\r");
rtbLog.ScrollToCaret();
}
bufFree += (fileLines[fileLinesConfirmed].Length + 1);//update bytes supose to be free on grbl rx bufer
fileLinesConfirmed++;//line processed
if (fileLinesConfirmed >= fileLinesCount)//Transfer finished and processed? Update status and controls
{
setTransferFalse();
pbFile.Value = 100;
lblFileProgress.Text = "100%";
lblRemaining.Text = "00:00:00";
// updateProgress();
rtbLog.AppendText("[Yeah!. Burning Done! @" + lblElapsed.Text + "]\r\n");
rtbLog.ScrollToCaret();
}
else//not finished
{
if (fileLinesSent < fileLinesCount) sendNextLine();//If more lines on file, send it
}
//updateProgress();
dataProcessing = false;
}
//Send next line from fileStreaming
private void sendNextLine()
{
try
{
while ((fileLinesSent < fileLinesCount) && (bufFree >= fileLines[fileLinesSent].Length + 1))
{
serialPort1.Write(fileLines[fileLinesSent] + "\r");
bufFree -= (fileLines[fileLinesSent].Length + 1);
fileLinesSent++;
}
}
catch (Exception er)
{
logError("Error sending next line", er);
ClosePort();
}
}
//Rx from serial port
string mens;
Exception err;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
while ((serialPort1.IsOpen)&& (serialPort1.BytesToRead > 0))
{
rxString = string.Empty;
try
{
rxString = serialPort1.ReadTo("\r\n");//read line from grbl, discard CR LF
dataProcessing = true;
this.Invoke(new EventHandler(dataRx));//tigger rx process
while ((serialPort1.IsOpen) && (dataProcessing)) ;//wait previous data line processed done
}
catch (Exception errort)
{
mens = "Error reading line from serial port";
ClosePort();
err = errort;
this.Invoke(new EventHandler(logErrorThr));
}
}
}
//Send a line adding a CR and log it
private void sendLine(string data)
{
try
{
serialPort1.Write(data + "\r");
rtbLog.AppendText(data + " >");//if not in transfer log the txLine
}
catch(Exception err)
{
logError("Sending line", err);
}
}
//Open port
private bool OpenPort()
{
try
{
connected = false;
toolStripStatusLabel1.Text = "";
toolStripStatusLabel1.BackColor=SystemColors.Control;
rtbLog.AppendText("\r\n[CONNECTING]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
serialPort1.PortName = cbPort.Text;
serialPort1.Open();
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
dataProcessing = true;
GRBL_errCount = 0;
return (true);
}
catch (Exception err)
{
logError("Opening port", err);
ClosePort();
return (false);
}
}
private bool ClosePort()
{
if (transfer) { button5_Click(this, null); return (false);}
else
{
try
{
setTransferFalse();
if (serialPort1.IsOpen)
{
serialPort1.DiscardInBuffer();
serialPort1.DiscardOutBuffer();
serialPort1.Close();
rtbLog.AppendText("\r\n[DISCONNECTED]\r\n");
rtbLog.ScrollToCaret();
toolStripStatusLabel1.BackColor = SystemColors.Control;
}
toolStripStatusLabel1.Text = "";
toolStripStatusLabel1.BackColor = SystemColors.Control;
return (true);
}
catch (Exception err)
{
logError("Closing port", err);
return (false);
}
}
}
//Send reset sentence
private void grblReset()//Stop/reset button
{
try
{
setTransferFalse();
rtbLog.AppendText("[RESET]");
var dataArray = new byte[] { 24 };//Ctrl-X
serialPort1.Write(dataArray, 0, 1);
}
catch (Exception err)
{
logError("Reset command fail", err);
}
}
//Open port button
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen) ClosePort(); else OpenPort();
}
//Refresh port names on the combo box
private void refreshPorts()
{
try
{
List<String> tList = new List<String>();
cbPort.Items.Clear();
foreach (string s in SerialPort.GetPortNames()) tList.Add(s);
if (tList.Count < 1) logError("No serial ports found", null);
else
{
tList.Sort();
cbPort.Items.AddRange(tList.ToArray());
}
}
catch (Exception e)
{
logError("Refreshing available ports", e);
}
}
//Form on load
private void Form1_Load(object sender, EventArgs e)
{
Text = "3dpBurner Sender v" + ver;
btnGotoXoYo.Text = "GoTo\r\nX0,Y0";
bHome.Text = "Homing\r\nXY";
btnZero.Text = "Zero\r\nXY";
btnUnlock.Text = "Alarm\r\nUnlock";
loadSettings();
}
//Load settings
private void loadSettings()
{
try
{
cbPort.Text = Properties.Settings1.Default.port;
tbFile.Text = Properties.Settings1.Default.file;
tbStepSize.Text = Properties.Settings1.Default.step;
tbLaserPwr.Text = Properties.Settings1.Default.pwr;
tbCustom1.Text = Properties.Settings1.Default.custom1;
tbCustom2.Text = Properties.Settings1.Default.custom2;
}
catch (Exception e)
{
logError("Loading settings",e);
}
}
//Save settings
private void saveSettings()
{
try
{
Properties.Settings1.Default.port = cbPort.Text;
Properties.Settings1.Default.file = tbFile.Text;
Properties.Settings1.Default.step = tbStepSize.Text;
Properties.Settings1.Default.pwr = tbLaserPwr.Text;
Properties.Settings1.Default.custom1 = tbCustom1.Text;
Properties.Settings1.Default.custom2 = tbCustom2.Text;
Properties.Settings1.Default.Save();
}
catch (Exception e)
{
logError("Saving settings", e);
}
}
//Send command button
private void bSendCmd_Click(object sender, EventArgs e)
{
sendLine(tbCommand.Text);
tbCommand.Clear();
}
//TextBox manual command
private void tbCommand_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)13) return;
sendLine(tbCommand.Text);
tbCommand.Clear();
}
//Jog X+ button
private void bXup_Click(object sender, EventArgs e)
{
jogging = true;
sendLine("G91G0X+" + tbStepSize.Text);
}
//Jog X- button
private void bXdown_Click(object sender, EventArgs e)
{
jogging = true;
sendLine("G91G0X-" + tbStepSize.Text);
}
//Jog Y+ button
private void bYup_Click(object sender, EventArgs e)
{
jogging = true;
sendLine("G91G0Y+" + tbStepSize.Text);
}
//Jog Y- button
private void bYdown_Click(object sender, EventArgs e)
{
jogging = true;
sendLine("G91G0Y-" + tbStepSize.Text);
}
//GoTo X0, Y0 button
private void btnGotoXoYo_Click(object sender, EventArgs e)
{
sendLine("G0 X0 Y0");
}
//Homming button
private void bHome_Click(object sender, EventArgs e)
{
sendLine("$H");
}
//.01 Step button
private void button12_Click(object sender, EventArgs e)
{
tbStepSize.Text="0.01";
}
//.1 Step button
private void button19_Click(object sender, EventArgs e)
{
tbStepSize.Text = "0.1";
}
//1 Step button
private void button20_Click(object sender, EventArgs e)
{
tbStepSize.Text = "1";
}
//5 step button
private void button1_Click_1(object sender, EventArgs e)
{
tbStepSize.Text = "5";
}
//10 Step button
private void button21_Click(object sender, EventArgs e)
{
tbStepSize.Text = "10";
}
//100 Step button
private void button22_Click(object sender, EventArgs e)
{
tbStepSize.Text = "100";
}
//Fornm on close
private void frm3dpBurner_FormClosing(object sender, FormClosingEventArgs e)
{
saveSettings();
if (transfer)
{
button5_Click(this, null);
e.Cancel=true;
}
else
{
ClosePort();
}
}
//Open file
private void bOpenfile_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tbFile.Text = openFileDialog1.FileName;
}
}
//Prepare file
private void prepareFile()
{
rtbLog.AppendText("[Optimizing file...]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
fileLinesSent = 0;
fileLinesCount = 0;
fileLinesConfirmed = 0;
bufFree = grblBuferSize;
if (File.Exists(tbFile.Text))
{
fileLines = new List<string>();
StreamReader file = new StreamReader(tbFile.Text);
string line = "";
line = file.ReadLine();
while (line!= null)
{
line=line.Replace(" ", "");//remove spaces
line = line.Replace("\r", " ");//remove CR
line = line.Replace("\n", " ");//remove LF
line = line.ToUpper();//all uppercase
line = line.Trim();
if ((!string.IsNullOrEmpty(line))&&(line[0]!='('))//trim lines and remove all empty lines and comment lines
{
fileLines.Add(line);//add line to list to send
fileLinesCount++;//Count total lines
}
line = file.ReadLine();
}
file.Close();
elapsed = TimeSpan.Zero;
lblElapsed.Text = elapsed.ToString(@"hh\:mm\:ss");
}
}
//Resume button
private void bStart_Click(object sender, EventArgs e)
{
{
rtbLog.AppendText("[RESUME]\r\n");
rtbLog.ScrollToCaret();
try
{
serialPort1.Write("~");
}
catch (Exception err)
{
logError("Sending command", err);
}
}
}
//Reset button
private void button5_Click(object sender, EventArgs e)
{
grblReset();
}
//Unlock alarm button
private void button11_Click(object sender, EventArgs e)
{
sendLine("$X");
}
//Update time elapsed
private void tmrUpdates_Tick(object sender, EventArgs e)
{
//Unlockl button flashing
if (statusStrip1.BackColor == Color.DarkOrange)//is in alarm mode?
{
if (btnUnlock.BackColor == Color.Silver) btnUnlock.BackColor = Color.DarkOrange; else btnUnlock.BackColor = Color.Silver;
}
else btnUnlock.BackColor = Color.Silver;
if (transfer)//if active transfer update elapsed/remaining time time
{
float progressPorc;
//file progress status
if ((!File.Exists(tbFile.Text)) || (fileLinesCount < 1))//|| (fileLinesSent < 1))
progressPorc = 0;
else
{
progressPorc = (float)(fileLinesConfirmed * 100.0 / fileLinesCount);
//Text = Convert.ToString(fileLinesConfirmed) + "/" + Convert.ToString(fileLinesCount) + " lines "+ Convert.ToString (fileLinesConfirmed * 100.0 / fileLinesCount)+"%";//debug
}
pbFile.Value = Convert.ToInt32(progressPorc);
lblFileProgress.Text = Convert.ToString((Int32)progressPorc) + "%";// "% (" + Convert.ToString(fileLinesConfirmed) + "/" + Convert.ToString(fileLinesCount) + " lines)";
elapsed = DateTime.UtcNow - timeInit;
lblElapsed.Text = elapsed.ToString(@"hh\:mm\:ss");
if (fileLinesConfirmed>0)
remaining = TimeSpan.FromSeconds((100 - progressPorc) * (elapsed.TotalSeconds / progressPorc));
lblRemaining.Text = remaining.ToString(@"hh\:mm\:ss");
}
//retrieve GRBL status
if (serialPort1.IsOpen)
{
try
{
var dataArray = new byte[] { Convert.ToByte('?') };
serialPort1.Write(dataArray, 0, 1);
GRBL_errCount=0;
}
catch (Exception er)
{
GRBL_errCount ++;
logError("Retrieving 3dpBurner status ( TRY " + GRBL_errCount.ToString() + " / " + GRBL_errMax.ToString() + " )", er);
if (GRBL_errCount >= GRBL_errMax)
{
logError("3dpBurner seem to not respond", null);
ClosePort();
}
}
}
}
//Laser On button
private void btnLaserOn_Click(object sender, EventArgs e)
{
sendLine("M3");
}
//Laser Off button
private void btsLaserOff_Click(object sender, EventArgs e)
{
sendLine("M5");
}
//Custom 1 button
private void btnCustom1_Click(object sender, EventArgs e)
{
sendLine(tbCustom1.Text);
}
//Custom 1 textBox
private void tbCustom1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)13) return;
sendLine(tbCustom1.Text);
}
//Custom2 button
private void btnCustom2_Click(object sender, EventArgs e)
{
sendLine(tbCustom2.Text);
}
//Custom 2 textBox
private void tbCustom2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)13) return;
sendLine(tbCustom2.Text);
}
//Homing button
private void btnZero_Click(object sender, EventArgs e)
{
sendLine("G92X0Y0");
}
//Clear log button
private void btlClearLog_Click(object sender, EventArgs e)
{
rtbLog.Clear();
}
//Laser PWR button
private void btnLaserPwr_Click(object sender, EventArgs e)
{
sendLine("S" + tbLaserPwr.Text);//Variable spindle PWM managed by 'Z'
}
//Zero XY button
private void btnZeroXY_Click(object sender, EventArgs e)
{
sendLine("G92X0Y0");
}
//Zero Z button
private void btnZeroZ_Click(object sender, EventArgs e)
{
sendLine("G92Z0");
}
//Restore settings
private void restoreSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Restore Settings?", "Restore Settings", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.OK) return;
Properties.Settings1.Default.Reset();
Properties.Settings1.Default.Save();
loadSettings();
}
//About dialog
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAbout frmAb = new frmAbout();
frmAb.ShowDialog();
}
//Enter on power texbox-Send Sxx command
private void tbLaserPwr_KeyPress(object sender, KeyPressEventArgs e)
{
{
if (e.KeyChar != (char)13) return;
sendLine("S"+tbLaserPwr.Text);
}
}
//Serial port selection
private void cbPort_DropDown(object sender, EventArgs e)
{
refreshPorts();
}
//Pause button
private void bPause_Click(object sender, EventArgs e)
{
rtbLog.AppendText("[PAUSE]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
try
{
serialPort1.Write("!");//ensure pause is received
}
catch (Exception err)
{
logError("Sending command", err);
}
}
//Reset button
private void btnReset_Click(object sender, EventArgs e)
{
grblReset();
}
//Send file button
private void btnFileStart_Click(object sender, EventArgs e)
{
if (!transfer)
{
if (!File.Exists(tbFile.Text))
{
logError("Error opening file", null);
return;
}
btnFileStart.Text = "Abort File";
btnFileStart.BackColor = Color.Salmon;
bOpenfile.Enabled = false;
Refresh();
prepareFile();
rtbLog.AppendText("[Sending file...]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
setTransferTrue();
fileLinesConfirmed = 0;
timeInit = DateTime.UtcNow;
sendNextLine();
}
else
{
setTransferFalse();
pbFile.Value = 0;
rtbLog.AppendText("[Stop]\r\n");
rtbLog.ScrollToCaret();
}
}
//Visual controls updates
private void tmrControlsUpdate_Tick(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
bOpenPort.Text = "Disconnect";
bOpenPort.BackColor = Color.Salmon;
}
else
{
bOpenPort.Text = "Connect";
bOpenPort.BackColor = Color.YellowGreen;
}
if (transfer)
{
btnFileStart.Text="Abort File";
btnFileStart.BackColor = Color.Salmon;
bOpenfile.Enabled = false;
}
else
{
btnFileStart.Text = "Send File";
btnFileStart.BackColor = Color.YellowGreen;
bOpenfile.Enabled = true;
}
if (toolStripStatusLabel1.Text== "") statusStrip1.BackColor = SystemColors.Control;
}
//Serial ports info button
private void btnPortsInfo_Click(object sender, EventArgs e)
{
try
{
//Display port and device names on the console
rtbLog.AppendText("\r\n[PORTS INFO]\r\n");
rtbLog.ScrollToCaret();
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPEntity");
foreach (ManagementObject queryObj in searcher.Get())
{
if ((queryObj["Caption"] != null) && (queryObj["Name"].ToString().Contains("(COM")))
{
rtbLog.AppendText((queryObj["Caption"]).ToString()+"\r\n");
rtbLog.ScrollToCaret();
}
}
rtbLog.AppendText("\r\n");
}
catch (Exception er)
{
logError("Geting available ports info", er);
}
}
//Send configuration file to machine
private void sendConfigurationFileTo3dpBurnerToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (!connected)//if no connectio
{
logError("Connect first!" + "\r\n", null);
rtbLog.ScrollToCaret();
return;
}
if (transfer)//if transfering file the exit
{
logError("Stop the file transfer first!" + "\r\n",null);
rtbLog.ScrollToCaret();
return;
}
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
if (!File.Exists(openFileDialog1.FileName))
{
logError("Error opening file", null);
return;
}
rtbLog.AppendText("[Sending configuration file...]\r\n");
rtbLog.ScrollToCaret();
rtbLog.Refresh();
StreamReader file = new StreamReader(openFileDialog1.FileName);
try
{
string line = file.ReadLine();
while (line!=null )//line=null mean end of file
{
rtbLog.AppendText(line + " >>> ");
dataProcessing = true;
serialPort1.Write(line + "\r");
while (dataProcessing) Application.DoEvents() ;//wait response processed
line = file.ReadLine();
}
}
catch (Exception er)
{
logError("Error sending file", er);
}
file.Close();
}
catch (Exception err)
{
logError("Sending configuration file", err);
}
}
//View 3dpBurner configuration
private void view3dpBurnerSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (!connected)//if no connectio
{
logError("Connect first!" + "\r\n", null);
rtbLog.ScrollToCaret();
return;
}
if (transfer)//if transfering file the exit
{
logError("Stop the file transfer first!" + "\r\n", null);
rtbLog.ScrollToCaret();
return;
}
rtbLog.AppendText("[Getting configuration...]\r\n");
serialPort1.Write("$$\r$N\r");
}
catch(Exception err)
{
logError("Retrieving configuration", err);
}
}
}
}