pylablib.devices.NI package

Submodules

pylablib.devices.NI.daq module

exception pylablib.devices.NI.daq.NIError[source]

Bases: pylablib.core.devio.base.DeviceError

Generic NI error

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception pylablib.devices.NI.daq.NIDAQmxError(exc)[source]

Bases: pylablib.devices.NI.daq.NIError, pylablib.core.devio.comm_backend.DeviceBackendError

NI DAQmx backend operation error

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pylablib.devices.NI.daq.TDeviceInfo(name, model, serial_number)

Bases: tuple

count()

Return number of occurrences of value.

index()

Return first index of value.

Raises ValueError if the value is not present.

model
name
serial_number
pylablib.devices.NI.daq.get_device_info(name)[source]

Get device info.

Return tuple (name, model, serial).

pylablib.devices.NI.daq.list_devices()[source]

List all connected NI DAQ devices

class pylablib.devices.NI.daq.NIDAQ(dev_name='dev0', rate=100.0, buffer_size=100000.0, reset=False)[source]

Bases: pylablib.core.devio.interface.IDevice

National Instruments DAQ device interface (wrapper around nidaqmx library).

Simplified interface to NI DAQ devices. Supports voltage, digital, and counter inputs (all synchronized to the same clock), and digital and voltage outputs (asynchronous).

Parameters:
  • dev_name (str) – root device name.
  • rate (float) – analog input sampling rate (can be adjusted later).
  • buffer_size (int) – size of the input buffer.
  • reset (int) – if True, reset the device upon connection.
Error

alias of NIError

ReraiseError

alias of NIDAQmxError

BackendError

Used by autodoc_mock_imports.

open()[source]

Open the connection

close()[source]

Close the connection

is_opened()[source]

Check if the device is connected

reset()[source]

Reset the device. All channels will be removed

get_device_info()[source]

Get device info.

Return tuple (name, model, serial).

setup_clock(rate, src=None)[source]

Setup analog input clock (which is the main system clock).

If src==None, use internal clock with the given rate; otherwise use src terminal as a clock source (in this case, rate should be higher than the expected source rate).

get_clock_parameters()[source]

Get analog input clock configuration.

Return tuple (rate, src).

export_clock(terminal)[source]

Export system clock to the given terminal (None to disconnect all terminals)

Only terminal one can be active at a time.

get_export_clock_terminal()[source]

Return terminal which outputs system clock (None if none is connected)

add_voltage_input(name, channel, rng=(-10, 10), terminal_cfg='default')[source]

Add analog voltage input.

Readout is synchronized to the system clock.

Parameters:
  • name (str) – channel name to refer to it later.
  • channel (str) – terminal name (e.g., "ai0").
  • rng – voltage range
  • terminal_cfg – terminal configuration; can be "default", "rse" (single-ended, referenced to AI SENSE input), "nrse" (single-ended, referenced to AI GND), "diff" (differential), or "pseudodiff" (see NI DAQ manual for details).
add_counter_input(name, counter, terminal, clk_src='ai/SampleClock', output_format='rate')[source]

Add counter input (value is related to the number of counts).

Readout is synchronized to the system clock.

Parameters:
  • name (str) – channel name.
  • counter (str) – on-board counter name (e.g., "ctr0").
  • terminal (str) – terminal name (e.g., "pfi0").
  • clk_src (str) – source of the counter sampling clock. By default it is the analog input clock, which requires at least one voltage input channel (could be a dummy channel) to be set up first.
  • output_format (str) – output format. Can be "acc" (return accumulated number of counts since the sampling start), "diff" (return number of counts passed between the two consecutive sampling points; essentially, a derivative of "acc"), or "rate" (return count rate based on the "diff" samples).
add_clock_period_input(counter, clk_src='ai/SampleClock')[source]

Add clock period counter.

Useful when using external sample clock with unknown period. The clock input can be returned during read() operation, and it is used to calculate counter inputs in "rate" mode. Readout is synchronized to the system clock.

Parameters:
  • counter (str) – on-board counter name (e.g., "ctr0") to be used for clock measure.
  • clk_src (str) – source of the counter sampling clock. By default it is the analog input clock, which requires at least one voltage input channel (could be dummy channel) to operate.
add_digital_input(name, channel)[source]

Add digital input.

Readout is synchronized to the system clock. :param name: channel name. :type name: str :param channel: terminal name (e.g., "port0/line12"). :type channel: str

get_input_channels(include=('ai', 'ci', 'di'))[source]

Get names of all input channels (voltage input and counter input).

include specifies which channel types to include into the list ("ai" for voltage inputs, "ci" for counter inputs, "di" for digital inputs, "cpi" for clock period channel). The channels order is always fixed: first voltage inputs, then counter inputs, then digital inputs.

get_voltage_input_parameters()[source]

Get parameters (names, channels, output ranges, and terminal configurations) of all analog voltage input channels

get_counter_input_parameters()[source]

Get parameters (names, counters, terminals, clock sources, and output formats) of all counter input channels

get_digital_input_parameters()[source]

Get parameters (names and channels) of all digital input channels

get_clock_period_input_parameters()[source]

Get parameters (counter input) of the clock period input channel

start(flush_read=0, finite=None)[source]

Start the sampling and output task.

flush_read specifies number of samples to read and discard after start. If finite is not None, it specifies finite number of sample to acquire before stopping.

If counter channels are used, the first sample is usually unreliable, so flush_read=1 is recommended; however, if exactly finite pulses are required at the clock export channel, flush_read=0 is needed (the total number of pulses is flush_read+finite).

stop()[source]

Stop the sampling task

is_running()[source]

Check if the task is running

available_samples()[source]

Get number of available samples to read (return 0 if the task is not running)

get_buffer_size()[source]

Get the sampling buffer size

wait_for_sample(num=1, timeout=10.0, wait_time=0.001)[source]

Wait until at least num samples are available.

If they are not available immediately, loop while checking every wait_time interval until enough samples are accumulated. Return the number of available samples if successful, or 0 if the execution timed out.

read(n=1, flush_read=0, timeout=10.0, include=('ai', 'ci', 'di'))[source]

Read n samples. If the task is not running, automatically start before reading and stop after.

Parameters:
  • n (int) – number of samples to read. If n<=0, read all available samples.
  • flush_read (int) – number of initial samples to skip if the task is currently stopped and needs to be started. If counter channels are used, the first sample is usually unreliable, so flush_read=1 is recommended; however, if exactly n pulses are required at the clock export channel, flush_read=0 is needed.
  • include (tuple) – specifies which channel types to include into the list ("ai" for voltage inputs, "ci" for counter inputs, "di" for digital inputs, "cpi" for clock period channel).
Returns:

2D numpy array of values arranged according to get_input_channels() order with the given include parameter.

add_digital_output(name, channel)[source]

Add digital output.

Parameters:
  • name (str) – channel name.
  • channel (str) – terminal name (e.g., "do0").
get_digital_output_channels()[source]

Get names of all digital output channels

get_digital_output_parameters()[source]

Get parameters (names and channels) of all digital output channels

set_digital_outputs(names, values)[source]

Set values of one or several digital outputs.

Parameters:
  • names (str or [str]) – name or list of names of outputs.
  • values – output value or list of values.
get_digital_outputs(names=None)[source]

Get values of one or several digital outputs.

Parameters:names (str or [str] or None) – name or list of names of outputs (None means all outputs).

Return list of values ordered by names (or by get_digital_output_channels() if names==None).

add_voltage_output(name, channel, rng=(-10, 10), initial_value=0.0)[source]

Add analog voltage output.

Parameters:
  • name (str) – channel name.
  • channel (str) – terminal name (e.g., "ao0").
  • rng – voltage range.
  • initial_value (float) – initial output value (has to be initialized).
get_voltage_output_channels()[source]

Get names of all analog voltage output channels

get_voltage_output_parameters()[source]

Get parameters (names, channels and output ranges) of all analog voltage output channels

set_voltage_outputs(names, values)[source]

Set values of one or several analog voltage outputs.

Parameters:
  • names (str or [str]) – name or list of names of outputs.
  • values – output value or list values. These can be single numbers, or arrays if the output clock is setup (see setup_voltage_output_clock()). In the latter case it sets up the output waveforms; not that waveforms for all channels must have the same length (a single number signifying a constant output is also allowed) If the analog output is set up to the finite mode (continuous==False), the finite waveform output happens right away, with the number of samples determined by samps_per_channel parameter of setup_voltage_output_clock(). In this case, if the supplied waveform is shorter than the number of samples, it gets repeated; if it’s longer, it gets cut off.
get_voltage_outputs(names=None)[source]

Get values of one or several analog voltage outputs.

Parameters:names (str or [str] or None) – name or list of names of outputs (None means all outputs).

Return list of values ordered by names (or by get_voltage_output_channels() if names==None). For continuous waveforms, return the array containing a single repetition of the waveform. For finite waveforms, repeat the array containing the last outputted waveform.

class NoParameterCaller(device, kind)

Bases: object

Class to simplify calling functions without a parameter

apply_settings(settings)

Apply the settings.

settings is the dict {name: value} of the device available settings. Non-applicable settings are ignored.

get_device_variable(key)

Get the value of a settings, status, or full info parameter

get_full_info(include=0)

Get dict {name: value} containing full device information (including status and settings).

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

get_full_status(include=0)

Get dict {name: value} containing the device status (including settings).

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

get_settings(include=0)

Get dict {name: value} containing all the device settings.

include specifies either a list of variables (only these variables are returned), a priority threshold (only values with the priority equal or higher are returned), or "all" (all available variables). Since the lowest priority is -10, setting include=-10 queries all available variables, which is equivalent to include="all".

set_device_variable(key, value)

Set the value of a settings parameter

setup_voltage_output_clock(rate=0, sync_with_ai=False, continuous=True, samps_per_chan=1000)[source]

Setup analog output clock configuration.

Parameters:
  • rate – clock rate; if 0, assume constant voltage output (default)
  • sync_with_ai – if True, the clock is synchronized to the analog input clock (the main clock); note that in this case output changes only when the analog read task is running
  • continuous – if True, any written waveform gets repeated continuously; otherwise, it outputs written waveform only once, and then latches the output on the last value
  • samps_per_chan – if continuous==False, it determines number of samples to output before stopping
get_voltage_output_clock_parameters()[source]

Get analog output clock configuration.

Return tuple (rate, sync_with_ai, continuous, samps_per_chan).

Module contents