/// <summary> /// Start to sweep the specified channel asynchronously /// </summary> /// <param name="Channel"></param> /// <param name="DataReadCallback"></param> /// <returns></returns> private async Task SweepAsync(Action <double, List <double> > DataReadCallback) { DateTime startTime = DateTime.Now; try { OnMessagesUpdated?.Invoke(this, $"Start to sweep from {Config.SweepStart}nm to {Config.SweepEnd}nm with step {Config.SweepStep}nm ..."); IProgress <double> progressChanged = new Progress <double>(prog => { OnSweepingProgressChanged?.Invoke(this, prog); }); IProgress <string> ProgressMessageUpdate = new Progress <string>(msg => { OnMessagesUpdated?.Invoke(this, msg); }); // reset the cancellation token source ctsSweep = new CancellationTokenSource(); await Task.Run(() => { // set the optical power TunableLaser.SetPower(this.Config.OpticalPower); // turn on the laser TunableLaser.SetOutput(true); if (TunableLaser.GetOutput() == false) { throw new Exception("Unable to turn on the laser."); } for (int i = 0; i < Config.MaxChannel; i++) { SourceMeter[i].SetDataElement(Keithley2400.EnumDataStringElements.CURR); SourceMeter[i].SetMeasurementFunc(Keithley2400.EnumMeasFunc.ONCURR); SourceMeter[i].SetSourceMode(Keithley2400.EnumSourceMode.VOLT); SourceMeter[i].SetComplianceCurrent(Keithley2400.EnumComplianceLIMIT.REAL, 0.01); // SourceMeter.SetMeasRangeOfAmps(Keithley2400.EnumMeasRangeAmps.R1MA); SourceMeter[i].SetMeasRangeOfAmps(Keithley2400.EnumMeasRangeAmps.R1MA); SourceMeter[i].SetVoltageSourceLevel(0); SourceMeter[i].SetOutputState(true); Thread.Sleep(100); SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR); Thread.Sleep(100); SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR); Thread.Sleep(100); SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR); Thread.Sleep(100); SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR); Thread.Sleep(100); SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR); } for (var lambda = Config.SweepStart; lambda <= Config.SweepEnd; lambda += Config.SweepStep) { lambda = Math.Round(lambda, 3); //TODO Add data fetching process here TunableLaser.SetWavelenght(lambda); Thread.Sleep(30); List <double> intensityList = new List <double>(); for (int i = 0; i < Config.MaxChannel; i++) { var intensity = SourceMeter[i].GetMeasuredData(Keithley2400.EnumDataStringElements.CURR).Current; intensity *= 1000; // convert A to mA intensityList.Add(Math.Abs(intensity)); } // add the point to collection on UI thread System.Windows.Application.Current.Dispatcher.Invoke(() => { DataReadCallback.Invoke(lambda, intensityList); }); // calculate the progress in percent var prog = (lambda - Config.SweepStart) / (Config.SweepEnd - Config.SweepStart); progressChanged.Report(prog); // check if the task should be canceled if (ctsSweep.Token.IsCancellationRequested) { ProgressMessageUpdate.Report("The sweeping process was canceled."); throw new OperationCanceledException("The sweeping process was canceled by user."); } } }); } catch (AggregateException ae) { StringBuilder sb = new StringBuilder(); foreach (var ex in ae.Flatten().InnerExceptions) { sb.Append(ex.Message); sb.Append("\r\n"); } throw new Exception(sb.ToString()); } finally { #if !FAKE_ME // turn off the laser TunableLaser.SetOutput(false); for (int i = 0; i < Config.MaxChannel; i++) { // turn off 2400 SourceMeter[i].SetOutputState(false); } #endif OnMessagesUpdated?.Invoke(this, $"The sweeping process costs {(DateTime.Now - startTime).TotalSeconds}s"); } }
/// <summary> /// Connect instruments defined in the config file /// </summary> /// <returns></returns> public async Task ConnectInstruments() { IProgress <string> ProgressMessageUpdate = new Progress <string>(msg => { OnMessagesUpdated?.Invoke(this, msg); }); await Task.Run(() => { List <Exception> exceptions = new List <Exception>(); ProgressMessageUpdate.Report("Connecting KEYSIGHT 8164B ..."); // connect keysight 8164B #if !FAKE_ME try { if (TunableLaser.GetDescription().Contains("8164B")) { ProgressMessageUpdate.Report($"{TunableLaser} was found."); } } catch (Exception ex) { ProgressMessageUpdate.Report($"Unabel to find {TunableLaser}, {ex.Message}"); exceptions.Add(ex); } #else ProgressMessageUpdate.Report($"{TunableLaser} was found."); #endif int i = 1; foreach (var sm in this.SourceMeter) { #if !FAKE_ME // connect keithley 2400 try { ProgressMessageUpdate.Report("Connecting KEITHLEY 2400 ..."); if (sm.GetDescription().Contains("2400")) { ProgressMessageUpdate.Report($"{sm} was found."); sm.SetDisplayTextState(1, true); sm.SetDisplayTextMessage(1, $"PLC CH {i}"); } } catch (Exception ex) { ProgressMessageUpdate.Report($"Unabel to find {sm}, {ex.Message}"); exceptions.Add(ex); } i++; #else ProgressMessageUpdate.Report($"{sm} was found."); #endif } if (exceptions.Count > 0) { throw new AggregateException(exceptions); } }); }