public double ReadDigitalPulseWidth(string counter, CIPulseWidthStartingEdge StartingEdge, double minPW_InSeconds, double maxPW_InSeconds) { // This example uses the default source (or gate) terminal for // the counter of your device. To determine what the default // counter pins for your device are or to set a different source // (or gate) pin, refer to the Connecting Counter Signals topic // in the NI-DAQmx Help (search for "Connecting Counter Signals"). // Uses default PFI 9/P2.1 as the pulse input. // Returns PW in seconds. double pulseWidth = 0; Task myTask = null; NationalInstruments.DAQmx.CounterReader myCounterReader; try { myTask = new Task(); myTask.CIChannels.CreatePulseWidthChannel(counter, "Meas Pulse Width", minPW_InSeconds, maxPW_InSeconds, StartingEdge, CIPulseWidthUnits.Seconds); //RLC Wait up to 5 sec default was 10 sec myTask.Stream.Timeout = 5000; myCounterReader = new CounterReader(myTask.Stream); // For .NET Framework 2.0 or later, use SynchronizeCallbacks to specify that the object // marshals callbacks across threads appropriately. myCounterReader.SynchronizeCallbacks = true; // For .NET Framework 1.1, set SynchronizingObject to the Windows Form to specify // that the object marshals callbacks across threads appropriately. //myCounterReader.SynchronizingObject = this; //rlc AsyncCallback myCallback = new AsyncCallback(MeasurementCallback); //myCounterReader.BeginReadSingleSampleDouble(myCallback, null); IAsyncResult ar = null; ar = myCounterReader.BeginReadSingleSampleDouble(null, null); pulseWidth = myCounterReader.EndReadSingleSampleDouble(ar); } catch (Exception exception) { mstr_Error = exception.Message; pulseWidth = -1; } finally { if (myTask != null) { myTask.Dispose(); } } return(pulseWidth); }
private void CounterInCallback(IAsyncResult ar) { // Read the measured value try { m_d_MeasuredFrequency = myCounterReader.EndReadSingleSampleDouble(ar); } catch (DaqException exception) { mstr_Error = exception.Message; } finally { Task_MeasureLowFreq.Dispose(); } }