/// <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 SimpleMeasurement Measure(Method method, int muxChannel) { _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.Measure() _taskCompletionSource = new TaskCompletionSource <SimpleMeasurement>(); _comm.BeginMeasurement += GetActiveMeasurement; //Start the measurement on the connected device, this triggers an event that updates _activeMeasurement string error = Run(() => _comm.Measure(method, muxChannel)); if (!(string.IsNullOrEmpty(error))) { throw new Exception($"Could not start measurement: {error}"); } _taskCompletionSource.Task.Wait(); return(_taskCompletionSource.Task.Result); }