/// <summary> /// Sets the voltage. /// </summary> /// <param name="voltage">The voltage to set in volts.</param> /// <param name="timeout">The maximum length of time to wait for the voltage to be set.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <exception cref="OperationCanceledException">Cancellation was requested through <paramref name="cancellationToken"/>.</exception> /// <exception cref="Ptr32Exception">An error occurred communicating with the device.</exception> /// <exception cref="TimeoutException">The voltage was not set within the allotted time.</exception> /// <remarks> /// This method does not return until one of the following occurs: /// <list type="number"> /// <item><description>The voltage is within <see cref="VoltageTolerance"/> volts of <paramref name="voltage"/>.</description></item> /// <item><description>More than <paramref name="timeout"/> time has elapsed.</description></item> /// <item><description>Cancellation is requested through <paramref name="cancellationToken"/>.</description></item> /// </list> /// </remarks> private void SetVoltage(int voltage, TimeSpan timeout, CancellationToken cancellationToken) { m_logger.TraceEvent(LogLevels.Verbose, 0, "PTR-32[{0}]: Setting voltage to {1} volts, timeout is {2}, tolerance is {3}...", DeviceName, voltage, MaxSetVoltageTime.ToString("g"), VoltageTolerance); if (m_device.VoltageCorrectionFactors != null) { m_logger.TraceEvent(LogLevels.Verbose, 0, "PTR-32[{0}]: Voltage correction factors {1}, {2}...", DeviceName, m_device.VoltageCorrectionFactors[0], m_device.VoltageCorrectionFactors[1]); } m_logger.Flush(); // set the voltage and check it, loop for timeout seconds waiting for it to settle at the desired value m_device.SetVoltage(voltage); Stopwatch stopwatch = Stopwatch.StartNew(); int lrvoltage = m_device.Voltage; // devnote: using an inline eval in the inner loop below sometimes changed so fast that the output had an incorrect follow-on voltage while (Math.Abs(lrvoltage - voltage) > VoltageTolerance) { if (cancellationToken.IsCancellationRequested) { m_logger.TraceEvent(LogLevels.Verbose, 0, "PTR-32[{0}]: Cancellation requested while setting voltage", DeviceName); m_logger.Flush(); cancellationToken.ThrowIfCancellationRequested(); } else if (stopwatch.Elapsed > timeout) { m_logger.TraceEvent(LogLevels.Warning, 0, "PTR-32[{0}]: Timed out while setting voltage (last seen {1} volts)", DeviceName, lrvoltage); m_logger.Flush(); throw new TimeoutException("Timed out while setting voltage"); } lrvoltage = m_device.Voltage; // the register read occurs here if (stopwatch.Elapsed.Milliseconds % 10000 == 0) { m_logger.TraceEvent(LogLevels.Verbose, 0, "PTR-32[{0}]: At {1} volts", DeviceName, lrvoltage); } Thread.Yield(); Thread.Sleep(500); } m_logger.TraceEvent(LogLevels.Verbose, 0, "PTR-32[{0}]: Voltage set to {1} volts (Elapsed time: {2})", DeviceName, lrvoltage, stopwatch.Elapsed.ToString("g")); m_logger.Flush(); }
/// <summary> /// Sets the voltage. /// </summary> /// <param name="voltage">The voltage to set in volts.</param> /// <param name="timeout">The maximum length of time to wait for the voltage to be set.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <exception cref="OperationCanceledException">Cancellation was requested through <paramref name="cancellationToken"/>.</exception> /// <exception cref="MCADeviceLostConnectionException">An error occurred communicating with the device.</exception> /// <exception cref="TimeoutException">The voltage was not set within the allotted time.</exception> /// <remarks> /// This method does not return until one of the following occurs: /// <list type="number"> /// <item><description>The voltage is within <see cref="VoltageTolerance"/> volts of <paramref name="voltage"/>.</description></item> /// <item><description>More than <paramref name="timeout"/> time has elapsed.</description></item> /// <item><description>Cancellation is requested through <paramref name="cancellationToken"/>.</description></item> /// </list> /// </remarks> private uint SetVoltage(ushort voltage, TimeSpan timeout, CancellationToken cancellationToken) { m_logger.TraceEvent(LogLevels.Verbose, 0, "MCA527[{0}]: Setting voltage to {1} volts, timeout is {2}, tolerance is {3}...", DeviceName, voltage, MaxSetVoltageTime.ToString("g"), VoltageTolerance); m_logger.Flush(); Stopwatch stopwatch = Stopwatch.StartNew(); m_device.SetHighVoltage(voltage, BiasInhibitInput.InhibitOff); // don't wait here, but loop and report, permitting cancel at any time in case the HW hose-ed uint hvx = m_device.GetHighVoltage(); while (Math.Abs(hvx - voltage) > VoltageTolerance) { if (cancellationToken.IsCancellationRequested) { m_logger.TraceEvent(LogLevels.Verbose, 0, "MCA527[{0}]: Cancellation requested while setting voltage", DeviceName); m_logger.Flush(); cancellationToken.ThrowIfCancellationRequested(); } else if (stopwatch.Elapsed > timeout) { m_logger.TraceEvent(LogLevels.Warning, 0, "MCA527[{0}]: Timed out while setting voltage (last seen {1} volts)", DeviceName, hvx); m_logger.Flush(); throw new TimeoutException("Timed out while setting voltage"); } hvx = m_device.GetHighVoltage(); if (stopwatch.Elapsed.Milliseconds % 10000 == 0) { m_logger.TraceEvent(LogLevels.Verbose, 0, "MCA527[{0}]: At {1} volts", DeviceName, hvx); } Thread.Sleep(100); } m_logger.TraceEvent(LogLevels.Verbose, 0, "MCA527[{0}]: Voltage set to {1} volts (Elapsed time: {2})", DeviceName, hvx, stopwatch.Elapsed.ToString("g")); m_logger.Flush(); return(hvx); }