Examples

You can download all niscope examples here

niscope_fetch.py

 1#!/usr/bin/python
 2
 3import argparse
 4import niscope
 5import pprint
 6import sys
 7
 8pp = pprint.PrettyPrinter(indent=4, width=80)
 9
10
11def example(resource_name, channels, options, length, voltage):
12    with niscope.Session(resource_name=resource_name, options=options) as session:
13        session.configure_vertical(range=voltage, coupling=niscope.VerticalCoupling.AC)
14        session.configure_horizontal_timing(min_sample_rate=50000000, min_num_pts=length, ref_position=50.0, num_records=1, enforce_realtime=True)
15        with session.initiate():
16            waveforms = session.channels[channels].fetch(num_samples=length)
17        for i in range(len(waveforms)):
18            print(f'Waveform {i} information:')
19            print(str(waveforms[i]) + '\n\n')
20
21
22def _main(argsv):
23    parser = argparse.ArgumentParser(description='Acquires one record from the given channels.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
24    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI digitizer.')
25    parser.add_argument('-c', '--channels', default='0', help='Channel(s) to use')
26    parser.add_argument('-l', '--length', default=1000, type=int, help='Measure record length')
27    parser.add_argument('-v', '--voltage', default=1.0, type=float, help='Voltage range (V)')
28    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
29    args = parser.parse_args(argsv)
30    example(args.resource_name, args.channels, args.option_string, args.length, args.voltage)
31
32
33def main():
34    _main(sys.argv[1:])
35
36
37def test_example():
38    options = {'simulate': True, 'driver_setup': {'Model': '5164', 'BoardType': 'PXIe', }, }
39    example('PXI1Slot2', '0', options, 1000, 1.0)
40
41
42def test_main():
43    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe', ]
44    _main(cmd_line)
45
46
47if __name__ == '__main__':
48    main()
49

niscope_fetch_forever.py

 1#!/usr/bin/python
 2
 3import argparse
 4import hightime
 5import niscope
 6import numpy as np
 7import pprint
 8import sys
 9
10
11pp = pprint.PrettyPrinter(indent=4, width=80)
12
13
14# We use fetch_into which allows us to allocate a single buffer per channel and "fetch into" it a section at a time without having to
15# reconstruct the waveform once we are done
16def example(resource_name, options, total_acquisition_time_in_seconds, voltage, sample_rate_in_hz, samples_per_fetch):
17    total_samples = int(total_acquisition_time_in_seconds * sample_rate_in_hz)
18    # 1. Opening session
19    with niscope.Session(resource_name=resource_name, options=options) as session:
20        # We will acquire on all channels of the device
21        channel_list = [c for c in range(session.channel_count)]  # Need an actual list and not a range
22
23        # 2. Creating numpy arrays
24        waveforms = [np.ndarray(total_samples, dtype=np.float64) for c in channel_list]
25
26        # 3. Configuring
27        session.configure_horizontal_timing(min_sample_rate=sample_rate_in_hz, min_num_pts=1, ref_position=0.0, num_records=1, enforce_realtime=True)
28        session.channels[channel_list].configure_vertical(voltage, coupling=niscope.VerticalCoupling.DC, enabled=True)
29        # Configure software trigger, but never send the trigger.
30        # This starts an infinite acquisition, until you call session.abort() or session.close()
31        session.configure_trigger_software()
32        current_pos = 0
33        # 4. initiating
34        with session.initiate():
35            while current_pos < total_samples:
36                # We fetch each channel at a time so we don't have to de-interleave afterwards
37                # We do not keep the wfm_info returned from fetch_into
38                for channel, waveform in zip(channel_list, waveforms):
39                    # 5. fetching - we return the slice of the waveform array that we want to "fetch into"
40                    session.channels[channel].fetch_into(waveform[current_pos:current_pos + samples_per_fetch], relative_to=niscope.FetchRelativeTo.READ_POINTER,
41                                                         offset=0, record_number=0, num_records=1, timeout=hightime.timedelta(seconds=5.0))
42                current_pos += samples_per_fetch
43
44
45def _main(argsv):
46    parser = argparse.ArgumentParser(description='Fetch more samples than will fit in memory.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
47    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI digitizer.')
48    parser.add_argument('-t', '--time', default=10, type=int, help='Time to sample (s)')
49    parser.add_argument('-v', '--voltage', default=1.0, type=float, help='Voltage range (V)')
50    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
51    parser.add_argument('-r', '--sample-rate', default=1000.0, type=float, help='Sample Rate (Hz)')
52    parser.add_argument('-s', '--samples-per-fetch', default=100, type=int, help='Samples per fetch')
53    args = parser.parse_args(argsv)
54    example(args.resource_name, args.option_string, args.time, args.voltage, args.sample_rate, args.samples_per_fetch)
55
56
57def main():
58    _main(sys.argv[1:])
59
60
61def test_example():
62    options = {'simulate': True, 'driver_setup': {'Model': '5164', 'BoardType': 'PXIe', }, }
63    example('PXI1Slot2', options, 10, 1.0, 1000.0, 100)
64
65
66def test_main():
67    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe', ]
68    _main(cmd_line)
69
70
71if __name__ == '__main__':
72    main()
73

niscope_fetch_into.py

 1#!/usr/bin/python
 2
 3import argparse
 4import niscope
 5import numpy
 6import pprint
 7import sys
 8
 9pp = pprint.PrettyPrinter(indent=4, width=80)
10
11
12def example(resource_name, channels, options, length, voltage):
13    # fetch_into() allows you to preallocate and reuse the destination of the fetched waveforms, which can result in better performance at the expense of the usability of fetch().
14    channels = [ch.strip() for ch in channels.split(",")]
15    num_channels = len(channels)
16    num_records = 5
17    total_num_wfms = num_channels * num_records
18    # preallocate a single array for all samples in all waveforms
19    # Supported array types are: numpy.float64, numpy.int8, numpy.int16, numpy.int32
20    # int8, int16, int32 are for fetching unscaled data, which is the fastest way to fetch.
21    # Gain and Offset are stored in the returned WaveformInfo objects and can be applied to the data by the user later.
22    wfm = numpy.ndarray(length * total_num_wfms, dtype=numpy.float64)
23    with niscope.Session(resource_name=resource_name, options=options) as session:
24        session.configure_vertical(range=voltage, coupling=niscope.VerticalCoupling.AC)
25        session.configure_horizontal_timing(min_sample_rate=50000000, min_num_pts=length, ref_position=50.0, num_records=num_records, enforce_realtime=True)
26        with session.initiate():
27            waveforms = session.channels[channels].fetch_into(waveform=wfm, num_records=num_records)
28        for i in range(len(waveforms)):
29            print(f'Waveform {i} information:')
30            print(f'{waveforms[i]}\n\n')
31            print(f'Samples: {waveforms[i].samples.tolist()}')
32
33
34def _main(argsv):
35    parser = argparse.ArgumentParser(description='Fetches data directly into a preallocated numpy array.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
36    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI digitizer.')
37    parser.add_argument('-c', '--channels', default='0', help='Channel(s) to use')
38    parser.add_argument('-l', '--length', default=100, type=int, help='Measure record length')
39    parser.add_argument('-v', '--voltage', default=1.0, type=float, help='Voltage range (V)')
40    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
41    args = parser.parse_args(argsv)
42    example(args.resource_name, args.channels, args.option_string, args.length, args.voltage)
43
44
45def main():
46    _main(sys.argv[1:])
47
48
49def test_example():
50    options = {'simulate': True, 'driver_setup': {'Model': '5164', 'BoardType': 'PXIe', }, }
51    example('PXI1Slot2', '0, 1', options, 100, 1.0)
52
53
54def test_main():
55    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe', ]
56    _main(cmd_line)
57
58
59if __name__ == '__main__':
60    main()
61

niscope_read.py

 1#!/usr/bin/python
 2
 3import argparse
 4import niscope
 5import pprint
 6import sys
 7
 8pp = pprint.PrettyPrinter(indent=4, width=80)
 9
10
11def example(resource_name, channels, options, length, voltage):
12    with niscope.Session(resource_name=resource_name, options=options) as session:
13        session.configure_vertical(range=voltage, coupling=niscope.VerticalCoupling.AC)
14        session.configure_horizontal_timing(min_sample_rate=50000000, min_num_pts=length, ref_position=50.0, num_records=1, enforce_realtime=True)
15        waveforms = session.channels[channels].read(num_samples=length)
16        for i in range(len(waveforms)):
17            print(f'Waveform {i} information:')
18            print(str(waveforms[i]) + '\n\n')
19
20
21def _main(argsv):
22    parser = argparse.ArgumentParser(description='Acquires one record from the given channels.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
23    parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI digitizer.')
24    parser.add_argument('-c', '--channels', default='0', help='Channel(s) to use')
25    parser.add_argument('-l', '--length', default=1000, type=int, help='Measure record length')
26    parser.add_argument('-v', '--voltage', default=1.0, type=float, help='Voltage range (V)')
27    parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
28    args = parser.parse_args(argsv)
29    example(args.resource_name, args.channels, args.option_string, args.length, args.voltage)
30
31
32def main():
33    _main(sys.argv[1:])
34
35
36def test_example():
37    options = {'simulate': True, 'driver_setup': {'Model': '5164', 'BoardType': 'PXIe', }, }
38    example('PXI1Slot2', '0', options, 1000, 1.0)
39
40
41def test_main():
42    cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe', ]
43    _main(cmd_line)
44
45
46if __name__ == '__main__':
47    main()
48