Exemplo n.º 1
0
        /// <summary>
        /// Runs a measurement as specified in the method on the connected device.
        /// </summary>
        /// <param name="method">The method containing the measurement parameters.</param>
        /// <param name="muxChannel">The mux channel to measure on.</param>
        /// <returns>
        /// A SimpleMeasurement instance containing all the data related to the measurement.
        /// </returns>
        /// <exception cref="System.NullReferenceException">Not connected to a device.</exception>
        /// <exception cref="System.ArgumentException">Method is incompatible with the connected device.</exception>
        /// <exception cref="System.Exception">Could not start measurement.</exception>
        public async Task <SimpleMeasurement> MeasureAsync(Method method, int muxChannel, TaskBarrier taskBarrier = null)
        {
            _activeMeasurement = null;
            if (_comm == null)
            {
                throw new NullReferenceException("Not connected to a device.");
            }

            //Update the autoranging depending on the current ranges supported by the connected device
            if (Connected)
            {
                method.Ranging.SupportedCurrentRanges = Capabilities.SupportedRanges;
            }

            //Check whether method is compatible with the connected device
            ValidateMethod(method, out bool isValidMethod, out List <string> errors);
            if (!isValidMethod)
            {
                throw new ArgumentException("Method is incompatible with the connected device.");
            }

            //Init task to wait for the active measurement to be initiated by CommManager.MeasureAsync()
            _taskCompletionSource        = new TaskCompletionSource <SimpleMeasurement>();
            _comm.BeginMeasurementAsync += GetActiveMeasurementAsync;

            string error = "";

            //Start the measurement on the connected device, this triggers an event that updates _activeMeasurement
            error = await RunAsync <string>(async() =>
            {
                //Need to check again as the task can be scheduled to run at a later point after which this could have changed
                if (_comm == null)
                {
                    throw new NullReferenceException("Not connected to a device");
                }
                return(await _comm.MeasureAsync(method, muxChannel, taskBarrier));
            });

            if (!(string.IsNullOrEmpty(error)))
            {
                throw new Exception($"Could not start measurement: {error}");
            }

            return(await _taskCompletionSource.Task);
        }