Repository navigation
Expand file tree
/
Copy pathForm1.cs
More file actions
413 lines (341 loc) · 12.5 KB
/
Copy pathForm1.cs
File metadata and controls
413 lines (341 loc) · 12.5 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using GCodeStreamer.NC;
namespace grblui
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UpdateIncrement(btnMul_5, EventArgs.Empty);
updateFeed(btnF200, EventArgs.Empty);
updateMoveType(btnG0, EventArgs.Empty);
}
private void sevenSegmentArray2_Load(object sender, EventArgs e)
{
}
private void statusTimer_Tick(object sender, EventArgs e)
{
UpdateStatus();
}
private int totalBlocks = 0;
private int usedblocks = 0;
object syncRoot = new object();
private void UpdateStatus()
{
if(port.IsOpen)
{
string response;
lock (syncRoot)
{
port.WriteLine("?");
response = port.ReadLine();
port.ReadLine(); // ok
}
var strings = response.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, string> responseDic = strings.Select(s => s.Split("=".ToCharArray(), 2)).ToDictionary(split => split[0], split => split[1]);
//byte []dataX = new byte[4];
//byte []dataY = new byte[4];
//byte []dataZ = new byte[4];
//port.WriteLine("??");
//port.Read(dataX, 0, 4);
//port.Read(dataY, 0, 4);
//port.Read(dataZ, 0, 4);
//var readLine = port.ReadLine();
//double dX = BitConverter.ToSingle(dataX, 0);
//double dY = BitConverter.ToSingle(dataY, 0);
//double dZ = BitConverter.ToSingle(dataZ, 0);
if (responseDic.ContainsKey("x"))
dspX.Value = responseDic["x"];
if (responseDic.ContainsKey("y"))
dspY.Value = responseDic["y"];
if (responseDic.ContainsKey("z"))
dspZ.Value = responseDic["z"];
if (responseDic.ContainsKey("UB"))
usedblocks = Convert.ToInt32(responseDic["UB"]);
if (responseDic.ContainsKey("TB"))
totalBlocks = Convert.ToInt32(responseDic["TB"]);
}
}
private void label1_Click(object sender, EventArgs e)
{
if(port.IsOpen)
{
statusTimer.Enabled = false;
port.Close();
btnConnect.BackColor = Color.Lime;
}
else
{
btnConnect.BackColor = Color.Red;
Application.DoEvents();
port.PortName = "COM10";
port.NewLine = "\n";
port.Open();
btnConnect.BackColor = Color.Lime;
while(port.BytesToRead > 0)
{
port.ReadLine();
Application.DoEvents();
}
statusTimer.Enabled = true;
if (ConnectedChanged != null)
ConnectedChanged(this, EventArgs.Empty);
}
}
private void btnY_Click(object sender, EventArgs e)
{
var increment = Convert.ToInt32((sender as Control).Tag);
SendCommand(string.Format("G21"));
SendCommand(string.Format("G91"));
SendCommand(string.Format("{0} Y{1} F{2}", moveType, increment * multiplier, feedRate));
SendCommand(string.Format("G90"));
}
private void btnX_Click(object sender, EventArgs e)
{
var increment = Convert.ToInt32((sender as Control).Tag);
SendCommand(("G21"));
SendCommand(("G91"));
SendCommand(string.Format("{0} X{1} F{2}", moveType, increment * multiplier, feedRate));
SendCommand(("G90"));
}
bool SendCommand(string cmd, params object[] args)
{
return SendCommand(string.Format(cmd, args));
}
bool SendCommand(string cmd)
{
port.WriteLine(cmd);
return port.ReadLine().ToLower().Trim() == "ok";
}
private double multiplier;
Control[] MultiplierCtrls
{
get { return new Control[] { btnMul001, btnMul005, btnMul01, label4btnMul05, btnMul5, btnMul1, btnMul_5, btnMul_1, btnMul10, btnMul50 }; }
}
Control[] FCtrls
{
get { return new Control[] {btnF1, btnF5, btnF10, btnF50, btnF100, btnF150, btnF200, btnF250, btnF300, btnF400, btnF500}; }
}
private void UpdateIncrement(object sender, EventArgs e)
{
multiplier = Convert.ToDouble((sender as Control).Text);
foreach (var ctrl in MultiplierCtrls)
{
if (ctrl == sender)
ctrl.BackColor = Color.Red;
else
ctrl.BackColor = Color.Lime;
}
}
private void btnZ_Click(object sender, EventArgs e)
{
var increment = Convert.ToInt32((sender as Control).Tag);
SendCommand(("G21"));
SendCommand(("G91"));
SendCommand(string.Format("{0} Z{1} F{2}", moveType, increment * multiplier, feedRate));
SendCommand(("G90"));
}
private void btnRun_Click(object sender, EventArgs e)
{
var fileStream = File.OpenRead(@"C:\Users\Bogdan\eagle\l298_PM\channel_PM.top.drill.tap");
StreamReader sr = new StreamReader(fileStream);
GCodeFile file = new GCodeFile();
StringBuilder errs = new StringBuilder();
file.ReadTextStream(sr, errs);
Application.DoEvents();
Stopwatch sw = Stopwatch.StartNew();
foreach (var op in file.GCodes)
{
UpdateStatus();
while(totalBlocks - 1 == usedblocks)
Application.DoEvents();
string line = op.ToString();
Console.WriteLine(line);
switch (op.OpCode)
{
case GcodeOpCode.OpCodes.M06:
{
//Console.WriteLine("Change Tool Please ({0})", line);
//Console.ReadLine();
continue;
} break;
case GcodeOpCode.OpCodes.G04:
{
Thread.Sleep(TimeSpan.FromSeconds(op.Parameter[0].Value));
continue;
} break;
}
lock(syncRoot)
{
port.WriteLine(line);
port.ReadLine(); // ok
}
UpdateStatus();
Application.DoEvents();
}
sw.Stop();
port.WriteLine("X0Y0Z10");
Application.DoEvents();
}
private void btnZeroY_Click(object sender, EventArgs e)
{
SendCommand("G92Y0");
}
private void btnZeroZ_Click(object sender, EventArgs e)
{
SendCommand("G92Z0");
}
private void btnGoTo_Click(object sender, EventArgs e)
{
GoToForm f = new GoToForm();
f.X = Convert.ToDecimal(dspX.Value);
f.Y = Convert.ToDecimal(dspY.Value);
f.Z = Convert.ToDecimal(dspZ.Value);
if(f.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
dspX.Value = f.X.ToString();
dspY.Value = f.Y.ToString();
dspZ.Value = f.Z.ToString();
}
SendCommand("{0}X{1}Y{2}Z{3}", moveType, f.X, f.Y, f.Z);
}
private void btnZeroX_Click(object sender, EventArgs e)
{
SendCommand("G92X0");
}
private void btnGoToZeroZ_Click(object sender, EventArgs e)
{
SendCommand("{0}Z0", moveType);
}
private void btnGoToZeroY_Click(object sender, EventArgs e)
{
SendCommand("{0}Y0", moveType);
}
private void btnGoToZeroX_Click(object sender, EventArgs e)
{
SendCommand("{0}Y0", moveType);
}
private void btnZeroXY_Click(object sender, EventArgs e)
{
SendCommand("{0}X0Y0", moveType);
}
private void btnZeroAll_Click(object sender, EventArgs e)
{
SendCommand("{0}X0Y0Z0", moveType);
}
public bool Connected
{
get { return port.IsOpen; }
}
public event EventHandler ConnectedChanged;
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
if(control == btnConnect)
continue;
control.DataBindings.Add("Enabled", this, "Connected");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
btnX_Click(btnXM, EventArgs.Empty);
break;
case Keys.Right:
btnX_Click(btnXP, EventArgs.Empty);
break;
case Keys.Up:
btnY_Click(btnYM, EventArgs.Empty);
break;
case Keys.Down:
btnY_Click(btnYP, EventArgs.Empty);
break;
case Keys.F1:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F2:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F3:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F4:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F5:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F6:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F7:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
case Keys.F8:
UpdateIncrement(MultiplierCtrls[e.KeyCode - Keys.F1], EventArgs.Empty);
break;
}
}
private double feedRate = 200;
private void updateFeed(object sender, EventArgs e)
{
feedRate = Convert.ToDouble((sender as Control).Text.Substring(1));
foreach (var ctrl in FCtrls)
{
if (ctrl == sender)
ctrl.BackColor = Color.Red;
else
ctrl.BackColor = Color.Lime;
}
}
Control[] MoVeTypeControls{
get { return new[] {btnG0, btnG1}; }
}
string moveType;
private void updateMoveType(object sender, EventArgs e)
{
moveType = (sender as Control).Text;
foreach (var ctrl in MoVeTypeControls)
{
if (ctrl == sender)
ctrl.BackColor = Color.Red;
else
ctrl.BackColor = Color.Lime;
}
}
private void btnReset_Click(object sender, EventArgs e)
{
btnReset.BackColor = Color.Red;
Application.DoEvents();
port.DtrEnable = true;
port.RtsEnable = false;
Thread.Sleep(3000);
port.DtrEnable = false;
port.RtsEnable = true;
port.ReadLine();
port.ReadLine();
while (port.BytesToRead > 0)
{
port.ReadLine();
}
btnReset.BackColor = Color.Lime;
}
private void btnLoad_Click(object sender, EventArgs e)
{
}
}
}