/// <summary> /// The main re-coding function /// Reads the calibration tokens, codes and reprograms the same tokens /// </summary> /// <param name="cancel"></param> public void Run(CancellationToken cancel) { // Check to see if Ember is to be used as USB and open ISA channel if so // Also set the box address Ember.Interfaces ember_interface = (Ember.Interfaces)Enum.Parse( typeof(Ember.Interfaces), Properties.Settings.Default.Ember_Interface); _ember.Interface = ember_interface; if (_ember.Interface == Ember.Interfaces.USB) { _ember.Interface_Address = Properties.Settings.Default.Ember_Interface_USB_Address; fire_run_status("Open ISA Channels"); _ember.OpenISAChannels(); } else { _ember.Interface_Address = Properties.Settings.Default.Ember_Interface_IP_Address; } TCLI.Tokens caltokens = new TCLI.Tokens(eui: "", ifactor: 0, vfactor: 0, igain: 0, vgain: 0); string msg = ""; bool got_tokens = false; string log_file = ""; string eui = ""; while (true) { if (cancel.IsCancellationRequested) break; try { // Create a new telnet connection fire_run_status("Start telnet"); // If interface is USB we use localhost string telnet_address = "localhost"; if (_ember.Interface == Ember.Interfaces.IP) telnet_address = _ember.Interface_Address; _telnet_connection = new TelnetConnection(telnet_address, 4900); fire_run_status("Get EUI"); eui = TCLI.Get_EUI(_telnet_connection); //DialogResult rc = ShowInputDialog(ref serial_number, inputbox_label: "Serial"); //if (rc == DialogResult.Cancel) // throw new Exception("Serial number not entered"); fire_status("EUI = " + eui); fire_run_status("Get UUT Tokens"); string cmd_prefix = TCLI.Get_Custom_Command_Prefix(_telnet_connection); caltokens = TCLI.Parse_Pinfo_Tokens(_telnet_connection, cmd_prefix); caltokens.EUI = eui; msg = string.Format("Voltage Factor: {0}, ", caltokens.VoltageFactor); msg += string.Format("Current Factor: {0}, ", caltokens.CurrentFactor); msg += string.Format("VGain Token: 0x{0:X08}, ", caltokens.VoltageGainToken); msg += string.Format("CGain Token: 0x{0:X08}", caltokens.CurrentGainToken); fire_status(msg); string filename = string.Format("tokens_{0}-{1:yyyy-MM-dd_hh-mm-ss-tt}.txt", eui, DateTime.Now); log_file = Path.Combine(_app_data_dir, filename); // Path to the app log file msg = string.Format("::EUI: {0}\r\n::{1}\r\n", eui, msg); File.WriteAllText(log_file, msg); got_tokens = true; break; } catch (Exception ex) { string retry_err_msg = ex.Message; int max_len = 1000; if (retry_err_msg.Length > max_len) retry_err_msg = retry_err_msg.Substring(0, max_len) + "..."; DialogResult dlg_rc = MessageBox.Show(retry_err_msg, "Exception reading tokens", MessageBoxButtons.RetryCancel); if (dlg_rc == System.Windows.Forms.DialogResult.Cancel) throw; } finally { _telnet_connection.Close(); _ember.CloseISAChannels(); } } if (!got_tokens) { throw new Exception("Tokens not read"); } try { // Code here fire_run_status("Code"); Coder coder = new Coder(new TimeSpan(0, 2, 0)); coder.Code(cancel); } catch (Exception) { throw; } finally { // Repatch the tokens fire_run_status("Patch UUT Tokens"); //caltokens.VoltageGainToken = 0x00405E00; //caltokens.CurrentGainToken = 0X002F7EB7; //caltokens.VoltageFactor = 240; //caltokens.CurrentFactor = 15; _ember.VoltageRefereceValue = caltokens.VoltageFactor; _ember.CurrentRefereceValue = caltokens.CurrentFactor; string cmd_str = _ember.CreateCalibrationPatchBath(vgain: caltokens.VoltageGainToken, igain: caltokens.CurrentGainToken); if (File.Exists(log_file)) { File.AppendAllText(log_file, cmd_str); } while (true) { string[] ember_temp_files = Ember.CleanupTempPatchFile(); foreach (string file in ember_temp_files) fire_status(string.Format("Ember temp file found and removed {0}", file)); try { string patch_output = _ember.RunCalibrationPatchBatch(); break; } catch (Exception ex) { string retry_err_msg = ex.Message; int max_len = 1000; if (retry_err_msg.Length > max_len) retry_err_msg = retry_err_msg.Substring(0, max_len) + "..."; DialogResult dlg_rc = MessageBox.Show(retry_err_msg, "Exception patching tokens", MessageBoxButtons.RetryCancel); if (dlg_rc == System.Windows.Forms.DialogResult.Cancel) throw; } } } }
/// <summary> /// Starts the coding task /// </summary> void code() { // Init coder Coder coder = new Coder(new TimeSpan(0, 2, 0)); coder.Status_Event += coder_Status_Event; openTelnet(); coder.Ember = _ember; _coding_error_msg = null; setRunStatus("Start Coding", Color.Black, Color.White); updateOutputStatus("Start Coding".PadBoth(80, '-')); relaysShowSetttings(); // Run coding _cancel_token_uut = new CancellationTokenSource(); _task_uut = new Task(() => coder.Code(_cancel_token_uut.Token), _cancel_token_uut.Token); _task_uut.ContinueWith(coding_exception_handler, TaskContinuationOptions.OnlyOnFaulted); _task_uut.ContinueWith(coding_done_handler, TaskContinuationOptions.OnlyOnRanToCompletion); _task_uut.Start(); }