-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
415 lines (354 loc) · 13.9 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
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 Excel = Microsoft.Office.Interop.Excel;
namespace ProfilometerAutomation
{
public partial class Form1 : Form
{
int i = 0;
int xCellIndex;
SerialPort ComPort = new SerialPort();
Profilometer profilometer = new Profilometer();
MotionController controller;
bool portsButtonClicked = false;
#region
/*
//Make a vallidation method that displays
// a message box
string message = "Open the ports first you fool";
string caption = "error";
MessageBoxButtons button = MessageBoxButtons.OK;
string errorMissingInitial = "Program Missing #INITIAL Label or Program is already running";
StringBuilder builder = new StringBuilder();
*/
#endregion
string profResult = String.Empty;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bool connected = false;
GalilWidgets.GalilWidget wi; //for casting to Widget Interface
wi = (GalilWidgets.GalilWidget)gwTerm1; gwComs1.GWRegisterWidget(ref wi);
wi = (GalilWidgets.GalilWidget)gwDatRec1; gwComs1.GWRegisterWidget(ref wi);
wi = (GalilWidgets.GalilWidget)gwDatRec2; gwComs1.GWRegisterWidget(ref wi);
wi = (GalilWidgets.GalilWidget)gwPoll1; gwComs1.GWRegisterWidget(ref wi);
wi = (GalilWidgets.GalilWidget)gwSettings1; gwComs1.GWRegisterWidget(ref wi);
string gi = GalilWidgets.LibraryPath.GclibDllPath_; gi = "C:\\Program Files (x86)\\Galil\\gclib\\dll\\x64\\gclib.dll";
string xmlPath = "C:\\Users\\cbracamontes\\Downloads\\galilwidgets_15\\xml\\4000\\";
gwPoll1.GWLoadFile(xmlPath + "GWPoll_Revised.xml");
gwDatRec2.GWLoadFile(xmlPath + "GWDatRec_IO.xml"); //assuming #2 is upper right
gwDatRec1.GWLoadFile(xmlPath + "GWDatRec_Axis_A_Revised.xml");
gwSettings1.GWLoadFile(xmlPath + "GWSettings_Revised.xml");
connected = gwComs1.GWOpen("172.18.114.194"); //put the correct connection address here
controller = new MotionController(gwComs1);
profilometer.GetSerialPorts(cboBaudRate, cboPorts, cboDataBits, cboStopBits,
cboParity, cboHandShaking, portsButtonClicked);
cboBaudRate.Hide();
cboDataBits.Hide();
cboHandShaking.Hide();
cboParity.Hide();
cboPorts.Hide();
cboStopBits.Hide();
btnPorts.Hide();
btnPortState.Hide();
btnHello.Hide();
}
private void btnGetSerialPorts_Click(object sender, EventArgs e)
{
//Acknowledge this button has been clicked
portsButtonClicked = true;
#region commented out old code
/*
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
//Acknowledge this button has been clicked
portsButtonClicked = true;
//Com Ports
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
cboPorts.Items.Add(ArrayComPortsNames[index]);
} while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
//get first item print in text
cboPorts.Text = ArrayComPortsNames[0];
//Baud Rate
cboBaudRate.Items.Add(300);
cboBaudRate.Items.Add(600);
cboBaudRate.Items.Add(1200);
cboBaudRate.Items.Add(2400);
cboBaudRate.Items.Add(9600);
cboBaudRate.Items.Add(14400);
cboBaudRate.Items.Add(19200);
cboBaudRate.Items.Add(38400);
cboBaudRate.Items.Add(57600);
cboBaudRate.Items.Add(115200);
cboBaudRate.Items.ToString();
//9600 Baud Rate as default
cboBaudRate.Text = cboBaudRate.Items[4].ToString();
//Data Bits
cboDataBits.Items.Add(7);
cboDataBits.Items.Add(8);
//8 data bits as default
cboDataBits.Text = cboDataBits.Items[1].ToString();
//Stop Bits
cboStopBits.Items.Add("One");
cboStopBits.Items.Add("OnePointFive");
cboStopBits.Items.Add("Two");
//get the first item print in the text
cboStopBits.Text = cboStopBits.Items[0].ToString();
//Parity
cboParity.Items.Add("None");
cboParity.Items.Add("Even");
cboParity.Items.Add("Mark");
cboParity.Items.Add("Odd");
cboParity.Items.Add("Space");
//get the first item print in the text
cboParity.Text = cboParity.Items[0].ToString();
//Handshake
cboHandShaking.Items.Add("None");
cboHandShaking.Items.Add("XOnXOff");
cboHandShaking.Items.Add("RequestToSend");
cboHandShaking.Items.Add("RequestToSendXOnXOff");
//get the first item print it in the text
cboHandShaking.Text = cboHandShaking.Items[0].ToString();
*/
#endregion
}
private void btnPortState_Click(object sender, EventArgs e)
{
profilometer.PortState(btnPortState, cboPorts, cboBaudRate, cboDataBits, cboStopBits, cboHandShaking, cboParity);
#region
/*
if (btnPortState.Text == "Closed")
{
btnPortState.Text = "Open";
ComPort.PortName = Convert.ToString(cboPorts.Text);
ComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
ComPort.DataBits = Convert.ToInt16(cboDataBits.Text);
ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboStopBits.Text);
ComPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), cboHandShaking.Text);
ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboParity.Text);
ComPort.Open();
}
else if (btnPortState.Text == "Open")
{
btnPortState.Text = "Closed";
ComPort.Close();
}
*/
#endregion
}
private void rtbOutgoing_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) // enter key
{
System.Threading.Thread.Sleep(1000);
profilometer.Command(txtCommand.Text + "\r");
System.Threading.Thread.Sleep(2000);
while (profilometer.Parameter.Count == 0)
{
e.Handled = false;
}
if (profilometer.Parameter.Count > 0)
{
rtbIncoming.Text += profilometer.Parameter[i];
i = i + 1;
}
txtCommand.Text = "";
}
else if (e.KeyChar < 32 || e.KeyChar > 126)
{
e.Handled = true; // ignores anything else outside printable ASCII range
}
else
{
//Wrap around try catch because will
//will throw exception if port is closed
try
{
// ComPort.Write(e.KeyChar.ToString());
}
catch (InvalidOperationException error)
{
MessageBox.Show(error.Message);
}
//profilometer.UserCommand(e.KeyChar, e.Handled, txtCommand);
#region Old Code
/*
if (e.KeyChar == (char)13) // enter key
{
ComPort.Write("\r\n");
txtCommand.Text = "";
}
else if (e.KeyChar < 32 || e.KeyChar > 126)
{
e.Handled = true; // ignores anything else outside printable ASCII range
}
else
{
ComPort.Write(e.KeyChar.ToString());
}
*/
#endregion
}
}
private void btnHello_Click(object sender, EventArgs e)
{
profilometer.results(rtbIncoming);
}
private void btnStart_Click(object sender, EventArgs e)
{
// Will add the Xpoints, lenght between, Rotations, Rotaion Step
//Come back here and handle possible errors in textbox
// like unwanted characters or numbers i.e. Rotations*RotationsStep <= 360(full rev)
int inches;
int rotations;
int rotationStep;
if (textBoxLengthBetweenPointsX != null)
{
inches = int.Parse(textBoxLengthBetweenPointsX.Text);
rotations = int.Parse(textBoxRotations.Text);
rotationStep = int.Parse(textBoxLengthBetweenPointsDegrees.Text);
}
else
{
inches = 0;
rotations = 0;
rotationStep = 0;
}
controller.CheckProgramLoaded(comboBoxMeasurePoints.SelectedIndex+1,inches,rotations,rotationStep);
}
private void rtbIncoming_TextChanged(object sender, EventArgs e)
{
if (profResult == "OK000\r")
{
//string command2 = "ST";
//gwComs1.GWCommand(ref command2);
//System.Threading.Thread.Sleep(1000);
//gwComs1.GWCommand(ref command, true);
}
else
{
//rtbIncoming.Clear();
// ComPort.Write("RDSTU00\r");
}
//stateMachine();
/*
if (profResult == "OK\nOK001\n")
{
}
*/
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
controller.CloseController();
}
/// <summary>
/// Where program logic flows through
/// Returns a message from the motion controller
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Message(ref GalilWidgets.GWComs sender, ref string e)
{
if (!controller.InMotion(e))
{
System.Threading.Thread.Sleep(1000);
profilometer.Start(); //Begin profilometer measurement
profilometer.results(rtbIncoming); //Display contents of results to rich text box
controller.Resume(); //Resume controller to next position
}
}
private void XlExportButton_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("You a fool", "Error");
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(Type.Missing);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; xlWorkSheet.Name = "Profilometer";
foreach (var parameter in profilometer.Parameter)
{
xCellIndex += 1;
xlWorkSheet.Cells[xCellIndex, 1] = parameter;
}
xlWorkBook.SaveAs("csharp_excel.xls");
xlWorkBook.Close(true, "csharp_excel.xls");
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file C:\\csharp_excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
private void checkBoxManualSetting_CheckedChanged(object sender, EventArgs e)
{
if(checkBoxManualSetting.Checked)
{
cboBaudRate.Show();
cboDataBits.Show();
cboHandShaking.Show();
cboParity.Show();
cboPorts.Show();
cboStopBits.Show();
btnPorts.Show();
btnPortState.Show();
btnHello.Show();
}
else
{
cboBaudRate.Hide();
cboDataBits.Hide();
cboHandShaking.Hide();
cboParity.Hide();
cboPorts.Hide();
cboStopBits.Hide();
btnPorts.Hide();
btnPortState.Hide();
btnHello.Hide();
}
}
private void comboBoxMeasurePoints_SelectedIndexChanged(object sender, EventArgs e)
{
controller.CommandController("DA *[]"); //Will deallocate motion controller arrays to allow for a new set
}
}
}