/// <summary> /// Performs a high voltage calibration operation. /// </summary> /// <param name="voltage">The voltage to set in volts.</param> /// <param name="duration">The length of the measurement to take.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to monitor for cancellation requests.</param> /// <exception cref="OperationCanceledException">Cancellation was requested.</exception> /// <exception cref="Ptr32Exception">An error occurred communicating with the device.</exception> private void PerformHVCalibration(int voltage, TimeSpan duration, CancellationToken cancellationToken) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; try { m_logger.TraceEvent(LogLevels.Info, 0, "PTR-32[{0}]: Started HV calibration", DeviceName); m_logger.Flush(); cancellationToken.ThrowIfCancellationRequested(); SetVoltage(voltage, MaxSetVoltageTime, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Ptr32RateCounter counter = new Ptr32RateCounter(m_device); counter.TakeMeasurement(duration, cancellationToken); HVControl.HVStatus status = new HVControl.HVStatus(); status.HVread = m_device.Voltage; status.HVsetpt = voltage; for (int i = 0; i < Ptr32.ChannelCount; i++) { status.counts[i] = (ulong)counter.ChannelCounts[i]; } lock (m_monitor) { m_cancellationTokenSource.Dispose(); m_cancellationTokenSource = null; } m_logger.TraceEvent(LogLevels.Info, 0, "PTR-32[{0}]: Finished HV calibration; read {1} bytes in {2}s", DeviceName, counter.ByteCount, counter.Time.TotalSeconds); m_logger.Flush(); DAQControl.gControl.AppendHVCalibration(status); DAQControl.gControl.StepHVCalibration(); } catch (OperationCanceledException) { m_logger.TraceEvent(LogLevels.Info, 0, "PTR-32[{0}]: Stopped HV calibration", DeviceName); m_logger.Flush(); DAQState = DAQInstrState.Online; DAQControl.CurState.State = DAQInstrState.Online; DAQControl.gControl.MajorOperationCompleted(); // causes pending control thread caller to move forward PendingComplete(); //throw; running in a task, don't throw } catch (Exception ex) { m_logger.TraceEvent(LogLevels.Error, 0, "PTR-32[{0}]: Error during HV calibration: {1}", DeviceName, ex.Message); m_logger.TraceException(ex, true); m_logger.Flush(); DAQState = DAQInstrState.Online; DAQControl.CurState.State = DAQInstrState.Online; DAQControl.gControl.MajorOperationCompleted(); // causes pending control thread caller to move forward PendingComplete(); //throw; running in a task, don't throw } }
/// <summary> /// Performs a high voltage calibration operation. /// </summary> /// <param name="voltage">The voltage to set in volts.</param> /// <param name="duration">The length of the measurement to take.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to monitor for cancellation requests.</param> /// <exception cref="OperationCanceledException">Cancellation was requested.</exception> /// <exception cref="MCADeviceLostConnectionException">An error occurred communicating with the device.</exception> private void PerformHVCalibration(Measurement measurement, int voltage, TimeSpan duration, CancellationToken cancellationToken) { Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; try { m_logger.TraceEvent(LogLevels.Info, 0, "MCA527[{0}]: Started HV calibration", DeviceName); m_logger.Flush(); cancellationToken.ThrowIfCancellationRequested(); uint x = SetVoltage((ushort)voltage, MaxSetVoltageTime, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); HVControl.HVStatus status = new HVControl.HVStatus(); status.HVread = (int)m_device.GetHighVoltage(); status.HVsetpt = voltage; /// begin TakeMeasurement measurement.Cycles.Clear(); Cycle cycle = new Cycle(m_logger); cycle.UpdateDataSourceId(DetectorDefs.ConstructedSource.Live, DetectorDefs.InstrType.MCA527, DateTimeOffset.Now, string.Empty); measurement.Add(cycle); RDT.StartCycle(cycle); cycle.ExpectedTS = new TimeSpan(duration.Ticks); // expected is that requested for HV, not acquire ((MCA527ProcessingState)(RDT.State)).writingFile = false; // never for HV measurement.AnalysisParams = new CountingAnalysisParameters(); measurement.AnalysisParams.Add(new BaseRate()); // prep for a single cycle measurement.CountTimeInSeconds = duration.TotalSeconds; // the requested count time measurement.RequestedRepetitions = measurement.CurrentRepetition = 1; // 1 rep, always set to complete measurement.InitializeResultsSummarizers(); // reset data structures RDT.SetupCountingAnalyzerHandler(NC.App.Config, DetectorDefs.ConstructedSourceExtensions.TimeBase(DetectorDefs.ConstructedSource.Live, DetectorDefs.InstrType.MCA527)); RDT.PrepareAndStartCountingAnalyzers(measurement.AnalysisParams); // 1 rate counter m_setvoltage = false; // override DB settings here PerformAssay(measurement, new MeasTrackParams() { seq = 1, interval = duration.TotalSeconds }, cancellationToken); /// end TakeMeasurement for (int i = 0; i < 1; i++) { status.counts[i] = (ulong)cycle.HitsPerChannel[i]; } if (m_cancellationTokenSource != null) { lock (m_monitor) { m_cancellationTokenSource.Dispose(); m_cancellationTokenSource = null; } } DAQControl.gControl.AppendHVCalibration(status); DAQControl.gControl.StepHVCalibration(); } catch (OperationCanceledException) { m_logger.TraceEvent(LogLevels.Info, 0, "MCA527[{0}]: Stopped HV calibration", DeviceName); m_logger.Flush(); DAQControl.gControl.MajorOperationCompleted(); // causes pending control thread caller to move forward PendingComplete(); //throw; } catch (Exception ex) { m_logger.TraceEvent(LogLevels.Error, 0, "MCA527[{0}]: Error during HV calibration: {1}", DeviceName, ex.Message); m_logger.TraceException(ex, true); m_logger.Flush(); DAQControl.gControl.MajorOperationCompleted(); // causes pending control thread caller to move forward PendingComplete(); //throw; } }