CLEON CONNECTOR  1.0
Parameter updating SW for cloud-offloaded GPS receiver (CLEON)
Form1.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO.Ports;
10 using System.Threading;
11 using System.Globalization;
12 using System.Diagnostics;
13 
14 namespace CLEON_Connector
15 {
16  public partial class Form1 : Form
17  {
18  #region variable declaration
19  // Receive-USB-frame-count for state transition
20  byte USBRecvFrameByteCount = 0;
21  // Receive-USB-frame
22  byte[] USBRecvFrame = new byte[Constants.serialFrameLength];
23 
24  // Send-USB-frame
25  byte[] USBSsndFrame = new byte[Constants.serialFrameLength];
26 
27  // USB reception completion flag
28  volatile bool bFLAG_USBFrameReceived = false;
29 
30  // Parameters
31  int iSampleCount;
32  int iSampleGap;
33  int iChunkCount;
34  int iChunkGap;
35  int iBatteryCapacity;
36  #endregion
37 
38  // Initialization
39  public Form1()
40  {
41  InitializeComponent();
42  }
43 
44  // On loading of Form1
45  private void Form1_Load(object sender, EventArgs e)
46  {
47  // Request all the available ports
48  string[] ports = SerialPort.GetPortNames();
49 
50  // list all the available ports
51  foreach (string port in ports)
52  {
53  // Adding available port to combobox
54  comboBox_availableComPort.Items.Add(port);
55  }
56 
57  // Default com port
58  comboBox_availableComPort.Text = "COM4";
59 
60  // Start receive routine
61  serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
62 
63  // Update user parameter variables
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());
69 
70  TestIfParametersAreValid();
71  }
72 
73  // Time
74  private void Timer_Tick(object sender, EventArgs e)
75  {
76  // Current time
77  label_currentTime.Text = DateTime.Now.ToString(new CultureInfo("en-US"));
78 
79  // Current UTC time tick
80  label_currentTimeTick.Text = DateTime.UtcNow.Ticks.ToString();
81  }
82 
83  // If button 'Connect' is clicked
84  private void button_Connect_Click(object sender, EventArgs e)
85  {
86  if (button_connect.Text == "Connect")
87  {
88  // If nothing is selected, use default com port
89  if (comboBox_availableComPort.SelectedItem != null)
90  {
91  try
92  {
93  // Set port name as default port's name
94  serialPort1.PortName = comboBox_availableComPort.SelectedItem.ToString();
95  }
96  catch
97  {
98 
99  }
100  }
101  else
102  {
103  // Set port name as selected port's name
104  serialPort1.PortName = comboBox_availableComPort.Text;
105  }
106 
107  try
108  {
109  if (TestIfParametersAreValid() == Errors.NoErrors)
110  {
111  // Open port
112  serialPort1.Open();
113 
114  // Flush port
115  serialPort1.ReadExisting();
116 
117  // Disable available port list combo box
118  comboBox_availableComPort.Enabled = false;
119 
120  // Change text to "Disconnect"
121  button_connect.Text = "Disconnect";
122 
123  // Enable 'update CLEON time tick' button
124  button_updateCLEON.Enabled = true;
125 
126  // Update status label with opened port information
127  toolStripStatusLabel.Text = serialPort1.PortName.ToString() + " is opened";
128  }
129  else
130  {
131  toolStripStatusLabel.Text = "Invalid parameter exits";
132  }
133  }
134  catch (Exception Exception)
135  {
136  // Pop up message box for error message
137  MessageBox.Show(Exception.Message);
138  }
139  }
140  else if (button_connect.Text == "Disconnect")
141  {
142  // Close port
143  serialPort1.Close();
144 
145  // Enable combo box
146  comboBox_availableComPort.Enabled = true;
147 
148  // Change text to "Connect"
149  button_connect.Text = "Connect";
150 
151  // Change text of 'CLEON time tick'
152  button_updateCLEON.Text = "Connect to CLEON (1/3)";
153 
154  // Disable 'CLEON time tick' button
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;
161 
162  // Update status label with closed port information
163  toolStripStatusLabel.Text = serialPort1.PortName.ToString() + " is closed";
164  }
165  }
166 
167  // If picture 'Help' is clicked
168  private void pictureBox1_MouseClick(object sender, EventArgs e)
169  {
170  Form2 formHelp = new Form2();
171  formHelp.Show();
172  }
173 
174  // Update CLEON's time information
175  private void button_updateCLEON_Click(object sender, EventArgs e)
176  {
177  if (button_updateCLEON.Text == "Connect to CLEON (1/3)")
178  {
179  if(TestIfParametersAreValid() == Errors.NoErrors)
180  {
181  // If port is opened
182  if (serialPort1.IsOpen)
183  {
184  // Send command 'Connect'
185  // (ACK is expected)
186  SendUSBCommand(Constants.commandConnect);
187 
188  TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
189  Stopwatch sw = Stopwatch.StartNew();
190 
191  // Wait until ACK is received (Duration: 50ms)
192  while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
193 
194  // Check if a frame is received
195  if (bFLAG_USBFrameReceived == true)
196  {
197  bFLAG_USBFrameReceived = false;
198 
199  // Check if received frame is ACK
200  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandConnect))
201  {
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)";
209  }
210  }
211  else
212  {
213  toolStripStatusLabel.Text = "CLEON doesn't respond";
214  }
215  }
216  }
217  else
218  {
219  toolStripStatusLabel.Text = "Invalid parameter exits";
220  }
221  }
222  else if (button_updateCLEON.Text == "Update parameters (2/3)")
223  {
224  int iNumberOfParametersSuccessfullyUpdated = 0;
225 
226  TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
227  Stopwatch sw;
228 
229  if (TestIfParametersAreValid() == Errors.NoErrors)
230  {
231  textBox_sampleGap.Enabled = false;
232 
233  // Send command 'Update sample count'
234  // (ACK is expected)
235  SendUSBCommand(Constants.commandUpdateSampleCount);
236 
237  // Start stop watch
238  sw = Stopwatch.StartNew();
239 
240  // Wait until ACK is received (Duration: 50ms)
241  while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
242 
243  // Check if a frame is received
244  if (bFLAG_USBFrameReceived == true)
245  {
246  bFLAG_USBFrameReceived = false;
247 
248  // Check if received frame is ACK
249  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateSampleCount))
250  {
251  toolStripStatusLabel.Text = "Sample count updated";
252  iNumberOfParametersSuccessfullyUpdated++;
253  }
254  }
255  else
256  {
257  toolStripStatusLabel.Text = "CLEON doesn't respond";
258  return;
259  }
260 
261  // Send command 'Update sample gap'
262  // (ACK is expected)
263  SendUSBCommand(Constants.commandUpdateSampleGap);
264 
265  // Start stop watch
266  sw = Stopwatch.StartNew();
267 
268  // Wait until ACK is received (Duration: 50ms)
269  while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
270 
271  // Check if a frame is received
272  if (bFLAG_USBFrameReceived == true)
273  {
274  bFLAG_USBFrameReceived = false;
275 
276  // Check if received frame is ACK
277  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateSampleGap))
278  {
279  toolStripStatusLabel.Text = "Sample gap updated";
280  iNumberOfParametersSuccessfullyUpdated++;
281  }
282  }
283  else
284  {
285  toolStripStatusLabel.Text = "CLEON doesn't respond";
286  return;
287  }
288 
289  // Send command 'Update chunk count'
290  // (ACK is expected)
291  SendUSBCommand(Constants.commandUpdateChunkCount);
292 
293  // Start stop watch
294  sw = Stopwatch.StartNew();
295 
296  // Wait until ACK is received (Duration: 50ms)
297  while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
298 
299  // Check if a frame is received
300  if (bFLAG_USBFrameReceived == true)
301  {
302  bFLAG_USBFrameReceived = false;
303 
304  // Check if received frame is ACK
305  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateChunkCount))
306  {
307  toolStripStatusLabel.Text = "Chunk count updated";
308  iNumberOfParametersSuccessfullyUpdated++;
309  }
310  }
311  else
312  {
313  toolStripStatusLabel.Text = "CLEON doesn't respond";
314  return;
315  }
316 
317  // Send command 'Update chunk gap'
318  // (ACK is expected)
319  SendUSBCommand(Constants.commnadUpdateChunkGap);
320 
321  // Start stop watch
322  sw = Stopwatch.StartNew();
323 
324  // Wait until ACK is received (Duration: 50ms)
325  while ((sw.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
326 
327  // Check if a frame is received
328  if (bFLAG_USBFrameReceived == true)
329  {
330  bFLAG_USBFrameReceived = false;
331 
332  // Check if received frame is ACK
333  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commnadUpdateChunkGap))
334  {
335  toolStripStatusLabel.Text = "Chunk gap updated";
336  iNumberOfParametersSuccessfullyUpdated++;
337  }
338  }
339  else
340  {
341  toolStripStatusLabel.Text = "CLEON doesn't respond";
342  return;
343  }
344 
345  if (iNumberOfParametersSuccessfullyUpdated == 4)
346  {
347  toolStripStatusLabel.Text = "User parameters updated (" + iNumberOfParametersSuccessfullyUpdated + "/4)";
348  button_updateCLEON.Text = "Update CLEON time tick (3/3)";
349  }
350  else
351  {
352  toolStripStatusLabel.Text = "User parameter update failed (" + iNumberOfParametersSuccessfullyUpdated + "/4)"; ;
353  }
354  }
355  }
356  else if (button_updateCLEON.Text == "Update CLEON time tick (3/3)")
357  {
358  // Send command 'Update RTC time'
359  // (ACK is expected)
360  SendUSBCommand(Constants.commandUpdateRTCTime);
361 
362  TimeSpan maxDuration = TimeSpan.FromMilliseconds(50);
363  Stopwatch sw1 = Stopwatch.StartNew();
364 
365  // Wait until ACK is received (Duration: 50ms)
366  while ((sw1.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
367 
368  // Check if a frame is received
369  if (bFLAG_USBFrameReceived == true)
370  {
371  bFLAG_USBFrameReceived = false;
372 
373  // Check if received frame is ACK
374  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateRTCTime))
375  {
376  toolStripStatusLabel.Text = "RTC time sent successfully";
377 
378  // Send command 'Update Time Tick'
379  // (ACK is expected)
380  SendUSBCommand(Constants.commandUpdateTimeTick);
381 
382  Stopwatch sw2 = Stopwatch.StartNew();
383 
384  // Wait until ACK is received (Duration: 50ms)
385  while ((sw2.Elapsed < maxDuration) && !bFLAG_USBFrameReceived) ;
386 
387  // Check if a frame is received
388  if (bFLAG_USBFrameReceived == true)
389  {
390  bFLAG_USBFrameReceived = false;
391 
392  // Check if received frame is ACK
393  if ((USBRecvFrame[3] == Constants.commandAck) && (USBRecvFrame[4] == Constants.commandUpdateTimeTick))
394  {
395  toolStripStatusLabel.Text = "Sent tick : " + currentTimeTick.ToString();
396  button_updateCLEON.Text = "DONE !!";
397  }
398  }
399  else
400  {
401  toolStripStatusLabel.Text = "CLEON doesn't respond";
402  }
403  }
404  }
405  else
406  {
407  toolStripStatusLabel.Text = "CLEON doesn't respond";
408  }
409  }
410  }
411 
412  #region Accquiring text from TextBox
413  // Update the value for the 'number of samples' and check if the value is valid
414  private void textBox_numberOfSamples_TextChanged(object sender, EventArgs e)
415  {
416  TestIfParametersAreValid();
417  }
418 
419  // Update the value for the 'sample gap' and check if the value is valid
420  private void textBox_sampleGap_TextChanged(object sender, EventArgs e)
421  {
422  TestIfParametersAreValid();
423  }
424 
425  // Update the value for the 'number of chunks' and check if the value is valid
426  private void textBox_numberOfChunks_TextChanged(object sender, EventArgs e)
427  {
428  TestIfParametersAreValid();
429  }
430 
431  // Update the value for the 'chunk gap' and check if the value is valid
432  private void textBox_chunkGap_TextChanged(object sender, EventArgs e)
433  {
434  TestIfParametersAreValid();
435  }
436 
437  // Update the value for the 'battery capacity' and check if the value is valid
438  private void textBox_batteryCapacity_TextChanged(object sender, EventArgs e)
439  {
440  TestIfParametersAreValid();
441  }
442  #endregion
443  }
444 }