2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
10 using System.Threading;
11 using System.Globalization;
12 using System.Diagnostics;
14 namespace CLEON_Connector
16 public partial class Form1 : Form
18 #region variable declaration
20 byte USBRecvFrameByteCount = 0;
22 byte[] USBRecvFrame =
new byte[Constants.serialFrameLength];
25 byte[] USBSsndFrame =
new byte[Constants.serialFrameLength];
28 volatile bool bFLAG_USBFrameReceived =
false;
41 InitializeComponent();
45 private void Form1_Load(
object sender, EventArgs e)
48 string[] ports = SerialPort.GetPortNames();
51 foreach (
string port
in ports)
54 comboBox_availableComPort.Items.Add(port);
58 comboBox_availableComPort.Text =
"COM4";
61 serialPort1.DataReceived +=
new SerialDataReceivedEventHandler(serialPort1_DataReceived);
64 iSampleCount =
int.Parse(textBox_SampleCount.Text.ToString());
65 iSampleGap =
int.Parse(textBox_sampleGap.Text.ToString());
66 iChunkCount =
int.Parse(textBox_ChunkCount.Text.ToString());
67 iChunkGap =
int.Parse(textBox_chunkGap.Text.ToString());
68 iBatteryCapacity =
int.Parse(textBox_batteryCapacity.Text.ToString());
70 TestIfParametersAreValid();
74 private void Timer_Tick(
object sender, EventArgs e)
77 label_currentTime.Text = DateTime.Now.ToString(
new CultureInfo(
"en-US"));
80 label_currentTimeTick.Text = DateTime.UtcNow.Ticks.ToString();
84 private void button_Connect_Click(
object sender, EventArgs e)
86 if (button_connect.Text ==
"Connect")
89 if (comboBox_availableComPort.SelectedItem != null)
94 serialPort1.PortName = comboBox_availableComPort.SelectedItem.ToString();
104 serialPort1.PortName = comboBox_availableComPort.Text;
109 if (TestIfParametersAreValid() == Errors.NoErrors)
115 serialPort1.ReadExisting();
118 comboBox_availableComPort.Enabled =
false;
121 button_connect.Text =
"Disconnect";
124 button_updateCLEON.Enabled =
true;
127 toolStripStatusLabel.Text = serialPort1.PortName.ToString() +
" is opened";
131 toolStripStatusLabel.Text =
"Invalid parameter exits";
134 catch (Exception Exception)
137 MessageBox.Show(Exception.Message);
140 else if (button_connect.Text ==
"Disconnect")
146 comboBox_availableComPort.Enabled =
true;
149 button_connect.Text =
"Connect";
152 button_updateCLEON.Text =
"Connect to CLEON (1/3)";
155 button_updateCLEON.Enabled =
false;
156 textBox_SampleCount.Enabled =
true;
157 textBox_sampleGap.Enabled =
true;
158 textBox_ChunkCount.Enabled =
true;
159 textBox_chunkGap.Enabled =
true;
160 textBox_batteryCapacity.Enabled =
true;
163 toolStripStatusLabel.Text = serialPort1.PortName.ToString() +
" is closed";
168 private void pictureBox1_MouseClick(
object sender, EventArgs e)
170 Form2 formHelp =
new Form2();
175 private void button_updateCLEON_Click(
object sender, EventArgs e)
177 if (button_updateCLEON.Text ==
"Connect to CLEON (1/3)")
179 if(TestIfParametersAreValid() == Errors.NoErrors)
182 if (serialPort1.IsOpen)
186 SendUSBCommand(Constants.commandConnect);
188 TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
189 Stopwatch sw = Stopwatch.StartNew();
192 while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
195 if (bFLAG_USBFrameReceived ==
true)
197 bFLAG_USBFrameReceived =
false;
200 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandConnect))
202 toolStripStatusLabel.Text =
"CLEON is connected";
203 textBox_SampleCount.Enabled =
false;
204 textBox_sampleGap.Enabled =
false;
205 textBox_ChunkCount.Enabled =
false;
206 textBox_chunkGap.Enabled =
false;
207 textBox_batteryCapacity.Enabled =
false;
208 button_updateCLEON.Text =
"Update parameters (2/3)";
213 toolStripStatusLabel.Text =
"CLEON doesn't respond";
219 toolStripStatusLabel.Text =
"Invalid parameter exits";
222 else if (button_updateCLEON.Text ==
"Update parameters (2/3)")
224 int iNumberOfParametersSuccessfullyUpdated = 0;
226 TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
229 if (TestIfParametersAreValid() == Errors.NoErrors)
231 textBox_sampleGap.Enabled =
false;
235 SendUSBCommand(Constants.commandUpdateSampleCount);
238 sw = Stopwatch.StartNew();
241 while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
244 if (bFLAG_USBFrameReceived ==
true)
246 bFLAG_USBFrameReceived =
false;
249 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateSampleCount))
251 toolStripStatusLabel.Text =
"Sample count updated";
252 iNumberOfParametersSuccessfullyUpdated++;
257 toolStripStatusLabel.Text =
"CLEON doesn't respond";
263 SendUSBCommand(Constants.commandUpdateSampleGap);
266 sw = Stopwatch.StartNew();
269 while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
272 if (bFLAG_USBFrameReceived ==
true)
274 bFLAG_USBFrameReceived =
false;
277 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateSampleGap))
279 toolStripStatusLabel.Text =
"Sample gap updated";
280 iNumberOfParametersSuccessfullyUpdated++;
285 toolStripStatusLabel.Text =
"CLEON doesn't respond";
291 SendUSBCommand(Constants.commandUpdateChunkCount);
294 sw = Stopwatch.StartNew();
297 while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
300 if (bFLAG_USBFrameReceived ==
true)
302 bFLAG_USBFrameReceived =
false;
305 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateChunkCount))
307 toolStripStatusLabel.Text =
"Chunk count updated";
308 iNumberOfParametersSuccessfullyUpdated++;
313 toolStripStatusLabel.Text =
"CLEON doesn't respond";
319 SendUSBCommand(Constants.commnadUpdateChunkGap);
322 sw = Stopwatch.StartNew();
325 while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
328 if (bFLAG_USBFrameReceived ==
true)
330 bFLAG_USBFrameReceived =
false;
333 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commnadUpdateChunkGap))
335 toolStripStatusLabel.Text =
"Chunk gap updated";
336 iNumberOfParametersSuccessfullyUpdated++;
341 toolStripStatusLabel.Text =
"CLEON doesn't respond";
345 if (iNumberOfParametersSuccessfullyUpdated == 4)
347 toolStripStatusLabel.Text =
"User parameters updated (" + iNumberOfParametersSuccessfullyUpdated +
"/4)";
348 button_updateCLEON.Text =
"Update CLEON time tick (3/3)";
352 toolStripStatusLabel.Text =
"User parameter update failed (" + iNumberOfParametersSuccessfullyUpdated +
"/4)"; ;
356 else if (button_updateCLEON.Text ==
"Update CLEON time tick (3/3)")
360 SendUSBCommand(Constants.commandUpdateRTCTime);
362 TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
363 Stopwatch sw1 = Stopwatch.StartNew();
366 while ((sw1.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
369 if (bFLAG_USBFrameReceived ==
true)
371 bFLAG_USBFrameReceived =
false;
374 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateRTCTime))
376 toolStripStatusLabel.Text =
"RTC time sent successfully";
380 SendUSBCommand(Constants.commandUpdateTimeTick);
382 Stopwatch sw2 = Stopwatch.StartNew();
385 while ((sw2.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
388 if (bFLAG_USBFrameReceived ==
true)
390 bFLAG_USBFrameReceived =
false;
393 if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateTimeTick))
395 toolStripStatusLabel.Text =
"Sent tick : " + currentTimeTick.ToString();
396 button_updateCLEON.Text =
"DONE !!";
401 toolStripStatusLabel.Text =
"CLEON doesn't respond";
407 toolStripStatusLabel.Text =
"CLEON doesn't respond";
412 #region Accquiring text from TextBox
414 private void textBox_numberOfSamples_TextChanged(
object sender, EventArgs e)
416 TestIfParametersAreValid();
420 private void textBox_sampleGap_TextChanged(
object sender, EventArgs e)
422 TestIfParametersAreValid();
426 private void textBox_numberOfChunks_TextChanged(
object sender, EventArgs e)
428 TestIfParametersAreValid();
432 private void textBox_chunkGap_TextChanged(
object sender, EventArgs e)
434 TestIfParametersAreValid();
438 private void textBox_batteryCapacity_TextChanged(
object sender, EventArgs e)
440 TestIfParametersAreValid();