Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@
#### [nidmm] Unreleased
- Added
- Added the `nidmm_measure_ac_volts.py` example.
- Added the `nidmm_measure_frequency.py` example.
- (Common) Added `nitlsconfig[grpc]>=1.0.0a2` to the optional gRPC dependencies.
- Changed
- Removed
Expand Down
9 changes: 9 additions & 0 deletions docs/nidmm/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ nidmm_measure_ac_volts.py
:encoding: utf8
:caption: `(nidmm_measure_ac_volts.py) <https://github.com/ni/nimi-python/blob/master/src/nidmm/examples/nidmm_measure_ac_volts.py>`_

nidmm_measure_frequency.py
--------------------------

.. literalinclude:: ../../src/nidmm/examples/nidmm_measure_frequency.py
:language: python
:linenos:
:encoding: utf8
:caption: `(nidmm_measure_frequency.py) <https://github.com/ni/nimi-python/blob/master/src/nidmm/examples/nidmm_measure_frequency.py>`_

nidmm_measurement.py
--------------------

Expand Down
56 changes: 56 additions & 0 deletions src/nidmm/examples/nidmm_measure_frequency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/python

import argparse
import nidmm
import sys
import warnings

# NIDMM_WARN_UNDERRANGE; reported by read() when the input is under range and used here to indicate "signal not detected"
SIGNAL_NOT_DETECTED_WARNING_CODE = 0x3FFA4F80
Comment thread
NI-velmurugan marked this conversation as resolved.
# Required by configure_measurement_digits(); 5.5 is the driver's documented default
RESOLUTION_DIGITS = 5.5


def example(resource_name, options, function, range, max_amplitude):
with nidmm.Session(resource_name=resource_name, options=options) as session:
session.configure_measurement_digits(measurement_function=nidmm.Function[function], range=range, resolution_digits=RESOLUTION_DIGITS)
session.freq_voltage_range = max_amplitude
# The driver reports warnings through Python's warnings module; the warning code is included in the message.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# The driver reports warnings through Python's warnings module; the warning code is included in the message.

with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter('always')
measurement = session.read()
signal_not_detected = any(f'Warning {SIGNAL_NOT_DETECTED_WARNING_CODE} occurred' in str(word.message) for word in caught)
units = 'Hz' if function == 'FREQ' else 's'
print(f'Measurement ({units}): {measurement}')
print(f'Signal not Detected: {signal_not_detected}')


def _main(argsv):
parser = argparse.ArgumentParser(description='Performs a single frequency or period measurement using the NI-DMM API.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Contains the resource_name of the device to initialize.')
parser.add_argument('-f', '--function', default='FREQ', choices=['FREQ', 'PERIOD'], type=str.upper, help='Specifies the measurement_function used to acquire the measurement.')
parser.add_argument('-r', '--range', default=20, type=float, help='Specifies the range for the measurement function. For FREQ, the minimum frequency expected in Hz; for PERIOD, the maximum period expected in seconds.')
parser.add_argument('--max-amplitude', default=50, type=float, help='Specifies the maximum amplitude of the input signal for frequency measurements.')
parser.add_argument('-op', '--option-string', default='', type=str, help='Sets the initial value of certain attributes for the session.')
args = parser.parse_args(argsv)
example(args.resource_name, args.option_string, args.function, args.range, args.max_amplitude)


def main():
_main(sys.argv[1:])


def test_example():
options = {'simulate': True, 'driver_setup': {'Model': '4082', 'BoardType': 'PXIe', }, }
example('PXI1Slot2', options, 'FREQ', 100, 10)
example('PXI1Slot2', options, 'PERIOD', 0.01, 10)


def test_main():
cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:4082; BoardType:PXIe', ]
_main(cmd_line)
_main(cmd_line + ['--function', 'period', '--range', '0.02'])


if __name__ == '__main__':
main()
Loading