private async Task SetCurrentAndTakeELSpec()
        {
            await Task.Run(async() =>
            {
                Debug.WriteLine("SetCurrentAndTakeELSpec");
                KE2400        = await Keithley2400Controller.CreateAsync();
                KE6485        = await Keithley6485Controller.CreateAsync();
                ELSpecCurrent = Convert.ToDouble(await CalculateCurrentForELSpec());
                await KE2400.SetCurrent(Convert.ToDecimal(ELSpecCurrent));
                await Task.Delay(1111).ConfigureAwait(false);
                presentPhotoCurrent = await KE6485.FetchPhotocurrentMeasurement();
                ELSpecData          = new ObservableCollection <ELSpecDatum>(await PRCamera.ELSpecMeasurement().ConfigureAwait(false));
                ELSpecPhotoCurrent  = Convert.ToDouble((presentPhotoCurrent + await KE6485.FetchPhotocurrentMeasurement()) / 2.0m);

                await Task.Delay(1111).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
 public async Task RunVoltageSweep(CancellationToken token = new CancellationToken(), LJVScanSpec spec = null)
 {
     await Task.Run(async() =>
     {
         Debug.WriteLine("RunVoltageSweep Task");
         if (spec != null)
         {
             ActiveLJVScanSpec = spec;
         }
         await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false);
         RawLJVData = new ObservableCollection <RawLJVDatum>();
         KE2400     = await Keithley2400Controller.CreateAsync().ConfigureAwait(false);
         KE6485     = await Keithley6485Controller.CreateAsync().ConfigureAwait(false);
         await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
         await KE2400.TurnOnSource(ActiveLJVScanSpec.StartVoltage);
         await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
         bool hasTakenCameraMeasurement    = false;
         PRCamera.ExceededMeasurementRange = false;
         //if we're using the PR650 need to set params:
         //1:MS-75,2:Sl-1X,0:integration time,1:SI units (cd/m^2)
         if (batchTestSystem == "BTS1")
         {
             await PRCamera.SendCommandAndWaitForResponse("S1,2,,,,0,,1");
         }
         PresentVoltage   = ActiveLJVScanSpec.StartVoltage;
         presentLuminance = 0;
         presentCurrent   = 0;
         ELSpecCurrent    = -1;
         assumedAlpha     = 3.33E9;
         decimal photoCurrentA;//need separate variable from presentphotocurrent to prevent errors with calcs
         int picoRangeCounter   = 0;
         int sourceRangeCounter = 0;
         int timeoutCount       = 0;
         while (!token.IsCancellationRequested && StopConditionsAreNotMet())
         {
             if (PresentVoltage > ActiveLJVScanSpec.StartVoltage)
             {
                 await KE2400.SetNextVoltageStep(PresentVoltage).ConfigureAwait(false);
             }
             //1st photocurrent reading
             photoCurrentA       = await KE6485.FetchPhotocurrentMeasurement().ConfigureAwait(false);
             presentPhotoCurrent = photoCurrentA;
             PresentCurrent      = await KE2400.FetchCurrentMeasurement().ConfigureAwait(false);
             //take a second measurement if range changes to smooth data from unstable devices
             if (presentPhotoCurrent > picoRanges[picoRangeCounter])
             {
                 photoCurrentA       = await KE6485.FetchPhotocurrentMeasurement();
                 presentPhotoCurrent = photoCurrentA;
                 PresentCurrent      = await KE2400.FetchCurrentMeasurement();
                 picoRangeCounter++;
             }
             //take a second measurement if range changes to smooth data from unstable devices
             if (PresentCurrent > sourceRanges[sourceRangeCounter])
             {
                 photoCurrentA       = await KE6485.FetchPhotocurrentMeasurement();
                 presentPhotoCurrent = photoCurrentA;
                 PresentCurrent      = await KE2400.FetchCurrentMeasurement();
                 sourceRangeCounter++;
             }
             if (presentCurrent != -1 && presentPhotoCurrent != -1)//-1 response == measurement timeout
             {
                 var rawDatum = new RawLJVDatum
                 {
                     Voltage       = PresentVoltage,
                     Current       = presentCurrent,
                     PhotoCurrentA = photoCurrentA,
                     Resistance    = Math.Round(presentVoltage / presentCurrent),//R=V/I
                     TimeStamp     = DateTime.Now
                 };
                 Debug.WriteLine("PresentVoltage: " + PresentVoltage);
                 Debug.WriteLine("PresentCurrent: " + PresentCurrent);
                 //2nd photocurrent reading
                 rawDatum.PhotoCurrentB = await KE6485.FetchPhotocurrentMeasurement();
                 if (Convert.ToDouble(presentPhotoCurrent) * assumedAlpha > 42)
                 {
                     if (!PRCamera.ExceededMeasurementRange)
                     {
                         var cameraDatum = await PRCamera.LuminanceMeasurement();
                         if (!PRCamera.ExceededMeasurementRange)
                         {
                             rawDatum.CameraLuminance = cameraDatum.Luminance;
                             rawDatum.CameraCIEx      = cameraDatum.CIEx;
                             rawDatum.CameraCIEy      = cameraDatum.CIEy;
                             //3rd photocurrent reading
                             rawDatum.PhotoCurrentC = await KE6485.FetchPhotocurrentMeasurement();
                             presentPhotoCurrent    = rawDatum.PhotoCurrentC;
                             presentLuminance       = cameraDatum.Luminance;
                             assumedAlpha           = Convert.ToDouble(cameraDatum.Luminance / rawDatum.PhotoCurrentC);
                             //presentLuminance = cameraDatum.Luminance;
                             Debug.Write("Luminance: " + rawDatum.CameraLuminance);
                             Debug.Write("CIEx: " + rawDatum.CameraCIEx);
                             Debug.WriteLine("CIEy: " + rawDatum.CameraCIEy);
                             hasTakenCameraMeasurement = true;
                         }
                         else
                         {
                             var alphaAndR2 = await DataProcessingService.LEDCalculator.AlphaFromRawData(RawLJVData.ToList());
                             assumedAlpha   = alphaAndR2.Item1;
                         }
                     }
                 }
                 else
                 {
                     rawDatum.PhotoCurrentC = rawDatum.PhotoCurrentA;
                 }
                 App.Current.Dispatcher.Invoke((Action) delegate
                 {
                     RawLJVData.Add(rawDatum);
                     RawLJVData.OrderByDescending(x => x.Voltage);
                 });
                 PresentVoltage += ActiveLJVScanSpec.StepSize;
             }
             else if (timeoutCount < 1)//measurement timed out so try again. ONE strikes and you're out
             {
                 timeoutCount++;
                 await Task.Delay(1111);
             }
             else//f**k it we're skipping this step
             {
                 PresentVoltage += ActiveLJVScanSpec.StepSize;
             }
         }
         if (!token.IsCancellationRequested && !PRCamera.ExceededMeasurementRange)
         {
             Debug.WriteLine("LJVScanCoordinator calls for ELSpecMeasurement");
             presentPhotoCurrent = await KE6485.FetchPhotocurrentMeasurement();
             var elspecMeas      = await PRCamera.ELSpecMeasurement(hasTakenCameraMeasurement).ConfigureAwait(false);
             ELSpecData          = new ObservableCollection <ELSpecDatum>(elspecMeas);
             //account for changing current during ELSpec measurement
             ELSpecCurrent      = Convert.ToDouble((PresentCurrent + await KE2400.FetchCurrentMeasurement()) / 2.0m);
             ELSpecPhotoCurrent = Convert.ToDouble((presentPhotoCurrent + await KE6485.FetchPhotocurrentMeasurement()) / 2.0m);
         }
         else if (PRCamera.ExceededMeasurementRange)
         {
             await SetCurrentAndTakeELSpec().ConfigureAwait(false);
         }
         Debug.WriteLine("closing keithleys");
         await Task.Delay(1111).ConfigureAwait(false);
         await KE2400.CloseGPIBDevice().ConfigureAwait(false);
         await KE6485.CloseGPIBDevice().ConfigureAwait(false);
     }).ConfigureAwait(false);
 }