diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b9df09754..a2fc3dfd3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -80,7 +80,7 @@ machine. To build the documentation install the optional docs packages and run sphinx. For example: ```sh -$ poetry install -E docs +$ poetry install --with docs $ poetry run sphinx-build -b html docs docs\_build ``` diff --git a/generated/nidaqmx/stream_readers.py b/generated/nidaqmx/stream_readers.py index 55b6e2fed..82e456d63 100644 --- a/generated/nidaqmx/stream_readers.py +++ b/generated/nidaqmx/stream_readers.py @@ -1,2768 +1,9 @@ -from __future__ import annotations +""" +NI-DAQmx stream readers. +""" -import numpy -from nidaqmx import DaqError +# Import all classes from the subpackage to maintain backward compatibility +from .stream_readers import * # noqa: F403, F401 -from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature -from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE -from nidaqmx.error_codes import DAQmxErrors -from nidaqmx.types import PowerMeasurement, CtrFreq, CtrTick, CtrTime -from nitypes.waveform import AnalogWaveform, DigitalWaveform - -__all__ = ['AnalogSingleChannelReader', 'AnalogMultiChannelReader', - 'AnalogUnscaledReader', 'CounterReader', - 'DigitalSingleChannelReader', 'DigitalMultiChannelReader', - 'PowerSingleChannelReader', 'PowerMultiChannelReader', 'PowerBinaryReader'] - - -class ChannelReaderBase: - """ - Defines base class for all NI-DAQmx stream readers. - """ - - def __init__(self, task_in_stream): - """ - Args: - task_in_stream: Specifies the input stream associated with - an NI-DAQmx task from which to read samples. - """ - self._in_stream = task_in_stream - self._task = task_in_stream._task - self._handle = task_in_stream._task._handle - self._interpreter = task_in_stream._task._interpreter - - self._verify_array_shape = True - - @property - def verify_array_shape(self): - """ - bool: Indicates whether the size and shape of the user-defined - NumPy arrays passed to read methods are verified. Defaults - to True when this object is instantiated. - - Setting this property to True may marginally adversely - impact the performance of read methods. - """ - return self._verify_array_shape - - @verify_array_shape.setter - def verify_array_shape(self, val): - self._verify_array_shape = val - - def _verify_array(self, data, number_of_samples_per_channel, - is_many_chan, is_many_samp): - """ - Verifies that the shape of the specified NumPy array can be used - to read multiple samples from the current task which contains - one or more channels, if the "verify_array_shape" property is - set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - number_of_samples_per_channel (int): Specifies the number of - samples per channel requested. - is_many_chan (bool): Specifies if the read method is a many - channel version. - is_many_samp (bool): Specifies if the read method is a many - samples version. - """ - if not self._verify_array_shape: - return - - channels_to_read = self._in_stream.channels_to_read - number_of_channels = len(channels_to_read.channel_names) - - array_shape: tuple[int, ...] | None = None - if is_many_chan: - if is_many_samp: - array_shape = (number_of_channels, - number_of_samples_per_channel) - else: - array_shape = (number_of_channels,) - else: - if is_many_samp: - array_shape = (number_of_samples_per_channel,) - - if array_shape is not None and data.shape != array_shape: - raise DaqError( - 'Read cannot be performed because the NumPy array passed into ' - 'this function is not shaped correctly. You must pass in a ' - 'NumPy array of the correct shape based on the number of ' - 'channels in task and the number of samples per channel ' - 'requested.\n\n' - 'Shape of NumPy Array provided: {}\n' - 'Shape of NumPy Array required: {}' - .format(data.shape, array_shape), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - def _verify_array_digital_lines( - self, data, is_many_chan, is_many_line): - """ - Verifies that the shape of the specified NumPy array can be used - to read samples from the current task which contains one or more - channels that have one or more digital lines per channel, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the read method is a - many channel version. - is_many_line (bool): Specifies if the read method is a - many line version. - """ - if not self._verify_array_shape: - return - - channels_to_read = self._in_stream.channels_to_read - number_of_channels = len(channels_to_read.channel_names) - number_of_lines = self._in_stream.di_num_booleans_per_chan - - array_shape: tuple[int, ...] | None = None - if is_many_chan: - if is_many_line: - array_shape = (number_of_channels, number_of_lines) - else: - array_shape = (number_of_channels,) - else: - if is_many_line: - array_shape = (number_of_lines,) - - if array_shape is not None and data.shape != array_shape: - raise DaqError( - 'Read cannot be performed because the NumPy array passed into ' - 'this function is not shaped correctly. You must pass in a ' - 'NumPy array of the correct shape based on the number of ' - 'channels in task and the number of digital lines per ' - 'channel.\n\n' - 'Shape of NumPy Array provided: {}\n' - 'Shape of NumPy Array required: {}' - .format(data.shape, array_shape), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - -class AnalogSingleChannelReader(ChannelReaderBase): - """ - Reads samples from an analog input channel in an NI-DAQmx task. - """ - - def read_many_sample( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from a single analog - input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_analog_f64( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample(self, timeout=10): - """ - Reads a single floating-point sample from a single analog input - channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: - - Indicates a single floating-point sample from the task. - """ - return self._interpreter.read_analog_scalar_f64(self._handle, timeout) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveform( - self, - waveform: AnalogWaveform[numpy.float64], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> int: - """ - Reads one or more floating-point samples from a single analog - input channel into a waveform. - - This read method optionally accepts a preallocated waveform to hold - the samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated waveform is valuable in continuous - acquisition scenarios, where the same waveform can be used - repeatedly in each call to the method. - - Args: - waveform (AnalogWaveform): Specifies an AnalogWaveform object - to use for reading samples into. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The provided waveform does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide a larger ' - 'waveform or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - return self._interpreter.read_analog_waveform( - self._handle, - number_of_samples_per_channel, - timeout, - waveform, - self._in_stream.waveform_attribute_mode, - ) - - -class AnalogMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more analog input channels in an NI-DAQmx - task. - """ - - def read_many_sample( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_analog_f64( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample(self, data, timeout=10): - """ - Reads a single floating-point sample from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_analog_f64(self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveforms( - self, - waveforms: list[AnalogWaveform[numpy.float64]], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> int: - """ - Reads one or more floating-point samples from one or more analog - input channels into a list of waveforms. - - This read method optionally accepts a preallocated list of waveforms to hold - the samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated list of waveforms is valuable in continuous - acquisition scenarios, where the same waveforms can be used - repeatedly in each call to the method. - - Args: - waveforms (list[AnalogWaveform]): Specifies a list of AnalogWaveform - objects to use for reading samples into. - The list must contain one waveform for each channel in the task. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_channels = self._in_stream.num_chans - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if len(waveforms) != number_of_channels: - raise DaqError( - f'The number of waveforms provided ({len(waveforms)}) does not match ' - f'the number of channels in the task ({number_of_channels}). Please provide ' - 'one waveform for each channel.', - DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) - - for i, waveform in enumerate(waveforms): - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' - 'waveforms or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - return self._interpreter.read_analog_waveforms( - self._handle, - number_of_samples_per_channel, - timeout, - waveforms, - self._in_stream.waveform_attribute_mode, - ) - - -class AnalogUnscaledReader(ChannelReaderBase): - """ - Reads unscaled samples from one or more analog input channels in an - NI-DAQmx task. - """ - - def read_int16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 16-bit integer samples from one or - more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 16-bit integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_i16( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_int32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 32-bit integer samples from one or - more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 32-bit integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_i32( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 16-bit unsigned integer samples from - one or more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 16-bit unsigned integer values to - hold the samples requested. The size of the array must - be large enough to hold all requested samples from all - channels in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_u16( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled unsigned 32-bit integer samples from - one or more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 32-bit unsigned integer values to - hold the samples requested. The size of the array must - be large enough to hold all requested samples from all - channels in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_u32( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - -class PowerSingleChannelReader(ChannelReaderBase): - """ - Reads samples from an analog input power channel in an NI-DAQmx task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point power samples from a single analog - input power channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the current samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, False, True) - self._verify_array(current_data, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_f64( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - def read_one_sample(self, timeout=10): - """ - Reads a single floating-point power sample from a single analog input - power channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: - - Indicates a single floating-point power sample from the task. - """ - voltage, current = self._interpreter.read_power_scalar_f64(self._handle, timeout) - return PowerMeasurement(voltage=voltage, current=current) - - -class PowerMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more analog input power channels in an NI-DAQmx - task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point power samples from one or more analog - input power channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the voltage samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - current_data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the current samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, True, True) - self._verify_array(current_data, number_of_samples_per_channel, True, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_f64( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - def read_one_sample(self, voltage_data, current_data, timeout=10): - """ - Reads a single floating-point power sample from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(voltage_data, 1, True, False) - self._verify_array(current_data, 1, True, False) - - self._interpreter.read_power_f64( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, - voltage_data, current_data) - - -class PowerBinaryReader(ChannelReaderBase): - """ - Reads binary samples from one or more analog input power channels in an - NI-DAQmx task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more binary int16 samples from one or more analog - input power channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of i16 values to hold the voltage samples requested. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of i16 values to hold the current samples requested. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, True, True) - self._verify_array(current_data, number_of_samples_per_channel, True, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_binary_i16( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - -class CounterReader(ChannelReaderBase): - """ - Reads samples from a counter input channel in an NI-DAQmx task. - """ - - def read_many_sample_double( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from a single counter - input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_counter_f64_ex( - self._handle,number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_pulse_frequency( - self, frequencies, duty_cycles, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of frequency from a - single counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - frequencies (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the frequency - portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - duty_cycles (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the duty - cycle portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - frequencies, number_of_samples_per_channel, False, True) - self._verify_array( - duty_cycles, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_freq( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) - - return samps_per_chan_read - - def read_many_sample_pulse_ticks( - self, high_ticks, low_ticks, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of ticks from a single - counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - high_ticks (numpy.ndarray): Specifies a preallocated 1D - NumPy array of 32-bit unsigned integer values to hold - the high ticks portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - low_ticks (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the low - ticks portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - high_ticks, number_of_samples_per_channel, False, True) - self._verify_array( - low_ticks, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_ticks( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) - - return samps_per_chan_read - - def read_many_sample_pulse_time( - self, high_times, low_times, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of time from a single - counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - high_times (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the high - time portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - low_times (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the low - time portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - high_times, number_of_samples_per_channel, False, True) - self._verify_array( - low_times, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_time( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) - - return samps_per_chan_read - - def read_many_sample_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from a single - counter input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_counter_u32_ex( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_double(self, timeout=10): - """ - Reads a single floating-point sample from a single counter input - channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: Indicates a single floating-point sample from the - task. - """ - return self._interpreter.read_counter_scalar_f64(self._handle, timeout) - - def read_one_sample_pulse_frequency(self, timeout=10): - """ - Reads a pulse sample in terms of frequency from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrFreq: - - Indicates a pulse sample in terms of frequency from the task. - """ - freq, duty_cycle = self._interpreter.read_ctr_freq_scalar(self._handle, timeout) - - return CtrFreq(freq, duty_cycle) - - def read_one_sample_pulse_ticks(self, timeout=10): - """ - Reads a pulse sample in terms of ticks from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrTick: - - Indicates a pulse sample in terms of ticks from the task. - """ - high_ticks, low_ticks = self._interpreter.read_ctr_ticks_scalar(self._handle, timeout) - - return CtrTick(high_ticks, low_ticks) - - def read_one_sample_pulse_time(self, timeout=10): - """ - Reads a pulse sample in terms of time from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrTime: - - Indicates a pulse sample in terms of time from the task. - """ - high_time, low_time = self._interpreter.read_ctr_time_scalar(self._handle, timeout) - - return CtrTime(high_time, low_time) - - def read_one_sample_uint32(self, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from a single - counter input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 32-bit unsigned integer sample from the - task. - """ - return self._interpreter.read_counter_scalar_u32(self._handle, timeout) - - -class DigitalSingleChannelReader(ChannelReaderBase): - """ - Reads samples from a digital input channel in an NI-DAQmx task. - """ - - def read_many_sample_port_byte( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 8-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 16-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u16( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u32( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_multi_line(self, data, timeout=10): - """ - Reads a single boolean sample from a single digital input - channel in a task. The channel can contain multiple digital - lines. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of boolean values to hold the samples requested. - - Each element in the array corresponds to a sample from - a line in the channel. The size of the array must be - large enough to hold all requested samples from the - channel in the task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, False, True) - - _, samps_per_chan_read, num_bytes_per_samp = self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_one_line(self, timeout=10): - """ - Reads a single boolean sample from a single digital input - channel in a task. The channel can contain only one digital - line. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - bool: - - Indicates a single boolean sample from the task. - """ - data = numpy.zeros(1, dtype=bool) - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return bool(data[0]) - - def read_one_sample_port_byte(self, timeout=10): - """ - Reads a single 8-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 8-bit unsigned integer sample from the - task. - """ - data = numpy.zeros(1, dtype=numpy.uint8) - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return int(data[0]) - - def read_one_sample_port_uint16(self, timeout=10): - """ - Reads a single 16-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 16-bit unsigned integer sample from the - task. - """ - data = numpy.zeros(1, dtype=numpy.uint16) - _, samps_per_read_chan = self._interpreter.read_digital_u16( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return int(data[0]) - - def read_one_sample_port_uint32(self, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 32-bit unsigned integer sample from the - task. - """ - return self._interpreter.read_digital_scalar_u32(self._handle, timeout) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveform( - self, - waveform: DigitalWaveform[numpy.uint8], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: float = 10.0, - ) -> int: - """ - Reads one or more digital samples from a single digital input - channel into a waveform. - - Args: - waveform (DigitalWaveform[numpy.uint8]): Specifies a - preallocated DigitalWaveform object to hold the samples - requested. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - return self._interpreter.read_digital_waveform( - self._handle, - number_of_samples_per_channel, - timeout, - waveform, - self._in_stream.waveform_attribute_mode - ) - - -class DigitalMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more digital input channels in an NI-DAQmx - task. - """ - - def read_many_sample_port_byte( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 8-bit unsigned integer samples from one or - more digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 16-bit unsigned integer samples from one or - more digital input channels in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u16( - self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from one or - more digital input channels in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u32( - self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_multi_line(self, data, timeout=10): - """ - Reads a single boolean sample from one or more digital input - channels in a task. The channels can contain multiple digital - lines. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of boolean values to hold the samples requested. - The size of the array must be large enough to hold all - requested samples from all channels in the task; - otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a line from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, True, True) - - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_one_line(self, data, timeout=10): - """ - Reads a single boolean sample from one or more digital input - channels in a task. The channel can contain only one digital - line. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of boolean values to hold the samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, True, False) - - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_byte(self, data, timeout=10): - """ - Reads a single 8-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u8( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_uint16(self, data, timeout=10): - """ - Reads a single 16-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u16( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_uint32(self, data, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u32( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveforms( - self, - waveforms: list[DigitalWaveform[numpy.uint8]], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> list[DigitalWaveform[numpy.uint8]]: - """ - Reads one or more samples from one or more digital - input channels into a list of waveforms. - - Args: - waveforms (list[DigitalWaveform]): Specifies an existing - list of DigitalWaveform objects to use for reading samples into. - The list must contain one waveform - for each channel in the task. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_channels = self._in_stream.num_chans - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if len(waveforms) != number_of_channels: - raise DaqError( - f'The number of waveforms provided ({len(waveforms)}) does not match ' - f'the number of channels in the task ({number_of_channels}). Please provide ' - 'one waveform for each channel.', - DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) - - for i, waveform in enumerate(waveforms): - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' - 'waveforms or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - waveforms = self._interpreter.read_digital_waveforms( - self._handle, - number_of_channels, - number_of_samples_per_channel, - self._in_stream.di_num_booleans_per_chan, - timeout, - waveforms, - self._in_stream.waveform_attribute_mode, - ) - - return waveforms +# Re-export the __all__ list from the subpackage +from .stream_readers import __all__ diff --git a/generated/nidaqmx/stream_readers/__init__.py b/generated/nidaqmx/stream_readers/__init__.py new file mode 100644 index 000000000..e4a29fef7 --- /dev/null +++ b/generated/nidaqmx/stream_readers/__init__.py @@ -0,0 +1,33 @@ +""" +NI-DAQmx stream readers. + +This package provides classes for reading samples from NI-DAQmx tasks. +""" + +from __future__ import annotations + +from nidaqmx import DaqError + +from .analog_single_channel_reader import AnalogSingleChannelReader +from .analog_multi_channel_reader import AnalogMultiChannelReader +from .analog_unscaled_reader import AnalogUnscaledReader +from .counter_reader import CounterReader +from .digital_single_channel_reader import DigitalSingleChannelReader +from .digital_multi_channel_reader import DigitalMultiChannelReader +from .power_readers import ( + PowerSingleChannelReader, + PowerMultiChannelReader, + PowerBinaryReader, +) + +__all__ = [ + 'AnalogSingleChannelReader', + 'AnalogMultiChannelReader', + 'AnalogUnscaledReader', + 'CounterReader', + 'DigitalSingleChannelReader', + 'DigitalMultiChannelReader', + 'PowerSingleChannelReader', + 'PowerMultiChannelReader', + 'PowerBinaryReader', +] diff --git a/generated/nidaqmx/stream_readers/_channel_reader_base.py b/generated/nidaqmx/stream_readers/_channel_reader_base.py new file mode 100644 index 000000000..af7a2db02 --- /dev/null +++ b/generated/nidaqmx/stream_readers/_channel_reader_base.py @@ -0,0 +1,129 @@ +from __future__ import annotations + +from nidaqmx import DaqError + +from nidaqmx.error_codes import DAQmxErrors + +class ChannelReaderBase: + """ + Defines base class for all NI-DAQmx stream readers. + """ + + def __init__(self, task_in_stream): + """ + Args: + task_in_stream: Specifies the input stream associated with + an NI-DAQmx task from which to read samples. + """ + self._in_stream = task_in_stream + self._task = task_in_stream._task + self._handle = task_in_stream._task._handle + self._interpreter = task_in_stream._task._interpreter + + self._verify_array_shape = True + + @property + def verify_array_shape(self): + """ + bool: Indicates whether the size and shape of the user-defined + NumPy arrays passed to read methods are verified. Defaults + to True when this object is instantiated. + + Setting this property to True may marginally adversely + impact the performance of read methods. + """ + return self._verify_array_shape + + @verify_array_shape.setter + def verify_array_shape(self, val): + self._verify_array_shape = val + + def _verify_array(self, data, number_of_samples_per_channel, + is_many_chan, is_many_samp): + """ + Verifies that the shape of the specified NumPy array can be used + to read multiple samples from the current task which contains + one or more channels, if the "verify_array_shape" property is + set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + number_of_samples_per_channel (int): Specifies the number of + samples per channel requested. + is_many_chan (bool): Specifies if the read method is a many + channel version. + is_many_samp (bool): Specifies if the read method is a many + samples version. + """ + if not self._verify_array_shape: + return + + channels_to_read = self._in_stream.channels_to_read + number_of_channels = len(channels_to_read.channel_names) + + array_shape: tuple[int, ...] | None = None + if is_many_chan: + if is_many_samp: + array_shape = (number_of_channels, + number_of_samples_per_channel) + else: + array_shape = (number_of_channels,) + else: + if is_many_samp: + array_shape = (number_of_samples_per_channel,) + + if array_shape is not None and data.shape != array_shape: + raise DaqError( + 'Read cannot be performed because the NumPy array passed into ' + 'this function is not shaped correctly. You must pass in a ' + 'NumPy array of the correct shape based on the number of ' + 'channels in task and the number of samples per channel ' + 'requested.\n\n' + 'Shape of NumPy Array provided: {}\n' + 'Shape of NumPy Array required: {}' + .format(data.shape, array_shape), + DAQmxErrors.UNKNOWN, task_name=self._task.name) + + def _verify_array_digital_lines( + self, data, is_many_chan, is_many_line): + """ + Verifies that the shape of the specified NumPy array can be used + to read samples from the current task which contains one or more + channels that have one or more digital lines per channel, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the read method is a + many channel version. + is_many_line (bool): Specifies if the read method is a + many line version. + """ + if not self._verify_array_shape: + return + + channels_to_read = self._in_stream.channels_to_read + number_of_channels = len(channels_to_read.channel_names) + number_of_lines = self._in_stream.di_num_booleans_per_chan + + array_shape: tuple[int, ...] | None = None + if is_many_chan: + if is_many_line: + array_shape = (number_of_channels, number_of_lines) + else: + array_shape = (number_of_channels,) + else: + if is_many_line: + array_shape = (number_of_lines,) + + if array_shape is not None and data.shape != array_shape: + raise DaqError( + 'Read cannot be performed because the NumPy array passed into ' + 'this function is not shaped correctly. You must pass in a ' + 'NumPy array of the correct shape based on the number of ' + 'channels in task and the number of digital lines per ' + 'channel.\n\n' + 'Shape of NumPy Array provided: {}\n' + 'Shape of NumPy Array required: {}' + .format(data.shape, array_shape), + DAQmxErrors.UNKNOWN, task_name=self._task.name) diff --git a/generated/nidaqmx/stream_readers/analog_multi_channel_reader.py b/generated/nidaqmx/stream_readers/analog_multi_channel_reader.py new file mode 100644 index 000000000..7eef6e59c --- /dev/null +++ b/generated/nidaqmx/stream_readers/analog_multi_channel_reader.py @@ -0,0 +1,227 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import AnalogWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more analog input channels in an NI-DAQmx + task. + """ + + def read_many_sample( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_analog_f64( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample(self, data, timeout=10): + """ + Reads a single floating-point sample from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_analog_f64(self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveforms( + self, + waveforms: list[AnalogWaveform[numpy.float64]], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> int: + """ + Reads one or more floating-point samples from one or more analog + input channels into a list of waveforms. + + This read method optionally accepts a preallocated list of waveforms to hold + the samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated list of waveforms is valuable in continuous + acquisition scenarios, where the same waveforms can be used + repeatedly in each call to the method. + + Args: + waveforms (list[AnalogWaveform]): Specifies a list of AnalogWaveform + objects to use for reading samples into. + The list must contain one waveform for each channel in the task. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_channels = self._in_stream.num_chans + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if len(waveforms) != number_of_channels: + raise DaqError( + f'The number of waveforms provided ({len(waveforms)}) does not match ' + f'the number of channels in the task ({number_of_channels}). Please provide ' + 'one waveform for each channel.', + DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) + + for i, waveform in enumerate(waveforms): + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' + 'waveforms or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + return self._interpreter.read_analog_waveforms( + self._handle, + number_of_samples_per_channel, + timeout, + waveforms, + self._in_stream.waveform_attribute_mode, + ) diff --git a/generated/nidaqmx/stream_readers/analog_single_channel_reader.py b/generated/nidaqmx/stream_readers/analog_single_channel_reader.py new file mode 100644 index 000000000..2df27dffd --- /dev/null +++ b/generated/nidaqmx/stream_readers/analog_single_channel_reader.py @@ -0,0 +1,187 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import AnalogWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogSingleChannelReader(ChannelReaderBase): + """ + Reads samples from an analog input channel in an NI-DAQmx task. + """ + + def read_many_sample( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from a single analog + input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_analog_f64( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample(self, timeout=10): + """ + Reads a single floating-point sample from a single analog input + channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: + + Indicates a single floating-point sample from the task. + """ + return self._interpreter.read_analog_scalar_f64(self._handle, timeout) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveform( + self, + waveform: AnalogWaveform[numpy.float64], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> int: + """ + Reads one or more floating-point samples from a single analog + input channel into a waveform. + + This read method optionally accepts a preallocated waveform to hold + the samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated waveform is valuable in continuous + acquisition scenarios, where the same waveform can be used + repeatedly in each call to the method. + + Args: + waveform (AnalogWaveform): Specifies an AnalogWaveform object + to use for reading samples into. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The provided waveform does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide a larger ' + 'waveform or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + return self._interpreter.read_analog_waveform( + self._handle, + number_of_samples_per_channel, + timeout, + waveform, + self._in_stream.waveform_attribute_mode, + ) diff --git a/generated/nidaqmx/stream_readers/analog_unscaled_reader.py b/generated/nidaqmx/stream_readers/analog_unscaled_reader.py new file mode 100644 index 000000000..0e4e31640 --- /dev/null +++ b/generated/nidaqmx/stream_readers/analog_unscaled_reader.py @@ -0,0 +1,352 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogUnscaledReader(ChannelReaderBase): + """ + Reads unscaled samples from one or more analog input channels in an + NI-DAQmx task. + """ + + def read_int16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 16-bit integer samples from one or + more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 16-bit integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_i16( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_int32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 32-bit integer samples from one or + more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 32-bit integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_i32( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 16-bit unsigned integer samples from + one or more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 16-bit unsigned integer values to + hold the samples requested. The size of the array must + be large enough to hold all requested samples from all + channels in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_u16( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled unsigned 32-bit integer samples from + one or more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 32-bit unsigned integer values to + hold the samples requested. The size of the array must + be large enough to hold all requested samples from all + channels in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_u32( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read diff --git a/generated/nidaqmx/stream_readers/counter_reader.py b/generated/nidaqmx/stream_readers/counter_reader.py new file mode 100644 index 000000000..546ccdeed --- /dev/null +++ b/generated/nidaqmx/stream_readers/counter_reader.py @@ -0,0 +1,521 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.types import CtrFreq, CtrTick, CtrTime + +from ._channel_reader_base import ChannelReaderBase + + +class CounterReader(ChannelReaderBase): + """ + Reads samples from a counter input channel in an NI-DAQmx task. + """ + + def read_many_sample_double( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from a single counter + input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_counter_f64_ex( + self._handle,number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_pulse_frequency( + self, frequencies, duty_cycles, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of frequency from a + single counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + frequencies (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the frequency + portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + duty_cycles (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the duty + cycle portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + frequencies, number_of_samples_per_channel, False, True) + self._verify_array( + duty_cycles, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_freq( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) + + return samps_per_chan_read + + def read_many_sample_pulse_ticks( + self, high_ticks, low_ticks, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of ticks from a single + counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + high_ticks (numpy.ndarray): Specifies a preallocated 1D + NumPy array of 32-bit unsigned integer values to hold + the high ticks portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + low_ticks (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the low + ticks portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + high_ticks, number_of_samples_per_channel, False, True) + self._verify_array( + low_ticks, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_ticks( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) + + return samps_per_chan_read + + def read_many_sample_pulse_time( + self, high_times, low_times, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of time from a single + counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + high_times (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the high + time portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + low_times (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the low + time portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + high_times, number_of_samples_per_channel, False, True) + self._verify_array( + low_times, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_time( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) + + return samps_per_chan_read + + def read_many_sample_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from a single + counter input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_counter_u32_ex( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_double(self, timeout=10): + """ + Reads a single floating-point sample from a single counter input + channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: Indicates a single floating-point sample from the + task. + """ + return self._interpreter.read_counter_scalar_f64(self._handle, timeout) + + def read_one_sample_pulse_frequency(self, timeout=10): + """ + Reads a pulse sample in terms of frequency from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrFreq: + + Indicates a pulse sample in terms of frequency from the task. + """ + freq, duty_cycle = self._interpreter.read_ctr_freq_scalar(self._handle, timeout) + + return CtrFreq(freq, duty_cycle) + + def read_one_sample_pulse_ticks(self, timeout=10): + """ + Reads a pulse sample in terms of ticks from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrTick: + + Indicates a pulse sample in terms of ticks from the task. + """ + high_ticks, low_ticks = self._interpreter.read_ctr_ticks_scalar(self._handle, timeout) + + return CtrTick(high_ticks, low_ticks) + + def read_one_sample_pulse_time(self, timeout=10): + """ + Reads a pulse sample in terms of time from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrTime: + + Indicates a pulse sample in terms of time from the task. + """ + high_time, low_time = self._interpreter.read_ctr_time_scalar(self._handle, timeout) + + return CtrTime(high_time, low_time) + + def read_one_sample_uint32(self, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from a single + counter input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 32-bit unsigned integer sample from the + task. + """ + return self._interpreter.read_counter_scalar_u32(self._handle, timeout) diff --git a/generated/nidaqmx/stream_readers/digital_multi_channel_reader.py b/generated/nidaqmx/stream_readers/digital_multi_channel_reader.py new file mode 100644 index 000000000..92355ff71 --- /dev/null +++ b/generated/nidaqmx/stream_readers/digital_multi_channel_reader.py @@ -0,0 +1,568 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import DigitalWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class DigitalMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more digital input channels in an NI-DAQmx + task. + """ + + def read_many_sample_port_byte( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 8-bit unsigned integer samples from one or + more digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 16-bit unsigned integer samples from one or + more digital input channels in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u16( + self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from one or + more digital input channels in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u32( + self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_multi_line(self, data, timeout=10): + """ + Reads a single boolean sample from one or more digital input + channels in a task. The channels can contain multiple digital + lines. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of boolean values to hold the samples requested. + The size of the array must be large enough to hold all + requested samples from all channels in the task; + otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a line from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, True, True) + + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_one_line(self, data, timeout=10): + """ + Reads a single boolean sample from one or more digital input + channels in a task. The channel can contain only one digital + line. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of boolean values to hold the samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, True, False) + + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_byte(self, data, timeout=10): + """ + Reads a single 8-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u8( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_uint16(self, data, timeout=10): + """ + Reads a single 16-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u16( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_uint32(self, data, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u32( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveforms( + self, + waveforms: list[DigitalWaveform[numpy.uint8]], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> list[DigitalWaveform[numpy.uint8]]: + """ + Reads one or more samples from one or more digital + input channels into a list of waveforms. + + Args: + waveforms (list[DigitalWaveform]): Specifies an existing + list of DigitalWaveform objects to use for reading samples into. + The list must contain one waveform + for each channel in the task. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_channels = self._in_stream.num_chans + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if len(waveforms) != number_of_channels: + raise DaqError( + f'The number of waveforms provided ({len(waveforms)}) does not match ' + f'the number of channels in the task ({number_of_channels}). Please provide ' + 'one waveform for each channel.', + DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) + + for i, waveform in enumerate(waveforms): + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' + 'waveforms or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + waveforms = self._interpreter.read_digital_waveforms( + self._handle, + number_of_channels, + number_of_samples_per_channel, + self._in_stream.di_num_booleans_per_chan, + timeout, + waveforms, + self._in_stream.waveform_attribute_mode, + ) + + return waveforms diff --git a/generated/nidaqmx/stream_readers/digital_single_channel_reader.py b/generated/nidaqmx/stream_readers/digital_single_channel_reader.py new file mode 100644 index 000000000..ea1687767 --- /dev/null +++ b/generated/nidaqmx/stream_readers/digital_single_channel_reader.py @@ -0,0 +1,447 @@ +from __future__ import annotations + +import numpy + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nitypes.waveform import DigitalWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class DigitalSingleChannelReader(ChannelReaderBase): + """ + Reads samples from a digital input channel in an NI-DAQmx task. + """ + + def read_many_sample_port_byte( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 8-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 16-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u16( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u32( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_multi_line(self, data, timeout=10): + """ + Reads a single boolean sample from a single digital input + channel in a task. The channel can contain multiple digital + lines. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of boolean values to hold the samples requested. + + Each element in the array corresponds to a sample from + a line in the channel. The size of the array must be + large enough to hold all requested samples from the + channel in the task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, False, True) + + _, samps_per_chan_read, num_bytes_per_samp = self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_one_line(self, timeout=10): + """ + Reads a single boolean sample from a single digital input + channel in a task. The channel can contain only one digital + line. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + bool: + + Indicates a single boolean sample from the task. + """ + data = numpy.zeros(1, dtype=bool) + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return bool(data[0]) + + def read_one_sample_port_byte(self, timeout=10): + """ + Reads a single 8-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 8-bit unsigned integer sample from the + task. + """ + data = numpy.zeros(1, dtype=numpy.uint8) + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return int(data[0]) + + def read_one_sample_port_uint16(self, timeout=10): + """ + Reads a single 16-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 16-bit unsigned integer sample from the + task. + """ + data = numpy.zeros(1, dtype=numpy.uint16) + _, samps_per_read_chan = self._interpreter.read_digital_u16( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return int(data[0]) + + def read_one_sample_port_uint32(self, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 32-bit unsigned integer sample from the + task. + """ + return self._interpreter.read_digital_scalar_u32(self._handle, timeout) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveform( + self, + waveform: DigitalWaveform[numpy.uint8], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: float = 10.0, + ) -> int: + """ + Reads one or more digital samples from a single digital input + channel into a waveform. + + Args: + waveform (DigitalWaveform[numpy.uint8]): Specifies a + preallocated DigitalWaveform object to hold the samples + requested. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + return self._interpreter.read_digital_waveform( + self._handle, + number_of_samples_per_channel, + timeout, + waveform, + self._in_stream.waveform_attribute_mode + ) diff --git a/generated/nidaqmx/stream_readers/power_readers.py b/generated/nidaqmx/stream_readers/power_readers.py new file mode 100644 index 000000000..2a581f9cf --- /dev/null +++ b/generated/nidaqmx/stream_readers/power_readers.py @@ -0,0 +1,385 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.types import PowerMeasurement + +from ._channel_reader_base import ChannelReaderBase + + +class PowerSingleChannelReader(ChannelReaderBase): + """ + Reads samples from an analog input power channel in an NI-DAQmx task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point power samples from a single analog + input power channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the current samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, False, True) + self._verify_array(current_data, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_f64( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read + + def read_one_sample(self, timeout=10): + """ + Reads a single floating-point power sample from a single analog input + power channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: + + Indicates a single floating-point power sample from the task. + """ + voltage, current = self._interpreter.read_power_scalar_f64(self._handle, timeout) + return PowerMeasurement(voltage=voltage, current=current) + + +class PowerMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more analog input power channels in an NI-DAQmx + task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point power samples from one or more analog + input power channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the voltage samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + current_data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the current samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, True, True) + self._verify_array(current_data, number_of_samples_per_channel, True, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_f64( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read + + def read_one_sample(self, voltage_data, current_data, timeout=10): + """ + Reads a single floating-point power sample from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(voltage_data, 1, True, False) + self._verify_array(current_data, 1, True, False) + + self._interpreter.read_power_f64( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, + voltage_data, current_data) + + +class PowerBinaryReader(ChannelReaderBase): + """ + Reads binary samples from one or more analog input power channels in an + NI-DAQmx task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more binary int16 samples from one or more analog + input power channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of i16 values to hold the voltage samples requested. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of i16 values to hold the current samples requested. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, True, True) + self._verify_array(current_data, number_of_samples_per_channel, True, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_binary_i16( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read diff --git a/generated/nidaqmx/stream_writers.py b/generated/nidaqmx/stream_writers.py index 6e05a5a4c..e9073de43 100644 --- a/generated/nidaqmx/stream_writers.py +++ b/generated/nidaqmx/stream_writers.py @@ -1,1401 +1,9 @@ -import numpy +""" +NI-DAQmx stream writers. +""" -from nidaqmx.constants import FillMode -from nidaqmx import DaqError -from nidaqmx.error_codes import DAQmxErrors +# Import all classes from the subpackage to maintain backward compatibility +from .stream_writers import * # noqa: F403, F401 -__all__ = ['AnalogSingleChannelWriter', 'AnalogMultiChannelWriter', - 'AnalogUnscaledWriter', 'CounterWriter', - 'DigitalSingleChannelWriter', 'DigitalMultiChannelWriter'] - - -class UnsetAutoStartSentinel: - pass - - -AUTO_START_UNSET = UnsetAutoStartSentinel() - -del UnsetAutoStartSentinel - - -class ChannelWriterBase: - """ - Defines base class for all NI-DAQmx stream writers. - """ - - def __init__(self, task_out_stream, auto_start=AUTO_START_UNSET): - """ - Args: - task_out_stream: Specifies the output stream associated with - an NI-DAQmx task which to write samples. - auto_start (Optional[bool]): Specifies if the write method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - - If you do not specify a value for this parameter, - NI-DAQmx determines its value based on the type of write - method used. If you use a one sample write method, the - value is True; conversely, if you use a many sample - write method, the value is False. - """ - self._out_stream = task_out_stream - self._task = task_out_stream._task - self._handle = task_out_stream._task._handle - self._interpreter = task_out_stream._task._interpreter - - self._verify_array_shape = True - self._auto_start = auto_start - - @property - def auto_start(self): - """ - bool: Specifies if the write method automatically starts the - task if you did not explicitly start it with the DAQmx Start - Task method. - - If you do not specify a value for this parameter, NI-DAQmx - determines its value based on the type of write method used. - If you use a one sample write method, its value is True; - conversely, if you use a many sample write method, its value - is False. - """ - return self._auto_start - - @auto_start.setter - def auto_start(self, val): - self._auto_start = val - - @auto_start.deleter - def auto_start(self): - self._auto_start = AUTO_START_UNSET - - @property - def verify_array_shape(self): - """ - bool: Indicates whether the size and shape of the user-defined - NumPy arrays passed to read methods are verified. Defaults - to True when this object is instantiated. - - Setting this property to True may marginally adversely - impact the performance of read methods. - """ - return self._verify_array_shape - - @verify_array_shape.setter - def verify_array_shape(self, val): - self._verify_array_shape = val - - def _verify_array(self, data, is_many_chan, is_many_samp): - """ - Verifies that the shape of the specified NumPy array can be used - with the specified write method type, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the write method is a many - channel version. - is_many_samp (bool): Specifies if the write method is a many - sample version. - """ - if not self._verify_array_shape: - return - - channels_to_write = self._task.channels - number_of_channels = len(channels_to_write.channel_names) - - expected_num_dimensions = None - if is_many_chan: - if is_many_samp: - expected_num_dimensions = 2 - else: - expected_num_dimensions = 1 - - if data.shape[0] != number_of_channels: - self._task._raise_invalid_write_num_chans_error( - number_of_channels, data.shape[0]) - else: - if is_many_samp: - expected_num_dimensions = 1 - - if expected_num_dimensions is not None: - self._raise_error_if_invalid_write_dimensions( - expected_num_dimensions, len(data.shape)) - - def _verify_array_digital_lines( - self, data, is_many_chan, is_many_line): - """ - Verifies that the shape of the specified NumPy array can be used - to read samples from the current task which contains one or more - channels that have one or more digital lines per channel, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the write method is a - many channel version. - is_many_line (bool): Specifies if the write method is a - many line version. - """ - if not self._verify_array_shape: - return - - channels_to_write = self._task.channels - number_of_channels = len(channels_to_write.channel_names) - number_of_lines = self._out_stream.do_num_booleans_per_chan - - expected_num_dimensions = None - if is_many_chan: - if data.shape[0] != number_of_channels: - self._task._raise_invalid_write_num_chans_error( - number_of_channels, data.shape[0]) - - if is_many_line: - expected_num_dimensions = 2 - if data.shape[1] != number_of_lines: - self._task._raise_invalid_num_lines_error( - number_of_lines, data.shape[1]) - else: - expected_num_dimensions = 1 - else: - if is_many_line: - expected_num_dimensions = 1 - if data.shape[0] != number_of_lines: - self._task._raise_invalid_num_lines_error( - number_of_lines, data.shape[0]) - - if expected_num_dimensions is not None: - self._raise_error_if_invalid_write_dimensions( - expected_num_dimensions, len(data.shape)) - - def _raise_error_if_invalid_write_dimensions( - self, num_dimensions_expected, num_dimensions_in_data): - if num_dimensions_expected != num_dimensions_in_data: - raise DaqError( - 'Write cannot be performed because the NumPy array passed ' - 'into this function is not shaped correctly. ' - 'You must pass in a NumPy array of the correct number of ' - 'dimensions based on the write method you use.\n\n' - 'No. of dimensions of NumPy Array provided: {}\n' - 'No. of dimensions of NumPy Array required: {}' - .format(num_dimensions_in_data, num_dimensions_expected), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - -class AnalogSingleChannelWriter(ChannelWriterBase): - """ - Writes samples to an analog output channel in an NI-DAQmx task. - """ - - def write_many_sample(self, data, timeout=10.0): - """ - Writes one or more floating-point samples to a single analog - output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of - floating-point samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_analog_f64( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample(self, data, timeout=10): - """ - Writes a single floating-point sample to a single analog output - channel in a task. - - Args: - data (float): Specifies the floating-point sample to write - to the task. - auto_start (Optional[bool]): Specifies if this method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_analog_scalar_f64( - self._handle, auto_start, timeout, data) - - -class AnalogMultiChannelWriter(ChannelWriterBase): - """ - Writes samples to one or more analog output channels in an NI-DAQmx - task. - """ - - def write_many_sample(self, data, timeout=10.0): - """ - Writes one or more floating-point samples to one or more analog - output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of - floating-point samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_analog_f64( - self._handle, data.shape[1], auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample(self, data, timeout=10): - """ - Writes a single floating-point sample to one or more analog - output channels in a task. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of - floating-point samples to write to the task. - - Each element of the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_analog_f64( - self._handle, 1, auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) - - -class AnalogUnscaledWriter(ChannelWriterBase): - """ - Writes unscaled samples to one or more analog output channels in - an NI-DAQmx task. - """ - - def write_int16(self, data, timeout=10.0): - """ - Writes one or more unscaled 16-bit integer samples to one or - more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 16-bit integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_i16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_int32(self, data, timeout=10.0): - """ - Writes one or more unscaled 32-bit integer samples to one or - more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 32-bit integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_i32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_uint16(self, data, timeout=10.0): - """ - Writes one or more unscaled 16-bit unsigned integer samples to - one or more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 16-bit unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_u16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_uint32(self, data, timeout=10.0): - """ - Writes one or more unscaled 32-bit unsigned integer samples to - one or more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 32-bit unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_u32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - -class CounterWriter(ChannelWriterBase): - """ - Writes samples to a counter output channel in an NI-DAQmx task. - """ - - def write_many_sample_pulse_frequency( - self, frequencies, duty_cycles, timeout=10.0): - """ - Writes one or more pulse samples in terms of frequency to a - single counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - frequencies (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the frequency portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - duty_cycles (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the duty cycle portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(frequencies, False, True) - self._verify_array(duty_cycles, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_freq( - self._handle, frequencies.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) - - def write_many_sample_pulse_ticks( - self, high_ticks, low_ticks, timeout=10.0): - """ - Writes one or more pulse samples in terms of ticks to a single - counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - high_ticks (numpy.ndarray): Contains a 1D NumPy array of - 32-bit unsigned integer values that holds the high ticks - portion of the pulse samples to write to the task. Each - element of the array corresponds to a sample to write. - low_ticks (numpy.ndarray): Contains a 1D NumPy array of - 32-bit unsigned integer values that holds the low ticks - portion of the pulse samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(high_ticks, False, True) - self._verify_array(low_ticks, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_ticks( - self._handle, high_ticks.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) - - def write_many_sample_pulse_time( - self, high_times, low_times, timeout=10.0): - """ - Writes one or more pulse samples in terms of time to a single - counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - high_times (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the high time portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - low_times (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the low time portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(high_times, False, True) - self._verify_array(low_times, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_time( - self._handle, high_times.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) - - def write_one_sample_pulse_frequency( - self, frequency, duty_cycle, timeout=10): - """ - Writes a new pulse frequency and duty cycle to a single counter - output channel in a task. - - Args: - frequency (float): Specifies at what frequency to generate - pulses. - duty_cycle (float): Specifies the width of the pulse divided - by the pulse period. NI-DAQmx uses this ratio combined - with frequency to determine pulse width and the interval - between pulses. - auto_start (Optional[bool]): Specifies if this method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_freq_scalar( - self._handle, auto_start, timeout, frequency, duty_cycle) - - def write_one_sample_pulse_ticks( - self, high_ticks, low_ticks, timeout=10): - """ - Writes a new pulse high tick count and low tick count to a - single counter output channel in a task. - - Args: - high_ticks (float): Specifies the number of ticks the pulse - is high. - low_ticks (float): Specifies the number of ticks the pulse - is low. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_ticks_scalar( - self._handle, auto_start, timeout, high_ticks, low_ticks) - - def write_one_sample_pulse_time( - self, high_time, low_time, timeout=10): - """ - Writes a new pulse high time and low time to a single counter - output channel in a task. - - Args: - high_time (float): Specifies the amount of time the pulse - is high. - low_time (float): Specifies the amount of time the pulse - is low. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_time_scalar( - self._handle, auto_start, timeout, high_time, low_time) - - -class DigitalSingleChannelWriter(ChannelWriterBase): - """ - Writes samples to a single digital output channel in an NI-DAQmx - task. - """ - - def write_many_sample_port_byte(self, data, timeout=10.0): - """ - Writes one or more 8-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 8 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 8-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u8( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint16(self, data, timeout=10.0): - """ - Writes one or more 16-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 16 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 16-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u16( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint32(self, data, timeout=10.0): - """ - Writes one or more 32-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 32 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 32-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u32( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_multi_line(self, data, timeout=10): - """ - Writes a single boolean sample to a single digital output - channel in a task. The channel can contain multiple digital - lines. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of boolean - samples to write to the task. Each element of the array - corresponds to a line in the channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_one_line(self, data, timeout=10): - """ - Writes a single boolean sample to a single digital output - channel in a task. The channel can contain only one digital - line. - - Args: - data (int): Specifies the boolean sample to write to the - task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=bool) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_byte(self, data, timeout=10): - """ - Writes a single 8-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - data (int): Specifies the 8-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=numpy.uint8) - - return self._interpreter.write_digital_u8( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_uint16(self, data, timeout=10): - """ - Writes a single 16-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - data (int): Specifies the 16-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=numpy.uint16) - - return self._interpreter.write_digital_u16( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_uint32(self, data, timeout=10): - """ - Writes a single 32-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - data (int): Specifies the 32-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_scalar_u32( - self._handle, auto_start, timeout, data) - - -class DigitalMultiChannelWriter(ChannelWriterBase): - """ - Writes samples to one or more digital output channels in an NI-DAQmx - task. - """ - - def write_many_sample_port_byte(self, data, timeout=10.0): - """ - Writes one or more 8-bit unsigned integer samples to one or more - digital output channels in a task. - - Use this method for devices with up to 8 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 8-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u8( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint16(self, data, timeout=10.0): - """ - Writes one or more 16-bit unsigned integer samples to one or - more digital output channels in a task. - - Use this method for devices with up to 16 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 16-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint32(self, data, timeout=10.0): - """ - Writes one or more 32-bit unsigned integer samples to one or - more digital output channels in a task. - - Use this method for devices with up to 32 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 32-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_multi_line(self, data, timeout=10): - """ - Writes a single boolean sample to one or more digital output - channels in a task. The channel can contain multiple digital - lines. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of boolean - samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a line from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_one_line(self, data, timeout=10): - """ - Writes a single boolean sample to one or more digital output - channels in a task. The channel can contain only one digital - line. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of boolean - samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_byte(self, data, timeout=10): - """ - Writes a single 8-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 8-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u8( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_uint16(self, data, timeout=10): - """ - Writes a single 16-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 16-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u16( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_uint32(self, data, timeout=10): - """ - Writes a single 32-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 32-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u32( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) +# Re-export the __all__ list from the subpackage +from .stream_writers import __all__ \ No newline at end of file diff --git a/generated/nidaqmx/stream_writers/__init__.py b/generated/nidaqmx/stream_writers/__init__.py new file mode 100644 index 000000000..37aabed61 --- /dev/null +++ b/generated/nidaqmx/stream_writers/__init__.py @@ -0,0 +1,22 @@ +""" +NI-DAQmx stream writers. + +This package provides classes for writing samples to NI-DAQmx tasks. +""" + +from __future__ import annotations + +from .analog_single_channel_writer import AnalogSingleChannelWriter +from .analog_multi_channel_writer import AnalogMultiChannelWriter +from .analog_unscaled_writer import AnalogUnscaledWriter +from .counter_writer import CounterWriter +from .digital_single_channel_writer import DigitalSingleChannelWriter +from .digital_multi_channel_writer import DigitalMultiChannelWriter + +__all__ = [ + 'AnalogSingleChannelWriter', + 'AnalogMultiChannelWriter', + 'AnalogUnscaledWriter', + 'CounterWriter', + 'DigitalSingleChannelWriter', + 'DigitalMultiChannelWriter'] diff --git a/generated/nidaqmx/stream_writers/_channel_writer_base.py b/generated/nidaqmx/stream_writers/_channel_writer_base.py new file mode 100644 index 000000000..c0d02a59a --- /dev/null +++ b/generated/nidaqmx/stream_writers/_channel_writer_base.py @@ -0,0 +1,175 @@ +from nidaqmx import DaqError +from nidaqmx.error_codes import DAQmxErrors + + +class UnsetAutoStartSentinel: + pass + + +AUTO_START_UNSET = UnsetAutoStartSentinel() + +del UnsetAutoStartSentinel + + +class ChannelWriterBase: + """ + Defines base class for all NI-DAQmx stream writers. + """ + + def __init__(self, task_out_stream, auto_start=AUTO_START_UNSET): + """ + Args: + task_out_stream: Specifies the output stream associated with + an NI-DAQmx task which to write samples. + auto_start (Optional[bool]): Specifies if the write method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + + If you do not specify a value for this parameter, + NI-DAQmx determines its value based on the type of write + method used. If you use a one sample write method, the + value is True; conversely, if you use a many sample + write method, the value is False. + """ + self._out_stream = task_out_stream + self._task = task_out_stream._task + self._handle = task_out_stream._task._handle + self._interpreter = task_out_stream._task._interpreter + + self._verify_array_shape = True + self._auto_start = auto_start + + @property + def auto_start(self): + """ + bool: Specifies if the write method automatically starts the + task if you did not explicitly start it with the DAQmx Start + Task method. + + If you do not specify a value for this parameter, NI-DAQmx + determines its value based on the type of write method used. + If you use a one sample write method, its value is True; + conversely, if you use a many sample write method, its value + is False. + """ + return self._auto_start + + @auto_start.setter + def auto_start(self, val): + self._auto_start = val + + @auto_start.deleter + def auto_start(self): + self._auto_start = AUTO_START_UNSET + + @property + def verify_array_shape(self): + """ + bool: Indicates whether the size and shape of the user-defined + NumPy arrays passed to read methods are verified. Defaults + to True when this object is instantiated. + + Setting this property to True may marginally adversely + impact the performance of read methods. + """ + return self._verify_array_shape + + @verify_array_shape.setter + def verify_array_shape(self, val): + self._verify_array_shape = val + + def _verify_array(self, data, is_many_chan, is_many_samp): + """ + Verifies that the shape of the specified NumPy array can be used + with the specified write method type, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the write method is a many + channel version. + is_many_samp (bool): Specifies if the write method is a many + sample version. + """ + if not self._verify_array_shape: + return + + channels_to_write = self._task.channels + number_of_channels = len(channels_to_write.channel_names) + + expected_num_dimensions = None + if is_many_chan: + if is_many_samp: + expected_num_dimensions = 2 + else: + expected_num_dimensions = 1 + + if data.shape[0] != number_of_channels: + self._task._raise_invalid_write_num_chans_error( + number_of_channels, data.shape[0]) + else: + if is_many_samp: + expected_num_dimensions = 1 + + if expected_num_dimensions is not None: + self._raise_error_if_invalid_write_dimensions( + expected_num_dimensions, len(data.shape)) + + def _verify_array_digital_lines( + self, data, is_many_chan, is_many_line): + """ + Verifies that the shape of the specified NumPy array can be used + to read samples from the current task which contains one or more + channels that have one or more digital lines per channel, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the write method is a + many channel version. + is_many_line (bool): Specifies if the write method is a + many line version. + """ + if not self._verify_array_shape: + return + + channels_to_write = self._task.channels + number_of_channels = len(channels_to_write.channel_names) + number_of_lines = self._out_stream.do_num_booleans_per_chan + + expected_num_dimensions = None + if is_many_chan: + if data.shape[0] != number_of_channels: + self._task._raise_invalid_write_num_chans_error( + number_of_channels, data.shape[0]) + + if is_many_line: + expected_num_dimensions = 2 + if data.shape[1] != number_of_lines: + self._task._raise_invalid_num_lines_error( + number_of_lines, data.shape[1]) + else: + expected_num_dimensions = 1 + else: + if is_many_line: + expected_num_dimensions = 1 + if data.shape[0] != number_of_lines: + self._task._raise_invalid_num_lines_error( + number_of_lines, data.shape[0]) + + if expected_num_dimensions is not None: + self._raise_error_if_invalid_write_dimensions( + expected_num_dimensions, len(data.shape)) + + def _raise_error_if_invalid_write_dimensions( + self, num_dimensions_expected, num_dimensions_in_data): + if num_dimensions_expected != num_dimensions_in_data: + raise DaqError( + 'Write cannot be performed because the NumPy array passed ' + 'into this function is not shaped correctly. ' + 'You must pass in a NumPy array of the correct number of ' + 'dimensions based on the write method you use.\n\n' + 'No. of dimensions of NumPy Array provided: {}\n' + 'No. of dimensions of NumPy Array required: {}' + .format(num_dimensions_in_data, num_dimensions_expected), + DAQmxErrors.UNKNOWN, task_name=self._task.name) diff --git a/generated/nidaqmx/stream_writers/analog_multi_channel_writer.py b/generated/nidaqmx/stream_writers/analog_multi_channel_writer.py new file mode 100644 index 000000000..c314d1a26 --- /dev/null +++ b/generated/nidaqmx/stream_writers/analog_multi_channel_writer.py @@ -0,0 +1,90 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogMultiChannelWriter(ChannelWriterBase): + """ + Writes samples to one or more analog output channels in an NI-DAQmx + task. + """ + + def write_many_sample(self, data, timeout=10.0): + """ + Writes one or more floating-point samples to one or more analog + output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of + floating-point samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_analog_f64( + self._handle, data.shape[1], auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample(self, data, timeout=10): + """ + Writes a single floating-point sample to one or more analog + output channels in a task. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of + floating-point samples to write to the task. + + Each element of the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_analog_f64( + self._handle, 1, auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/generated/nidaqmx/stream_writers/analog_single_channel_writer.py b/generated/nidaqmx/stream_writers/analog_single_channel_writer.py new file mode 100644 index 000000000..e5334140d --- /dev/null +++ b/generated/nidaqmx/stream_writers/analog_single_channel_writer.py @@ -0,0 +1,82 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogSingleChannelWriter(ChannelWriterBase): + """ + Writes samples to an analog output channel in an NI-DAQmx task. + """ + + def write_many_sample(self, data, timeout=10.0): + """ + Writes one or more floating-point samples to a single analog + output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of + floating-point samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_analog_f64( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample(self, data, timeout=10): + """ + Writes a single floating-point sample to a single analog output + channel in a task. + + Args: + data (float): Specifies the floating-point sample to write + to the task. + auto_start (Optional[bool]): Specifies if this method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_analog_scalar_f64( + self._handle, auto_start, timeout, data) + diff --git a/generated/nidaqmx/stream_writers/analog_unscaled_writer.py b/generated/nidaqmx/stream_writers/analog_unscaled_writer.py new file mode 100644 index 000000000..197fe297f --- /dev/null +++ b/generated/nidaqmx/stream_writers/analog_unscaled_writer.py @@ -0,0 +1,190 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogUnscaledWriter(ChannelWriterBase): + """ + Writes unscaled samples to one or more analog output channels in + an NI-DAQmx task. + """ + + def write_int16(self, data, timeout=10.0): + """ + Writes one or more unscaled 16-bit integer samples to one or + more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 16-bit integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_i16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_int32(self, data, timeout=10.0): + """ + Writes one or more unscaled 32-bit integer samples to one or + more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 32-bit integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_i32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_uint16(self, data, timeout=10.0): + """ + Writes one or more unscaled 16-bit unsigned integer samples to + one or more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 16-bit unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_u16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_uint32(self, data, timeout=10.0): + """ + Writes one or more unscaled 32-bit unsigned integer samples to + one or more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 32-bit unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_u32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/generated/nidaqmx/stream_writers/counter_writer.py b/generated/nidaqmx/stream_writers/counter_writer.py new file mode 100644 index 000000000..2c75a3720 --- /dev/null +++ b/generated/nidaqmx/stream_writers/counter_writer.py @@ -0,0 +1,254 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class CounterWriter(ChannelWriterBase): + """ + Writes samples to a counter output channel in an NI-DAQmx task. + """ + + def write_many_sample_pulse_frequency( + self, frequencies, duty_cycles, timeout=10.0): + """ + Writes one or more pulse samples in terms of frequency to a + single counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + frequencies (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the frequency portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + duty_cycles (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the duty cycle portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(frequencies, False, True) + self._verify_array(duty_cycles, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_freq( + self._handle, frequencies.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) + + def write_many_sample_pulse_ticks( + self, high_ticks, low_ticks, timeout=10.0): + """ + Writes one or more pulse samples in terms of ticks to a single + counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + high_ticks (numpy.ndarray): Contains a 1D NumPy array of + 32-bit unsigned integer values that holds the high ticks + portion of the pulse samples to write to the task. Each + element of the array corresponds to a sample to write. + low_ticks (numpy.ndarray): Contains a 1D NumPy array of + 32-bit unsigned integer values that holds the low ticks + portion of the pulse samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(high_ticks, False, True) + self._verify_array(low_ticks, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_ticks( + self._handle, high_ticks.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) + + def write_many_sample_pulse_time( + self, high_times, low_times, timeout=10.0): + """ + Writes one or more pulse samples in terms of time to a single + counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + high_times (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the high time portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + low_times (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the low time portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(high_times, False, True) + self._verify_array(low_times, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_time( + self._handle, high_times.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) + + def write_one_sample_pulse_frequency( + self, frequency, duty_cycle, timeout=10): + """ + Writes a new pulse frequency and duty cycle to a single counter + output channel in a task. + + Args: + frequency (float): Specifies at what frequency to generate + pulses. + duty_cycle (float): Specifies the width of the pulse divided + by the pulse period. NI-DAQmx uses this ratio combined + with frequency to determine pulse width and the interval + between pulses. + auto_start (Optional[bool]): Specifies if this method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_freq_scalar( + self._handle, auto_start, timeout, frequency, duty_cycle) + + def write_one_sample_pulse_ticks( + self, high_ticks, low_ticks, timeout=10): + """ + Writes a new pulse high tick count and low tick count to a + single counter output channel in a task. + + Args: + high_ticks (float): Specifies the number of ticks the pulse + is high. + low_ticks (float): Specifies the number of ticks the pulse + is low. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_ticks_scalar( + self._handle, auto_start, timeout, high_ticks, low_ticks) + + def write_one_sample_pulse_time( + self, high_time, low_time, timeout=10): + """ + Writes a new pulse high time and low time to a single counter + output channel in a task. + + Args: + high_time (float): Specifies the amount of time the pulse + is high. + low_time (float): Specifies the amount of time the pulse + is low. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_time_scalar( + self._handle, auto_start, timeout, high_time, low_time) diff --git a/generated/nidaqmx/stream_writers/digital_multi_channel_writer.py b/generated/nidaqmx/stream_writers/digital_multi_channel_writer.py new file mode 100644 index 000000000..15224fd0d --- /dev/null +++ b/generated/nidaqmx/stream_writers/digital_multi_channel_writer.py @@ -0,0 +1,330 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class DigitalMultiChannelWriter(ChannelWriterBase): + """ + Writes samples to one or more digital output channels in an NI-DAQmx + task. + """ + + def write_many_sample_port_byte(self, data, timeout=10.0): + """ + Writes one or more 8-bit unsigned integer samples to one or more + digital output channels in a task. + + Use this method for devices with up to 8 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 8-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u8( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint16(self, data, timeout=10.0): + """ + Writes one or more 16-bit unsigned integer samples to one or + more digital output channels in a task. + + Use this method for devices with up to 16 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 16-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint32(self, data, timeout=10.0): + """ + Writes one or more 32-bit unsigned integer samples to one or + more digital output channels in a task. + + Use this method for devices with up to 32 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 32-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_multi_line(self, data, timeout=10): + """ + Writes a single boolean sample to one or more digital output + channels in a task. The channel can contain multiple digital + lines. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of boolean + samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a line from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_one_line(self, data, timeout=10): + """ + Writes a single boolean sample to one or more digital output + channels in a task. The channel can contain only one digital + line. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of boolean + samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_byte(self, data, timeout=10): + """ + Writes a single 8-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 8-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u8( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_uint16(self, data, timeout=10): + """ + Writes a single 16-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 16-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u16( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_uint32(self, data, timeout=10): + """ + Writes a single 32-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 32-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u32( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/generated/nidaqmx/stream_writers/digital_single_channel_writer.py b/generated/nidaqmx/stream_writers/digital_single_channel_writer.py new file mode 100644 index 000000000..0edbacdb9 --- /dev/null +++ b/generated/nidaqmx/stream_writers/digital_single_channel_writer.py @@ -0,0 +1,294 @@ +import numpy + +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class DigitalSingleChannelWriter(ChannelWriterBase): + """ + Writes samples to a single digital output channel in an NI-DAQmx + task. + """ + + def write_many_sample_port_byte(self, data, timeout=10.0): + """ + Writes one or more 8-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 8 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 8-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u8( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint16(self, data, timeout=10.0): + """ + Writes one or more 16-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 16 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 16-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u16( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint32(self, data, timeout=10.0): + """ + Writes one or more 32-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 32 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 32-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u32( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_multi_line(self, data, timeout=10): + """ + Writes a single boolean sample to a single digital output + channel in a task. The channel can contain multiple digital + lines. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of boolean + samples to write to the task. Each element of the array + corresponds to a line in the channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_one_line(self, data, timeout=10): + """ + Writes a single boolean sample to a single digital output + channel in a task. The channel can contain only one digital + line. + + Args: + data (int): Specifies the boolean sample to write to the + task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=bool) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_byte(self, data, timeout=10): + """ + Writes a single 8-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + data (int): Specifies the 8-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=numpy.uint8) + + return self._interpreter.write_digital_u8( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_uint16(self, data, timeout=10): + """ + Writes a single 16-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + data (int): Specifies the 16-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=numpy.uint16) + + return self._interpreter.write_digital_u16( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_uint32(self, data, timeout=10): + """ + Writes a single 32-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + data (int): Specifies the 32-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_scalar_u32( + self._handle, auto_start, timeout, data) diff --git a/src/handwritten/stream_readers.py b/src/handwritten/stream_readers.py index 55b6e2fed..82e456d63 100644 --- a/src/handwritten/stream_readers.py +++ b/src/handwritten/stream_readers.py @@ -1,2768 +1,9 @@ -from __future__ import annotations +""" +NI-DAQmx stream readers. +""" -import numpy -from nidaqmx import DaqError +# Import all classes from the subpackage to maintain backward compatibility +from .stream_readers import * # noqa: F403, F401 -from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature -from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE -from nidaqmx.error_codes import DAQmxErrors -from nidaqmx.types import PowerMeasurement, CtrFreq, CtrTick, CtrTime -from nitypes.waveform import AnalogWaveform, DigitalWaveform - -__all__ = ['AnalogSingleChannelReader', 'AnalogMultiChannelReader', - 'AnalogUnscaledReader', 'CounterReader', - 'DigitalSingleChannelReader', 'DigitalMultiChannelReader', - 'PowerSingleChannelReader', 'PowerMultiChannelReader', 'PowerBinaryReader'] - - -class ChannelReaderBase: - """ - Defines base class for all NI-DAQmx stream readers. - """ - - def __init__(self, task_in_stream): - """ - Args: - task_in_stream: Specifies the input stream associated with - an NI-DAQmx task from which to read samples. - """ - self._in_stream = task_in_stream - self._task = task_in_stream._task - self._handle = task_in_stream._task._handle - self._interpreter = task_in_stream._task._interpreter - - self._verify_array_shape = True - - @property - def verify_array_shape(self): - """ - bool: Indicates whether the size and shape of the user-defined - NumPy arrays passed to read methods are verified. Defaults - to True when this object is instantiated. - - Setting this property to True may marginally adversely - impact the performance of read methods. - """ - return self._verify_array_shape - - @verify_array_shape.setter - def verify_array_shape(self, val): - self._verify_array_shape = val - - def _verify_array(self, data, number_of_samples_per_channel, - is_many_chan, is_many_samp): - """ - Verifies that the shape of the specified NumPy array can be used - to read multiple samples from the current task which contains - one or more channels, if the "verify_array_shape" property is - set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - number_of_samples_per_channel (int): Specifies the number of - samples per channel requested. - is_many_chan (bool): Specifies if the read method is a many - channel version. - is_many_samp (bool): Specifies if the read method is a many - samples version. - """ - if not self._verify_array_shape: - return - - channels_to_read = self._in_stream.channels_to_read - number_of_channels = len(channels_to_read.channel_names) - - array_shape: tuple[int, ...] | None = None - if is_many_chan: - if is_many_samp: - array_shape = (number_of_channels, - number_of_samples_per_channel) - else: - array_shape = (number_of_channels,) - else: - if is_many_samp: - array_shape = (number_of_samples_per_channel,) - - if array_shape is not None and data.shape != array_shape: - raise DaqError( - 'Read cannot be performed because the NumPy array passed into ' - 'this function is not shaped correctly. You must pass in a ' - 'NumPy array of the correct shape based on the number of ' - 'channels in task and the number of samples per channel ' - 'requested.\n\n' - 'Shape of NumPy Array provided: {}\n' - 'Shape of NumPy Array required: {}' - .format(data.shape, array_shape), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - def _verify_array_digital_lines( - self, data, is_many_chan, is_many_line): - """ - Verifies that the shape of the specified NumPy array can be used - to read samples from the current task which contains one or more - channels that have one or more digital lines per channel, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the read method is a - many channel version. - is_many_line (bool): Specifies if the read method is a - many line version. - """ - if not self._verify_array_shape: - return - - channels_to_read = self._in_stream.channels_to_read - number_of_channels = len(channels_to_read.channel_names) - number_of_lines = self._in_stream.di_num_booleans_per_chan - - array_shape: tuple[int, ...] | None = None - if is_many_chan: - if is_many_line: - array_shape = (number_of_channels, number_of_lines) - else: - array_shape = (number_of_channels,) - else: - if is_many_line: - array_shape = (number_of_lines,) - - if array_shape is not None and data.shape != array_shape: - raise DaqError( - 'Read cannot be performed because the NumPy array passed into ' - 'this function is not shaped correctly. You must pass in a ' - 'NumPy array of the correct shape based on the number of ' - 'channels in task and the number of digital lines per ' - 'channel.\n\n' - 'Shape of NumPy Array provided: {}\n' - 'Shape of NumPy Array required: {}' - .format(data.shape, array_shape), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - -class AnalogSingleChannelReader(ChannelReaderBase): - """ - Reads samples from an analog input channel in an NI-DAQmx task. - """ - - def read_many_sample( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from a single analog - input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_analog_f64( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample(self, timeout=10): - """ - Reads a single floating-point sample from a single analog input - channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: - - Indicates a single floating-point sample from the task. - """ - return self._interpreter.read_analog_scalar_f64(self._handle, timeout) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveform( - self, - waveform: AnalogWaveform[numpy.float64], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> int: - """ - Reads one or more floating-point samples from a single analog - input channel into a waveform. - - This read method optionally accepts a preallocated waveform to hold - the samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated waveform is valuable in continuous - acquisition scenarios, where the same waveform can be used - repeatedly in each call to the method. - - Args: - waveform (AnalogWaveform): Specifies an AnalogWaveform object - to use for reading samples into. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The provided waveform does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide a larger ' - 'waveform or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - return self._interpreter.read_analog_waveform( - self._handle, - number_of_samples_per_channel, - timeout, - waveform, - self._in_stream.waveform_attribute_mode, - ) - - -class AnalogMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more analog input channels in an NI-DAQmx - task. - """ - - def read_many_sample( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_analog_f64( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample(self, data, timeout=10): - """ - Reads a single floating-point sample from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_analog_f64(self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveforms( - self, - waveforms: list[AnalogWaveform[numpy.float64]], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> int: - """ - Reads one or more floating-point samples from one or more analog - input channels into a list of waveforms. - - This read method optionally accepts a preallocated list of waveforms to hold - the samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated list of waveforms is valuable in continuous - acquisition scenarios, where the same waveforms can be used - repeatedly in each call to the method. - - Args: - waveforms (list[AnalogWaveform]): Specifies a list of AnalogWaveform - objects to use for reading samples into. - The list must contain one waveform for each channel in the task. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_channels = self._in_stream.num_chans - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if len(waveforms) != number_of_channels: - raise DaqError( - f'The number of waveforms provided ({len(waveforms)}) does not match ' - f'the number of channels in the task ({number_of_channels}). Please provide ' - 'one waveform for each channel.', - DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) - - for i, waveform in enumerate(waveforms): - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' - 'waveforms or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - return self._interpreter.read_analog_waveforms( - self._handle, - number_of_samples_per_channel, - timeout, - waveforms, - self._in_stream.waveform_attribute_mode, - ) - - -class AnalogUnscaledReader(ChannelReaderBase): - """ - Reads unscaled samples from one or more analog input channels in an - NI-DAQmx task. - """ - - def read_int16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 16-bit integer samples from one or - more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 16-bit integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_i16( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_int32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 32-bit integer samples from one or - more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 32-bit integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_i32( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled 16-bit unsigned integer samples from - one or more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 16-bit unsigned integer values to - hold the samples requested. The size of the array must - be large enough to hold all requested samples from all - channels in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_u16( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more unscaled unsigned 32-bit integer samples from - one or more analog input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of unscaled 32-bit unsigned integer values to - hold the samples requested. The size of the array must - be large enough to hold all requested samples from all - channels in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_binary_u32( - self._handle, number_of_samples_per_channel, - timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - -class PowerSingleChannelReader(ChannelReaderBase): - """ - Reads samples from an analog input power channel in an NI-DAQmx task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point power samples from a single analog - input power channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the current samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, False, True) - self._verify_array(current_data, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_f64( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - def read_one_sample(self, timeout=10): - """ - Reads a single floating-point power sample from a single analog input - power channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: - - Indicates a single floating-point power sample from the task. - """ - voltage, current = self._interpreter.read_power_scalar_f64(self._handle, timeout) - return PowerMeasurement(voltage=voltage, current=current) - - -class PowerMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more analog input power channels in an NI-DAQmx - task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point power samples from one or more analog - input power channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the voltage samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - current_data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of floating-point values to hold the current samples - requested. The size of the array must be large enough to - hold all requested samples from all channels in the - task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, True, True) - self._verify_array(current_data, number_of_samples_per_channel, True, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_f64( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - def read_one_sample(self, voltage_data, current_data, timeout=10): - """ - Reads a single floating-point power sample from one or more analog - input channels in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the voltage samples - requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(voltage_data, 1, True, False) - self._verify_array(current_data, 1, True, False) - - self._interpreter.read_power_f64( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, - voltage_data, current_data) - - -class PowerBinaryReader(ChannelReaderBase): - """ - Reads binary samples from one or more analog input power channels in an - NI-DAQmx task. - """ - - def read_many_sample( - self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more binary int16 samples from one or more analog - input power channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of i16 values to hold the voltage samples requested. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - current_data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of i16 values to hold the current samples requested. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property to True may marginally adversely - impact the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(voltage_data, number_of_samples_per_channel, True, True) - self._verify_array(current_data, number_of_samples_per_channel, True, True) - - _, _, samps_per_chan_read = self._interpreter.read_power_binary_i16( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) - - return samps_per_chan_read - - -class CounterReader(ChannelReaderBase): - """ - Reads samples from a counter input channel in an NI-DAQmx task. - """ - - def read_many_sample_double( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more floating-point samples from a single counter - input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of floating-point values to hold the samples - requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_counter_f64_ex( - self._handle,number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_pulse_frequency( - self, frequencies, duty_cycles, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of frequency from a - single counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - frequencies (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the frequency - portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - duty_cycles (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the duty - cycle portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - frequencies, number_of_samples_per_channel, False, True) - self._verify_array( - duty_cycles, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_freq( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) - - return samps_per_chan_read - - def read_many_sample_pulse_ticks( - self, high_ticks, low_ticks, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of ticks from a single - counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - high_ticks (numpy.ndarray): Specifies a preallocated 1D - NumPy array of 32-bit unsigned integer values to hold - the high ticks portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - low_ticks (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the low - ticks portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - high_ticks, number_of_samples_per_channel, False, True) - self._verify_array( - low_ticks, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_ticks( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) - - return samps_per_chan_read - - def read_many_sample_pulse_time( - self, high_times, low_times, - number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): - """ - Reads one or more pulse samples in terms of time from a single - counter input channel in a task. - - This read method accepts preallocated NumPy arrays to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in preallocated arrays is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - high_times (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the high - time portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - low_times (numpy.ndarray): Specifies a preallocated 1D - NumPy array of floating-point values to hold the low - time portion of the pulse samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array( - high_times, number_of_samples_per_channel, False, True) - self._verify_array( - low_times, number_of_samples_per_channel, False, True) - - _, _, samps_per_chan_read = self._interpreter.read_ctr_time( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) - - return samps_per_chan_read - - def read_many_sample_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from a single - counter input channel in a task. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_counter_u32_ex( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_double(self, timeout=10): - """ - Reads a single floating-point sample from a single counter input - channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - float: Indicates a single floating-point sample from the - task. - """ - return self._interpreter.read_counter_scalar_f64(self._handle, timeout) - - def read_one_sample_pulse_frequency(self, timeout=10): - """ - Reads a pulse sample in terms of frequency from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrFreq: - - Indicates a pulse sample in terms of frequency from the task. - """ - freq, duty_cycle = self._interpreter.read_ctr_freq_scalar(self._handle, timeout) - - return CtrFreq(freq, duty_cycle) - - def read_one_sample_pulse_ticks(self, timeout=10): - """ - Reads a pulse sample in terms of ticks from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrTick: - - Indicates a pulse sample in terms of ticks from the task. - """ - high_ticks, low_ticks = self._interpreter.read_ctr_ticks_scalar(self._handle, timeout) - - return CtrTick(high_ticks, low_ticks) - - def read_one_sample_pulse_time(self, timeout=10): - """ - Reads a pulse sample in terms of time from a single counter - input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - nidaqmx.types.CtrTime: - - Indicates a pulse sample in terms of time from the task. - """ - high_time, low_time = self._interpreter.read_ctr_time_scalar(self._handle, timeout) - - return CtrTime(high_time, low_time) - - def read_one_sample_uint32(self, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from a single - counter input channel in a task. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 32-bit unsigned integer sample from the - task. - """ - return self._interpreter.read_counter_scalar_u32(self._handle, timeout) - - -class DigitalSingleChannelReader(ChannelReaderBase): - """ - Reads samples from a digital input channel in an NI-DAQmx task. - """ - - def read_many_sample_port_byte( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 8-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 16-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u16( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from a single - digital input channel in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold - the samples requested, which can be advantageous for performance - and interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - the channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, False, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u32( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_multi_line(self, data, timeout=10): - """ - Reads a single boolean sample from a single digital input - channel in a task. The channel can contain multiple digital - lines. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of boolean values to hold the samples requested. - - Each element in the array corresponds to a sample from - a line in the channel. The size of the array must be - large enough to hold all requested samples from the - channel in the task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, False, True) - - _, samps_per_chan_read, num_bytes_per_samp = self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_one_line(self, timeout=10): - """ - Reads a single boolean sample from a single digital input - channel in a task. The channel can contain only one digital - line. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - bool: - - Indicates a single boolean sample from the task. - """ - data = numpy.zeros(1, dtype=bool) - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return bool(data[0]) - - def read_one_sample_port_byte(self, timeout=10): - """ - Reads a single 8-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 8-bit unsigned integer sample from the - task. - """ - data = numpy.zeros(1, dtype=numpy.uint8) - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return int(data[0]) - - def read_one_sample_port_uint16(self, timeout=10): - """ - Reads a single 16-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 16-bit unsigned integer sample from the - task. - """ - data = numpy.zeros(1, dtype=numpy.uint16) - _, samps_per_read_chan = self._interpreter.read_digital_u16( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return int(data[0]) - - def read_one_sample_port_uint32(self, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from a single - digital input channel in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates a single 32-bit unsigned integer sample from the - task. - """ - return self._interpreter.read_digital_scalar_u32(self._handle, timeout) - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveform( - self, - waveform: DigitalWaveform[numpy.uint8], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: float = 10.0, - ) -> int: - """ - Reads one or more digital samples from a single digital input - channel into a waveform. - - Args: - waveform (DigitalWaveform[numpy.uint8]): Specifies a - preallocated DigitalWaveform object to hold the samples - requested. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - return self._interpreter.read_digital_waveform( - self._handle, - number_of_samples_per_channel, - timeout, - waveform, - self._in_stream.waveform_attribute_mode - ) - - -class DigitalMultiChannelReader(ChannelReaderBase): - """ - Reads samples from one or more digital input channels in an NI-DAQmx - task. - """ - - def read_many_sample_port_byte( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 8-bit unsigned integer samples from one or - more digital input channel in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u8( - self._handle, number_of_samples_per_channel, timeout, - FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint16( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 16-bit unsigned integer samples from one or - more digital input channels in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u16( - self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_many_sample_port_uint32( - self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, - timeout=10.0): - """ - Reads one or more 32-bit unsigned integer samples from one or - more digital input channels in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. The size of the array must be large - enough to hold all requested samples from all channels - in the task; otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - self._verify_array(data, number_of_samples_per_channel, True, True) - - _, samps_per_chan_read = self._interpreter.read_digital_u32( - self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - return samps_per_chan_read - - def read_one_sample_multi_line(self, data, timeout=10): - """ - Reads a single boolean sample from one or more digital input - channels in a task. The channels can contain multiple digital - lines. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 2D NumPy - array of boolean values to hold the samples requested. - The size of the array must be large enough to hold all - requested samples from all channels in the task; - otherwise, an error is thrown. - - Each row corresponds to a channel in the task. Each - column corresponds to a line from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task or to - the order of the channels you specify with the - "channels_to_read" property. - - If the size of the array is too large or the array is - shaped incorrectly, the previous statement may not hold - true as the samples read may not be separated into rows - and columns properly. Set the "verify_array_shape" - property on this channel reader object to True to - validate that the NumPy array object is shaped properly. - Setting this property may marginally adversely impact - the performance of the method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, True, True) - - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_one_line(self, data, timeout=10): - """ - Reads a single boolean sample from one or more digital input - channels in a task. The channel can contain only one digital - line. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of boolean values to hold the samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array_digital_lines(data, True, False) - - _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_byte(self, data, timeout=10): - """ - Reads a single 8-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 8 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 8-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u8( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_uint16(self, data, timeout=10): - """ - Reads a single 16-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 16 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 16-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u16( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def read_one_sample_port_uint32(self, data, timeout=10): - """ - Reads a single 32-bit unsigned integer sample from one or more - digital input channels in a task. - - Use this method for devices with up to 32 lines per port. - - This read method accepts a preallocated NumPy array to hold the - samples requested, which can be advantageous for performance and - interoperability with NumPy and SciPy. - - Passing in a preallocated array is valuable in continuous - acquisition scenarios, where the same array can be used - repeatedly in each call to the method. - - Args: - data (numpy.ndarray): Specifies a preallocated 1D NumPy - array of 32-bit unsigned integer values to hold the - samples requested. - - Each element in the array corresponds to a sample from - each channel. The size of the array must be large enough - to hold all requested samples from the channel in the - task; otherwise, an error is thrown. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - """ - self._verify_array(data, 1, True, False) - - self._interpreter.read_digital_u32( - self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - - @requires_feature(WAVEFORM_SUPPORT) - def read_waveforms( - self, - waveforms: list[DigitalWaveform[numpy.uint8]], - number_of_samples_per_channel: int = READ_ALL_AVAILABLE, - timeout: int = 10, - ) -> list[DigitalWaveform[numpy.uint8]]: - """ - Reads one or more samples from one or more digital - input channels into a list of waveforms. - - Args: - waveforms (list[DigitalWaveform]): Specifies an existing - list of DigitalWaveform objects to use for reading samples into. - The list must contain one waveform - for each channel in the task. - number_of_samples_per_channel (Optional[int]): Specifies the - number of samples to read. - - If you set this input to nidaqmx.constants. - READ_ALL_AVAILABLE, NI-DAQmx determines how many samples - to read based on if the task acquires samples - continuously or acquires a finite number of samples. - - If the task acquires samples continuously and you set - this input to nidaqmx.constants.READ_ALL_AVAILABLE, this - method reads all the samples currently available in the - buffer. - - If the task acquires a finite number of samples and you - set this input to nidaqmx.constants.READ_ALL_AVAILABLE, - the method waits for the task to acquire all requested - samples, then reads those samples. If you set the - "read_all_avail_samp" property to True, the method reads - the samples currently available in the buffer and does - not wait for the task to acquire all requested samples. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for samples to become available. If the - time elapses, the method returns an error and any - samples read before the timeout elapsed. The default - timeout is 10 seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to read the requested samples and returns an error - if it is unable to. - Returns: - int: - - Indicates the number of samples acquired by each channel. - NI-DAQmx returns a single value because this value is the - same for all channels. - """ - number_of_channels = self._in_stream.num_chans - number_of_samples_per_channel = ( - self._task._calculate_num_samps_per_chan( - number_of_samples_per_channel)) - - if len(waveforms) != number_of_channels: - raise DaqError( - f'The number of waveforms provided ({len(waveforms)}) does not match ' - f'the number of channels in the task ({number_of_channels}). Please provide ' - 'one waveform for each channel.', - DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) - - for i, waveform in enumerate(waveforms): - if number_of_samples_per_channel > waveform.sample_count: - # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform - raise DaqError( - f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' - f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' - 'waveforms or adjust the number of samples requested.', - DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) - - waveforms = self._interpreter.read_digital_waveforms( - self._handle, - number_of_channels, - number_of_samples_per_channel, - self._in_stream.di_num_booleans_per_chan, - timeout, - waveforms, - self._in_stream.waveform_attribute_mode, - ) - - return waveforms +# Re-export the __all__ list from the subpackage +from .stream_readers import __all__ diff --git a/src/handwritten/stream_readers/__init__.py b/src/handwritten/stream_readers/__init__.py new file mode 100644 index 000000000..e4a29fef7 --- /dev/null +++ b/src/handwritten/stream_readers/__init__.py @@ -0,0 +1,33 @@ +""" +NI-DAQmx stream readers. + +This package provides classes for reading samples from NI-DAQmx tasks. +""" + +from __future__ import annotations + +from nidaqmx import DaqError + +from .analog_single_channel_reader import AnalogSingleChannelReader +from .analog_multi_channel_reader import AnalogMultiChannelReader +from .analog_unscaled_reader import AnalogUnscaledReader +from .counter_reader import CounterReader +from .digital_single_channel_reader import DigitalSingleChannelReader +from .digital_multi_channel_reader import DigitalMultiChannelReader +from .power_readers import ( + PowerSingleChannelReader, + PowerMultiChannelReader, + PowerBinaryReader, +) + +__all__ = [ + 'AnalogSingleChannelReader', + 'AnalogMultiChannelReader', + 'AnalogUnscaledReader', + 'CounterReader', + 'DigitalSingleChannelReader', + 'DigitalMultiChannelReader', + 'PowerSingleChannelReader', + 'PowerMultiChannelReader', + 'PowerBinaryReader', +] diff --git a/src/handwritten/stream_readers/_channel_reader_base.py b/src/handwritten/stream_readers/_channel_reader_base.py new file mode 100644 index 000000000..af7a2db02 --- /dev/null +++ b/src/handwritten/stream_readers/_channel_reader_base.py @@ -0,0 +1,129 @@ +from __future__ import annotations + +from nidaqmx import DaqError + +from nidaqmx.error_codes import DAQmxErrors + +class ChannelReaderBase: + """ + Defines base class for all NI-DAQmx stream readers. + """ + + def __init__(self, task_in_stream): + """ + Args: + task_in_stream: Specifies the input stream associated with + an NI-DAQmx task from which to read samples. + """ + self._in_stream = task_in_stream + self._task = task_in_stream._task + self._handle = task_in_stream._task._handle + self._interpreter = task_in_stream._task._interpreter + + self._verify_array_shape = True + + @property + def verify_array_shape(self): + """ + bool: Indicates whether the size and shape of the user-defined + NumPy arrays passed to read methods are verified. Defaults + to True when this object is instantiated. + + Setting this property to True may marginally adversely + impact the performance of read methods. + """ + return self._verify_array_shape + + @verify_array_shape.setter + def verify_array_shape(self, val): + self._verify_array_shape = val + + def _verify_array(self, data, number_of_samples_per_channel, + is_many_chan, is_many_samp): + """ + Verifies that the shape of the specified NumPy array can be used + to read multiple samples from the current task which contains + one or more channels, if the "verify_array_shape" property is + set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + number_of_samples_per_channel (int): Specifies the number of + samples per channel requested. + is_many_chan (bool): Specifies if the read method is a many + channel version. + is_many_samp (bool): Specifies if the read method is a many + samples version. + """ + if not self._verify_array_shape: + return + + channels_to_read = self._in_stream.channels_to_read + number_of_channels = len(channels_to_read.channel_names) + + array_shape: tuple[int, ...] | None = None + if is_many_chan: + if is_many_samp: + array_shape = (number_of_channels, + number_of_samples_per_channel) + else: + array_shape = (number_of_channels,) + else: + if is_many_samp: + array_shape = (number_of_samples_per_channel,) + + if array_shape is not None and data.shape != array_shape: + raise DaqError( + 'Read cannot be performed because the NumPy array passed into ' + 'this function is not shaped correctly. You must pass in a ' + 'NumPy array of the correct shape based on the number of ' + 'channels in task and the number of samples per channel ' + 'requested.\n\n' + 'Shape of NumPy Array provided: {}\n' + 'Shape of NumPy Array required: {}' + .format(data.shape, array_shape), + DAQmxErrors.UNKNOWN, task_name=self._task.name) + + def _verify_array_digital_lines( + self, data, is_many_chan, is_many_line): + """ + Verifies that the shape of the specified NumPy array can be used + to read samples from the current task which contains one or more + channels that have one or more digital lines per channel, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the read method is a + many channel version. + is_many_line (bool): Specifies if the read method is a + many line version. + """ + if not self._verify_array_shape: + return + + channels_to_read = self._in_stream.channels_to_read + number_of_channels = len(channels_to_read.channel_names) + number_of_lines = self._in_stream.di_num_booleans_per_chan + + array_shape: tuple[int, ...] | None = None + if is_many_chan: + if is_many_line: + array_shape = (number_of_channels, number_of_lines) + else: + array_shape = (number_of_channels,) + else: + if is_many_line: + array_shape = (number_of_lines,) + + if array_shape is not None and data.shape != array_shape: + raise DaqError( + 'Read cannot be performed because the NumPy array passed into ' + 'this function is not shaped correctly. You must pass in a ' + 'NumPy array of the correct shape based on the number of ' + 'channels in task and the number of digital lines per ' + 'channel.\n\n' + 'Shape of NumPy Array provided: {}\n' + 'Shape of NumPy Array required: {}' + .format(data.shape, array_shape), + DAQmxErrors.UNKNOWN, task_name=self._task.name) diff --git a/src/handwritten/stream_readers/analog_multi_channel_reader.py b/src/handwritten/stream_readers/analog_multi_channel_reader.py new file mode 100644 index 000000000..7eef6e59c --- /dev/null +++ b/src/handwritten/stream_readers/analog_multi_channel_reader.py @@ -0,0 +1,227 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import AnalogWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more analog input channels in an NI-DAQmx + task. + """ + + def read_many_sample( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_analog_f64( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample(self, data, timeout=10): + """ + Reads a single floating-point sample from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_analog_f64(self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveforms( + self, + waveforms: list[AnalogWaveform[numpy.float64]], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> int: + """ + Reads one or more floating-point samples from one or more analog + input channels into a list of waveforms. + + This read method optionally accepts a preallocated list of waveforms to hold + the samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated list of waveforms is valuable in continuous + acquisition scenarios, where the same waveforms can be used + repeatedly in each call to the method. + + Args: + waveforms (list[AnalogWaveform]): Specifies a list of AnalogWaveform + objects to use for reading samples into. + The list must contain one waveform for each channel in the task. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_channels = self._in_stream.num_chans + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if len(waveforms) != number_of_channels: + raise DaqError( + f'The number of waveforms provided ({len(waveforms)}) does not match ' + f'the number of channels in the task ({number_of_channels}). Please provide ' + 'one waveform for each channel.', + DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) + + for i, waveform in enumerate(waveforms): + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' + 'waveforms or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + return self._interpreter.read_analog_waveforms( + self._handle, + number_of_samples_per_channel, + timeout, + waveforms, + self._in_stream.waveform_attribute_mode, + ) diff --git a/src/handwritten/stream_readers/analog_single_channel_reader.py b/src/handwritten/stream_readers/analog_single_channel_reader.py new file mode 100644 index 000000000..2df27dffd --- /dev/null +++ b/src/handwritten/stream_readers/analog_single_channel_reader.py @@ -0,0 +1,187 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import AnalogWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogSingleChannelReader(ChannelReaderBase): + """ + Reads samples from an analog input channel in an NI-DAQmx task. + """ + + def read_many_sample( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from a single analog + input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_analog_f64( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample(self, timeout=10): + """ + Reads a single floating-point sample from a single analog input + channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: + + Indicates a single floating-point sample from the task. + """ + return self._interpreter.read_analog_scalar_f64(self._handle, timeout) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveform( + self, + waveform: AnalogWaveform[numpy.float64], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> int: + """ + Reads one or more floating-point samples from a single analog + input channel into a waveform. + + This read method optionally accepts a preallocated waveform to hold + the samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated waveform is valuable in continuous + acquisition scenarios, where the same waveform can be used + repeatedly in each call to the method. + + Args: + waveform (AnalogWaveform): Specifies an AnalogWaveform object + to use for reading samples into. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The provided waveform does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide a larger ' + 'waveform or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + return self._interpreter.read_analog_waveform( + self._handle, + number_of_samples_per_channel, + timeout, + waveform, + self._in_stream.waveform_attribute_mode, + ) diff --git a/src/handwritten/stream_readers/analog_unscaled_reader.py b/src/handwritten/stream_readers/analog_unscaled_reader.py new file mode 100644 index 000000000..0e4e31640 --- /dev/null +++ b/src/handwritten/stream_readers/analog_unscaled_reader.py @@ -0,0 +1,352 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE + +from ._channel_reader_base import ChannelReaderBase + + +class AnalogUnscaledReader(ChannelReaderBase): + """ + Reads unscaled samples from one or more analog input channels in an + NI-DAQmx task. + """ + + def read_int16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 16-bit integer samples from one or + more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 16-bit integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_i16( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_int32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 32-bit integer samples from one or + more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 32-bit integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_i32( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled 16-bit unsigned integer samples from + one or more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 16-bit unsigned integer values to + hold the samples requested. The size of the array must + be large enough to hold all requested samples from all + channels in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_u16( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more unscaled unsigned 32-bit integer samples from + one or more analog input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of unscaled 32-bit unsigned integer values to + hold the samples requested. The size of the array must + be large enough to hold all requested samples from all + channels in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_binary_u32( + self._handle, number_of_samples_per_channel, + timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read diff --git a/src/handwritten/stream_readers/counter_reader.py b/src/handwritten/stream_readers/counter_reader.py new file mode 100644 index 000000000..546ccdeed --- /dev/null +++ b/src/handwritten/stream_readers/counter_reader.py @@ -0,0 +1,521 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.types import CtrFreq, CtrTick, CtrTime + +from ._channel_reader_base import ChannelReaderBase + + +class CounterReader(ChannelReaderBase): + """ + Reads samples from a counter input channel in an NI-DAQmx task. + """ + + def read_many_sample_double( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point samples from a single counter + input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_counter_f64_ex( + self._handle,number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_pulse_frequency( + self, frequencies, duty_cycles, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of frequency from a + single counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + frequencies (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the frequency + portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + duty_cycles (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the duty + cycle portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + frequencies, number_of_samples_per_channel, False, True) + self._verify_array( + duty_cycles, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_freq( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) + + return samps_per_chan_read + + def read_many_sample_pulse_ticks( + self, high_ticks, low_ticks, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of ticks from a single + counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + high_ticks (numpy.ndarray): Specifies a preallocated 1D + NumPy array of 32-bit unsigned integer values to hold + the high ticks portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + low_ticks (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the low + ticks portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + high_ticks, number_of_samples_per_channel, False, True) + self._verify_array( + low_ticks, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_ticks( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) + + return samps_per_chan_read + + def read_many_sample_pulse_time( + self, high_times, low_times, + number_of_samples_per_channel=READ_ALL_AVAILABLE, timeout=10.0): + """ + Reads one or more pulse samples in terms of time from a single + counter input channel in a task. + + This read method accepts preallocated NumPy arrays to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in preallocated arrays is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + high_times (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the high + time portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + low_times (numpy.ndarray): Specifies a preallocated 1D + NumPy array of floating-point values to hold the low + time portion of the pulse samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array( + high_times, number_of_samples_per_channel, False, True) + self._verify_array( + low_times, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_ctr_time( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) + + return samps_per_chan_read + + def read_many_sample_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from a single + counter input channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_counter_u32_ex( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_double(self, timeout=10): + """ + Reads a single floating-point sample from a single counter input + channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: Indicates a single floating-point sample from the + task. + """ + return self._interpreter.read_counter_scalar_f64(self._handle, timeout) + + def read_one_sample_pulse_frequency(self, timeout=10): + """ + Reads a pulse sample in terms of frequency from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrFreq: + + Indicates a pulse sample in terms of frequency from the task. + """ + freq, duty_cycle = self._interpreter.read_ctr_freq_scalar(self._handle, timeout) + + return CtrFreq(freq, duty_cycle) + + def read_one_sample_pulse_ticks(self, timeout=10): + """ + Reads a pulse sample in terms of ticks from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrTick: + + Indicates a pulse sample in terms of ticks from the task. + """ + high_ticks, low_ticks = self._interpreter.read_ctr_ticks_scalar(self._handle, timeout) + + return CtrTick(high_ticks, low_ticks) + + def read_one_sample_pulse_time(self, timeout=10): + """ + Reads a pulse sample in terms of time from a single counter + input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + nidaqmx.types.CtrTime: + + Indicates a pulse sample in terms of time from the task. + """ + high_time, low_time = self._interpreter.read_ctr_time_scalar(self._handle, timeout) + + return CtrTime(high_time, low_time) + + def read_one_sample_uint32(self, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from a single + counter input channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 32-bit unsigned integer sample from the + task. + """ + return self._interpreter.read_counter_scalar_u32(self._handle, timeout) diff --git a/src/handwritten/stream_readers/digital_multi_channel_reader.py b/src/handwritten/stream_readers/digital_multi_channel_reader.py new file mode 100644 index 000000000..92355ff71 --- /dev/null +++ b/src/handwritten/stream_readers/digital_multi_channel_reader.py @@ -0,0 +1,568 @@ +from __future__ import annotations + +import numpy +from nidaqmx import DaqError + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.error_codes import DAQmxErrors +from nitypes.waveform import DigitalWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class DigitalMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more digital input channels in an NI-DAQmx + task. + """ + + def read_many_sample_port_byte( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 8-bit unsigned integer samples from one or + more digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 16-bit unsigned integer samples from one or + more digital input channels in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u16( + self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from one or + more digital input channels in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. The size of the array must be large + enough to hold all requested samples from all channels + in the task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, True, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u32( + self._handle, number_of_samples_per_channel, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_multi_line(self, data, timeout=10): + """ + Reads a single boolean sample from one or more digital input + channels in a task. The channels can contain multiple digital + lines. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of boolean values to hold the samples requested. + The size of the array must be large enough to hold all + requested samples from all channels in the task; + otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a line from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property may marginally adversely impact + the performance of the method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, True, True) + + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_one_line(self, data, timeout=10): + """ + Reads a single boolean sample from one or more digital input + channels in a task. The channel can contain only one digital + line. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of boolean values to hold the samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, True, False) + + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_byte(self, data, timeout=10): + """ + Reads a single 8-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u8( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_uint16(self, data, timeout=10): + """ + Reads a single 16-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u16( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_port_uint32(self, data, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from one or more + digital input channels in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(data, 1, True, False) + + self._interpreter.read_digital_u32( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveforms( + self, + waveforms: list[DigitalWaveform[numpy.uint8]], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: int = 10, + ) -> list[DigitalWaveform[numpy.uint8]]: + """ + Reads one or more samples from one or more digital + input channels into a list of waveforms. + + Args: + waveforms (list[DigitalWaveform]): Specifies an existing + list of DigitalWaveform objects to use for reading samples into. + The list must contain one waveform + for each channel in the task. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_channels = self._in_stream.num_chans + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + if len(waveforms) != number_of_channels: + raise DaqError( + f'The number of waveforms provided ({len(waveforms)}) does not match ' + f'the number of channels in the task ({number_of_channels}). Please provide ' + 'one waveform for each channel.', + DAQmxErrors.MISMATCHED_INPUT_ARRAY_SIZES, task_name=self._task.name) + + for i, waveform in enumerate(waveforms): + if number_of_samples_per_channel > waveform.sample_count: + # TODO: AB#3228924 - if allowed by the caller, increase the sample count of the waveform + raise DaqError( + f'The waveform at index {i} does not have enough space ({waveform.sample_count}) to hold ' + f'the requested number of samples ({number_of_samples_per_channel}). Please provide larger ' + 'waveforms or adjust the number of samples requested.', + DAQmxErrors.READ_BUFFER_TOO_SMALL, task_name=self._task.name) + + waveforms = self._interpreter.read_digital_waveforms( + self._handle, + number_of_channels, + number_of_samples_per_channel, + self._in_stream.di_num_booleans_per_chan, + timeout, + waveforms, + self._in_stream.waveform_attribute_mode, + ) + + return waveforms diff --git a/src/handwritten/stream_readers/digital_single_channel_reader.py b/src/handwritten/stream_readers/digital_single_channel_reader.py new file mode 100644 index 000000000..ea1687767 --- /dev/null +++ b/src/handwritten/stream_readers/digital_single_channel_reader.py @@ -0,0 +1,447 @@ +from __future__ import annotations + +import numpy + +from nidaqmx._feature_toggles import WAVEFORM_SUPPORT, requires_feature +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nitypes.waveform import DigitalWaveform + +from ._channel_reader_base import ChannelReaderBase + + +class DigitalSingleChannelReader(ChannelReaderBase): + """ + Reads samples from a digital input channel in an NI-DAQmx task. + """ + + def read_many_sample_port_byte( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 8-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 8-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint16( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 16-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 16 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 16-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u16( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_many_sample_port_uint32( + self, data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more 32-bit unsigned integer samples from a single + digital input channel in a task. + + Use this method for devices with up to 32 lines per port. + + This read method accepts a preallocated NumPy array to hold + the samples requested, which can be advantageous for performance + and interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of 32-bit unsigned integer values to hold the + samples requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(data, number_of_samples_per_channel, False, True) + + _, samps_per_chan_read = self._interpreter.read_digital_u32( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, data) + + return samps_per_chan_read + + def read_one_sample_multi_line(self, data, timeout=10): + """ + Reads a single boolean sample from a single digital input + channel in a task. The channel can contain multiple digital + lines. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of boolean values to hold the samples requested. + + Each element in the array corresponds to a sample from + a line in the channel. The size of the array must be + large enough to hold all requested samples from the + channel in the task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array_digital_lines(data, False, True) + + _, samps_per_chan_read, num_bytes_per_samp = self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def read_one_sample_one_line(self, timeout=10): + """ + Reads a single boolean sample from a single digital input + channel in a task. The channel can contain only one digital + line. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + bool: + + Indicates a single boolean sample from the task. + """ + data = numpy.zeros(1, dtype=bool) + _, samps_per_chan_read, num_bytes_per_samp= self._interpreter.read_digital_lines( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return bool(data[0]) + + def read_one_sample_port_byte(self, timeout=10): + """ + Reads a single 8-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 8-bit unsigned integer sample from the + task. + """ + data = numpy.zeros(1, dtype=numpy.uint8) + _, samps_per_chan_read = self._interpreter.read_digital_u8( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return int(data[0]) + + def read_one_sample_port_uint16(self, timeout=10): + """ + Reads a single 16-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 16-bit unsigned integer sample from the + task. + """ + data = numpy.zeros(1, dtype=numpy.uint16) + _, samps_per_read_chan = self._interpreter.read_digital_u16( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + return int(data[0]) + + def read_one_sample_port_uint32(self, timeout=10): + """ + Reads a single 32-bit unsigned integer sample from a single + digital input channel in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates a single 32-bit unsigned integer sample from the + task. + """ + return self._interpreter.read_digital_scalar_u32(self._handle, timeout) + + @requires_feature(WAVEFORM_SUPPORT) + def read_waveform( + self, + waveform: DigitalWaveform[numpy.uint8], + number_of_samples_per_channel: int = READ_ALL_AVAILABLE, + timeout: float = 10.0, + ) -> int: + """ + Reads one or more digital samples from a single digital input + channel into a waveform. + + Args: + waveform (DigitalWaveform[numpy.uint8]): Specifies a + preallocated DigitalWaveform object to hold the samples + requested. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + return self._interpreter.read_digital_waveform( + self._handle, + number_of_samples_per_channel, + timeout, + waveform, + self._in_stream.waveform_attribute_mode + ) diff --git a/src/handwritten/stream_readers/power_readers.py b/src/handwritten/stream_readers/power_readers.py new file mode 100644 index 000000000..2a581f9cf --- /dev/null +++ b/src/handwritten/stream_readers/power_readers.py @@ -0,0 +1,385 @@ +from __future__ import annotations + +from nidaqmx.constants import FillMode, READ_ALL_AVAILABLE +from nidaqmx.types import PowerMeasurement + +from ._channel_reader_base import ChannelReaderBase + + +class PowerSingleChannelReader(ChannelReaderBase): + """ + Reads samples from an analog input power channel in an NI-DAQmx task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point power samples from a single analog + input power channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the current samples + requested. + + Each element in the array corresponds to a sample from + the channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, False, True) + self._verify_array(current_data, number_of_samples_per_channel, False, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_f64( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read + + def read_one_sample(self, timeout=10): + """ + Reads a single floating-point power sample from a single analog input + power channel in a task. + + Args: + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + float: + + Indicates a single floating-point power sample from the task. + """ + voltage, current = self._interpreter.read_power_scalar_f64(self._handle, timeout) + return PowerMeasurement(voltage=voltage, current=current) + + +class PowerMultiChannelReader(ChannelReaderBase): + """ + Reads samples from one or more analog input power channels in an NI-DAQmx + task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more floating-point power samples from one or more analog + input power channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the voltage samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + current_data (numpy.ndarray): Specifies a preallocated 2D NumPy + array of floating-point values to hold the current samples + requested. The size of the array must be large enough to + hold all requested samples from all channels in the + task; otherwise, an error is thrown. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, True, True) + self._verify_array(current_data, number_of_samples_per_channel, True, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_f64( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read + + def read_one_sample(self, voltage_data, current_data, timeout=10): + """ + Reads a single floating-point power sample from one or more analog + input channels in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of floating-point values to hold the voltage samples + requested. + + Each element in the array corresponds to a sample from + each channel. The size of the array must be large enough + to hold all requested samples from the channel in the + task; otherwise, an error is thrown. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + """ + self._verify_array(voltage_data, 1, True, False) + self._verify_array(current_data, 1, True, False) + + self._interpreter.read_power_f64( + self._handle, 1, timeout, FillMode.GROUP_BY_CHANNEL.value, + voltage_data, current_data) + + +class PowerBinaryReader(ChannelReaderBase): + """ + Reads binary samples from one or more analog input power channels in an + NI-DAQmx task. + """ + + def read_many_sample( + self, voltage_data, current_data, number_of_samples_per_channel=READ_ALL_AVAILABLE, + timeout=10.0): + """ + Reads one or more binary int16 samples from one or more analog + input power channel in a task. + + This read method accepts a preallocated NumPy array to hold the + samples requested, which can be advantageous for performance and + interoperability with NumPy and SciPy. + + Passing in a preallocated array is valuable in continuous + acquisition scenarios, where the same array can be used + repeatedly in each call to the method. + + Args: + voltage_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of i16 values to hold the voltage samples requested. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + current_data (numpy.ndarray): Specifies a preallocated 1D NumPy + array of i16 values to hold the current samples requested. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task or to + the order of the channels you specify with the + "channels_to_read" property. + + If the size of the array is too large or the array is + shaped incorrectly, the previous statement may not hold + true as the samples read may not be separated into rows + and columns properly. Set the "verify_array_shape" + property on this channel reader object to True to + validate that the NumPy array object is shaped properly. + Setting this property to True may marginally adversely + impact the performance of the method. + number_of_samples_per_channel (Optional[int]): Specifies the + number of samples to read. + + If you set this input to nidaqmx.constants. + READ_ALL_AVAILABLE, NI-DAQmx determines how many samples + to read based on if the task acquires samples + continuously or acquires a finite number of samples. + + If the task acquires samples continuously and you set + this input to nidaqmx.constants.READ_ALL_AVAILABLE, this + method reads all the samples currently available in the + buffer. + + If the task acquires a finite number of samples and you + set this input to nidaqmx.constants.READ_ALL_AVAILABLE, + the method waits for the task to acquire all requested + samples, then reads those samples. If you set the + "read_all_avail_samp" property to True, the method reads + the samples currently available in the buffer and does + not wait for the task to acquire all requested samples. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for samples to become available. If the + time elapses, the method returns an error and any + samples read before the timeout elapsed. The default + timeout is 10 seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to read the requested samples and returns an error + if it is unable to. + Returns: + int: + + Indicates the number of samples acquired by each channel. + NI-DAQmx returns a single value because this value is the + same for all channels. + """ + number_of_samples_per_channel = ( + self._task._calculate_num_samps_per_chan( + number_of_samples_per_channel)) + + self._verify_array(voltage_data, number_of_samples_per_channel, True, True) + self._verify_array(current_data, number_of_samples_per_channel, True, True) + + _, _, samps_per_chan_read = self._interpreter.read_power_binary_i16( + self._handle, number_of_samples_per_channel, timeout, + FillMode.GROUP_BY_CHANNEL.value, voltage_data, current_data) + + return samps_per_chan_read diff --git a/src/handwritten/stream_writers.py b/src/handwritten/stream_writers.py index 6e05a5a4c..e9073de43 100644 --- a/src/handwritten/stream_writers.py +++ b/src/handwritten/stream_writers.py @@ -1,1401 +1,9 @@ -import numpy +""" +NI-DAQmx stream writers. +""" -from nidaqmx.constants import FillMode -from nidaqmx import DaqError -from nidaqmx.error_codes import DAQmxErrors +# Import all classes from the subpackage to maintain backward compatibility +from .stream_writers import * # noqa: F403, F401 -__all__ = ['AnalogSingleChannelWriter', 'AnalogMultiChannelWriter', - 'AnalogUnscaledWriter', 'CounterWriter', - 'DigitalSingleChannelWriter', 'DigitalMultiChannelWriter'] - - -class UnsetAutoStartSentinel: - pass - - -AUTO_START_UNSET = UnsetAutoStartSentinel() - -del UnsetAutoStartSentinel - - -class ChannelWriterBase: - """ - Defines base class for all NI-DAQmx stream writers. - """ - - def __init__(self, task_out_stream, auto_start=AUTO_START_UNSET): - """ - Args: - task_out_stream: Specifies the output stream associated with - an NI-DAQmx task which to write samples. - auto_start (Optional[bool]): Specifies if the write method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - - If you do not specify a value for this parameter, - NI-DAQmx determines its value based on the type of write - method used. If you use a one sample write method, the - value is True; conversely, if you use a many sample - write method, the value is False. - """ - self._out_stream = task_out_stream - self._task = task_out_stream._task - self._handle = task_out_stream._task._handle - self._interpreter = task_out_stream._task._interpreter - - self._verify_array_shape = True - self._auto_start = auto_start - - @property - def auto_start(self): - """ - bool: Specifies if the write method automatically starts the - task if you did not explicitly start it with the DAQmx Start - Task method. - - If you do not specify a value for this parameter, NI-DAQmx - determines its value based on the type of write method used. - If you use a one sample write method, its value is True; - conversely, if you use a many sample write method, its value - is False. - """ - return self._auto_start - - @auto_start.setter - def auto_start(self, val): - self._auto_start = val - - @auto_start.deleter - def auto_start(self): - self._auto_start = AUTO_START_UNSET - - @property - def verify_array_shape(self): - """ - bool: Indicates whether the size and shape of the user-defined - NumPy arrays passed to read methods are verified. Defaults - to True when this object is instantiated. - - Setting this property to True may marginally adversely - impact the performance of read methods. - """ - return self._verify_array_shape - - @verify_array_shape.setter - def verify_array_shape(self, val): - self._verify_array_shape = val - - def _verify_array(self, data, is_many_chan, is_many_samp): - """ - Verifies that the shape of the specified NumPy array can be used - with the specified write method type, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the write method is a many - channel version. - is_many_samp (bool): Specifies if the write method is a many - sample version. - """ - if not self._verify_array_shape: - return - - channels_to_write = self._task.channels - number_of_channels = len(channels_to_write.channel_names) - - expected_num_dimensions = None - if is_many_chan: - if is_many_samp: - expected_num_dimensions = 2 - else: - expected_num_dimensions = 1 - - if data.shape[0] != number_of_channels: - self._task._raise_invalid_write_num_chans_error( - number_of_channels, data.shape[0]) - else: - if is_many_samp: - expected_num_dimensions = 1 - - if expected_num_dimensions is not None: - self._raise_error_if_invalid_write_dimensions( - expected_num_dimensions, len(data.shape)) - - def _verify_array_digital_lines( - self, data, is_many_chan, is_many_line): - """ - Verifies that the shape of the specified NumPy array can be used - to read samples from the current task which contains one or more - channels that have one or more digital lines per channel, if the - "verify_array_shape" property is set to True. - - Args: - data (numpy.ndarray): Specifies the NumPy array to verify. - is_many_chan (bool): Specifies if the write method is a - many channel version. - is_many_line (bool): Specifies if the write method is a - many line version. - """ - if not self._verify_array_shape: - return - - channels_to_write = self._task.channels - number_of_channels = len(channels_to_write.channel_names) - number_of_lines = self._out_stream.do_num_booleans_per_chan - - expected_num_dimensions = None - if is_many_chan: - if data.shape[0] != number_of_channels: - self._task._raise_invalid_write_num_chans_error( - number_of_channels, data.shape[0]) - - if is_many_line: - expected_num_dimensions = 2 - if data.shape[1] != number_of_lines: - self._task._raise_invalid_num_lines_error( - number_of_lines, data.shape[1]) - else: - expected_num_dimensions = 1 - else: - if is_many_line: - expected_num_dimensions = 1 - if data.shape[0] != number_of_lines: - self._task._raise_invalid_num_lines_error( - number_of_lines, data.shape[0]) - - if expected_num_dimensions is not None: - self._raise_error_if_invalid_write_dimensions( - expected_num_dimensions, len(data.shape)) - - def _raise_error_if_invalid_write_dimensions( - self, num_dimensions_expected, num_dimensions_in_data): - if num_dimensions_expected != num_dimensions_in_data: - raise DaqError( - 'Write cannot be performed because the NumPy array passed ' - 'into this function is not shaped correctly. ' - 'You must pass in a NumPy array of the correct number of ' - 'dimensions based on the write method you use.\n\n' - 'No. of dimensions of NumPy Array provided: {}\n' - 'No. of dimensions of NumPy Array required: {}' - .format(num_dimensions_in_data, num_dimensions_expected), - DAQmxErrors.UNKNOWN, task_name=self._task.name) - - -class AnalogSingleChannelWriter(ChannelWriterBase): - """ - Writes samples to an analog output channel in an NI-DAQmx task. - """ - - def write_many_sample(self, data, timeout=10.0): - """ - Writes one or more floating-point samples to a single analog - output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of - floating-point samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_analog_f64( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample(self, data, timeout=10): - """ - Writes a single floating-point sample to a single analog output - channel in a task. - - Args: - data (float): Specifies the floating-point sample to write - to the task. - auto_start (Optional[bool]): Specifies if this method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_analog_scalar_f64( - self._handle, auto_start, timeout, data) - - -class AnalogMultiChannelWriter(ChannelWriterBase): - """ - Writes samples to one or more analog output channels in an NI-DAQmx - task. - """ - - def write_many_sample(self, data, timeout=10.0): - """ - Writes one or more floating-point samples to one or more analog - output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of - floating-point samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_analog_f64( - self._handle, data.shape[1], auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample(self, data, timeout=10): - """ - Writes a single floating-point sample to one or more analog - output channels in a task. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of - floating-point samples to write to the task. - - Each element of the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_analog_f64( - self._handle, 1, auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) - - -class AnalogUnscaledWriter(ChannelWriterBase): - """ - Writes unscaled samples to one or more analog output channels in - an NI-DAQmx task. - """ - - def write_int16(self, data, timeout=10.0): - """ - Writes one or more unscaled 16-bit integer samples to one or - more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 16-bit integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_i16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_int32(self, data, timeout=10.0): - """ - Writes one or more unscaled 32-bit integer samples to one or - more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 32-bit integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_i32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_uint16(self, data, timeout=10.0): - """ - Writes one or more unscaled 16-bit unsigned integer samples to - one or more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 16-bit unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_u16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_uint32(self, data, timeout=10.0): - """ - Writes one or more unscaled 32-bit unsigned integer samples to - one or more analog output channels in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of unscaled - 32-bit unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_binary_u32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - -class CounterWriter(ChannelWriterBase): - """ - Writes samples to a counter output channel in an NI-DAQmx task. - """ - - def write_many_sample_pulse_frequency( - self, frequencies, duty_cycles, timeout=10.0): - """ - Writes one or more pulse samples in terms of frequency to a - single counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - frequencies (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the frequency portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - duty_cycles (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the duty cycle portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(frequencies, False, True) - self._verify_array(duty_cycles, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_freq( - self._handle, frequencies.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) - - def write_many_sample_pulse_ticks( - self, high_ticks, low_ticks, timeout=10.0): - """ - Writes one or more pulse samples in terms of ticks to a single - counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - high_ticks (numpy.ndarray): Contains a 1D NumPy array of - 32-bit unsigned integer values that holds the high ticks - portion of the pulse samples to write to the task. Each - element of the array corresponds to a sample to write. - low_ticks (numpy.ndarray): Contains a 1D NumPy array of - 32-bit unsigned integer values that holds the low ticks - portion of the pulse samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(high_ticks, False, True) - self._verify_array(low_ticks, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_ticks( - self._handle, high_ticks.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) - - def write_many_sample_pulse_time( - self, high_times, low_times, timeout=10.0): - """ - Writes one or more pulse samples in terms of time to a single - counter output channel in a task. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - high_times (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the high time portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - low_times (numpy.ndarray): Contains a 1D NumPy array of - floating-point values that holds the low time portion - of the pulse samples to write to the task. Each element - of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(high_times, False, True) - self._verify_array(low_times, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_ctr_time( - self._handle, high_times.shape[0], auto_start, timeout, - FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) - - def write_one_sample_pulse_frequency( - self, frequency, duty_cycle, timeout=10): - """ - Writes a new pulse frequency and duty cycle to a single counter - output channel in a task. - - Args: - frequency (float): Specifies at what frequency to generate - pulses. - duty_cycle (float): Specifies the width of the pulse divided - by the pulse period. NI-DAQmx uses this ratio combined - with frequency to determine pulse width and the interval - between pulses. - auto_start (Optional[bool]): Specifies if this method - automatically starts the task if you did not explicitly - start it with the DAQmx Start Task method. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_freq_scalar( - self._handle, auto_start, timeout, frequency, duty_cycle) - - def write_one_sample_pulse_ticks( - self, high_ticks, low_ticks, timeout=10): - """ - Writes a new pulse high tick count and low tick count to a - single counter output channel in a task. - - Args: - high_ticks (float): Specifies the number of ticks the pulse - is high. - low_ticks (float): Specifies the number of ticks the pulse - is low. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_ticks_scalar( - self._handle, auto_start, timeout, high_ticks, low_ticks) - - def write_one_sample_pulse_time( - self, high_time, low_time, timeout=10): - """ - Writes a new pulse high time and low time to a single counter - output channel in a task. - - Args: - high_time (float): Specifies the amount of time the pulse - is high. - low_time (float): Specifies the amount of time the pulse - is low. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_ctr_time_scalar( - self._handle, auto_start, timeout, high_time, low_time) - - -class DigitalSingleChannelWriter(ChannelWriterBase): - """ - Writes samples to a single digital output channel in an NI-DAQmx - task. - """ - - def write_many_sample_port_byte(self, data, timeout=10.0): - """ - Writes one or more 8-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 8 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 8-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u8( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint16(self, data, timeout=10.0): - """ - Writes one or more 16-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 16 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 16-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u16( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint32(self, data, timeout=10.0): - """ - Writes one or more 32-bit unsigned integer samples to a single - digital output channel in a task. - - Use this method for devices with up to 32 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 32-bit - unsigned integer samples to write to the task. Each - element of the array corresponds to a sample to write. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote. - """ - self._verify_array(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u32( - self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_multi_line(self, data, timeout=10): - """ - Writes a single boolean sample to a single digital output - channel in a task. The channel can contain multiple digital - lines. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of boolean - samples to write to the task. Each element of the array - corresponds to a line in the channel. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, False, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_one_line(self, data, timeout=10): - """ - Writes a single boolean sample to a single digital output - channel in a task. The channel can contain only one digital - line. - - Args: - data (int): Specifies the boolean sample to write to the - task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=bool) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_byte(self, data, timeout=10): - """ - Writes a single 8-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - data (int): Specifies the 8-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=numpy.uint8) - - return self._interpreter.write_digital_u8( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_uint16(self, data, timeout=10): - """ - Writes a single 16-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - data (int): Specifies the 16-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - numpy_array = numpy.asarray([data], dtype=numpy.uint16) - - return self._interpreter.write_digital_u16( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) - - def write_one_sample_port_uint32(self, data, timeout=10): - """ - Writes a single 32-bit unsigned integer sample to a single - digital output channel in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - data (int): Specifies the 32-bit unsigned integer sample to - write to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_scalar_u32( - self._handle, auto_start, timeout, data) - - -class DigitalMultiChannelWriter(ChannelWriterBase): - """ - Writes samples to one or more digital output channels in an NI-DAQmx - task. - """ - - def write_many_sample_port_byte(self, data, timeout=10.0): - """ - Writes one or more 8-bit unsigned integer samples to one or more - digital output channels in a task. - - Use this method for devices with up to 8 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 8-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u8( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint16(self, data, timeout=10.0): - """ - Writes one or more 16-bit unsigned integer samples to one or - more digital output channels in a task. - - Use this method for devices with up to 16 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 16-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u16( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_many_sample_port_uint32(self, data, timeout=10.0): - """ - Writes one or more 32-bit unsigned integer samples to one or - more digital output channels in a task. - - Use this method for devices with up to 32 lines per port. - - If the task uses on-demand timing, this method returns only - after the device generates all samples. On-demand is the default - timing type if you do not use the timing property on the task to - configure a sample timing type. If the task uses any timing type - other than on-demand, this method returns immediately and does - not wait for the device to generate all samples. Your - application must determine if the task is done to ensure that - the device generated all samples. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of 32-bit - unsigned integer samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a sample to write to each channel. - The order of the channels in the array corresponds to - the order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - Returns: - int: - - Specifies the actual number of samples this method - successfully wrote to each channel in the task. - """ - self._verify_array(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else False) - - return self._interpreter.write_digital_u32( - self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_multi_line(self, data, timeout=10): - """ - Writes a single boolean sample to one or more digital output - channels in a task. The channel can contain multiple digital - lines. - - Args: - data (numpy.ndarray): Contains a 2D NumPy array of boolean - samples to write to the task. - - Each row corresponds to a channel in the task. Each - column corresponds to a line from each channel. The - order of the channels in the array corresponds to the - order in which you add the channels to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, True, True) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_one_line(self, data, timeout=10): - """ - Writes a single boolean sample to one or more digital output - channels in a task. The channel can contain only one digital - line. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of boolean - samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array_digital_lines(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_lines( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_byte(self, data, timeout=10): - """ - Writes a single 8-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 8 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 8-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u8( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_uint16(self, data, timeout=10): - """ - Writes a single 16-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 16 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 16-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u16( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) - - def write_one_sample_port_uint32(self, data, timeout=10): - """ - Writes a single 32-bit unsigned integer sample to one or more - digital output channels in a task. - - Use this method for devices with up to 32 lines per port. - - Args: - data (numpy.ndarray): Contains a 1D NumPy array of 32-bit - unsigned integer samples to write to the task. - - Each element in the array corresponds to a channel in - the task. The order of the channels in the array - corresponds to the order in which you add the channels - to the task. - timeout (Optional[float]): Specifies the amount of time in - seconds to wait for the method to write all samples. - NI-DAQmx performs a timeout check only if the method - must wait before it writes data. This method returns an - error if the time elapses. The default timeout is 10 - seconds. If you set timeout to - nidaqmx.constants.WAIT_INFINITELY, the method waits - indefinitely. If you set timeout to 0, the method tries - once to write the submitted samples. If the method could - not write all the submitted samples, it returns an error - and the number of samples successfully written. - """ - self._verify_array(data, True, False) - - auto_start = (self._auto_start if self._auto_start is not - AUTO_START_UNSET else True) - - return self._interpreter.write_digital_u32( - self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) +# Re-export the __all__ list from the subpackage +from .stream_writers import __all__ \ No newline at end of file diff --git a/src/handwritten/stream_writers/__init__.py b/src/handwritten/stream_writers/__init__.py new file mode 100644 index 000000000..37aabed61 --- /dev/null +++ b/src/handwritten/stream_writers/__init__.py @@ -0,0 +1,22 @@ +""" +NI-DAQmx stream writers. + +This package provides classes for writing samples to NI-DAQmx tasks. +""" + +from __future__ import annotations + +from .analog_single_channel_writer import AnalogSingleChannelWriter +from .analog_multi_channel_writer import AnalogMultiChannelWriter +from .analog_unscaled_writer import AnalogUnscaledWriter +from .counter_writer import CounterWriter +from .digital_single_channel_writer import DigitalSingleChannelWriter +from .digital_multi_channel_writer import DigitalMultiChannelWriter + +__all__ = [ + 'AnalogSingleChannelWriter', + 'AnalogMultiChannelWriter', + 'AnalogUnscaledWriter', + 'CounterWriter', + 'DigitalSingleChannelWriter', + 'DigitalMultiChannelWriter'] diff --git a/src/handwritten/stream_writers/_channel_writer_base.py b/src/handwritten/stream_writers/_channel_writer_base.py new file mode 100644 index 000000000..c0d02a59a --- /dev/null +++ b/src/handwritten/stream_writers/_channel_writer_base.py @@ -0,0 +1,175 @@ +from nidaqmx import DaqError +from nidaqmx.error_codes import DAQmxErrors + + +class UnsetAutoStartSentinel: + pass + + +AUTO_START_UNSET = UnsetAutoStartSentinel() + +del UnsetAutoStartSentinel + + +class ChannelWriterBase: + """ + Defines base class for all NI-DAQmx stream writers. + """ + + def __init__(self, task_out_stream, auto_start=AUTO_START_UNSET): + """ + Args: + task_out_stream: Specifies the output stream associated with + an NI-DAQmx task which to write samples. + auto_start (Optional[bool]): Specifies if the write method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + + If you do not specify a value for this parameter, + NI-DAQmx determines its value based on the type of write + method used. If you use a one sample write method, the + value is True; conversely, if you use a many sample + write method, the value is False. + """ + self._out_stream = task_out_stream + self._task = task_out_stream._task + self._handle = task_out_stream._task._handle + self._interpreter = task_out_stream._task._interpreter + + self._verify_array_shape = True + self._auto_start = auto_start + + @property + def auto_start(self): + """ + bool: Specifies if the write method automatically starts the + task if you did not explicitly start it with the DAQmx Start + Task method. + + If you do not specify a value for this parameter, NI-DAQmx + determines its value based on the type of write method used. + If you use a one sample write method, its value is True; + conversely, if you use a many sample write method, its value + is False. + """ + return self._auto_start + + @auto_start.setter + def auto_start(self, val): + self._auto_start = val + + @auto_start.deleter + def auto_start(self): + self._auto_start = AUTO_START_UNSET + + @property + def verify_array_shape(self): + """ + bool: Indicates whether the size and shape of the user-defined + NumPy arrays passed to read methods are verified. Defaults + to True when this object is instantiated. + + Setting this property to True may marginally adversely + impact the performance of read methods. + """ + return self._verify_array_shape + + @verify_array_shape.setter + def verify_array_shape(self, val): + self._verify_array_shape = val + + def _verify_array(self, data, is_many_chan, is_many_samp): + """ + Verifies that the shape of the specified NumPy array can be used + with the specified write method type, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the write method is a many + channel version. + is_many_samp (bool): Specifies if the write method is a many + sample version. + """ + if not self._verify_array_shape: + return + + channels_to_write = self._task.channels + number_of_channels = len(channels_to_write.channel_names) + + expected_num_dimensions = None + if is_many_chan: + if is_many_samp: + expected_num_dimensions = 2 + else: + expected_num_dimensions = 1 + + if data.shape[0] != number_of_channels: + self._task._raise_invalid_write_num_chans_error( + number_of_channels, data.shape[0]) + else: + if is_many_samp: + expected_num_dimensions = 1 + + if expected_num_dimensions is not None: + self._raise_error_if_invalid_write_dimensions( + expected_num_dimensions, len(data.shape)) + + def _verify_array_digital_lines( + self, data, is_many_chan, is_many_line): + """ + Verifies that the shape of the specified NumPy array can be used + to read samples from the current task which contains one or more + channels that have one or more digital lines per channel, if the + "verify_array_shape" property is set to True. + + Args: + data (numpy.ndarray): Specifies the NumPy array to verify. + is_many_chan (bool): Specifies if the write method is a + many channel version. + is_many_line (bool): Specifies if the write method is a + many line version. + """ + if not self._verify_array_shape: + return + + channels_to_write = self._task.channels + number_of_channels = len(channels_to_write.channel_names) + number_of_lines = self._out_stream.do_num_booleans_per_chan + + expected_num_dimensions = None + if is_many_chan: + if data.shape[0] != number_of_channels: + self._task._raise_invalid_write_num_chans_error( + number_of_channels, data.shape[0]) + + if is_many_line: + expected_num_dimensions = 2 + if data.shape[1] != number_of_lines: + self._task._raise_invalid_num_lines_error( + number_of_lines, data.shape[1]) + else: + expected_num_dimensions = 1 + else: + if is_many_line: + expected_num_dimensions = 1 + if data.shape[0] != number_of_lines: + self._task._raise_invalid_num_lines_error( + number_of_lines, data.shape[0]) + + if expected_num_dimensions is not None: + self._raise_error_if_invalid_write_dimensions( + expected_num_dimensions, len(data.shape)) + + def _raise_error_if_invalid_write_dimensions( + self, num_dimensions_expected, num_dimensions_in_data): + if num_dimensions_expected != num_dimensions_in_data: + raise DaqError( + 'Write cannot be performed because the NumPy array passed ' + 'into this function is not shaped correctly. ' + 'You must pass in a NumPy array of the correct number of ' + 'dimensions based on the write method you use.\n\n' + 'No. of dimensions of NumPy Array provided: {}\n' + 'No. of dimensions of NumPy Array required: {}' + .format(num_dimensions_in_data, num_dimensions_expected), + DAQmxErrors.UNKNOWN, task_name=self._task.name) diff --git a/src/handwritten/stream_writers/analog_multi_channel_writer.py b/src/handwritten/stream_writers/analog_multi_channel_writer.py new file mode 100644 index 000000000..c314d1a26 --- /dev/null +++ b/src/handwritten/stream_writers/analog_multi_channel_writer.py @@ -0,0 +1,90 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogMultiChannelWriter(ChannelWriterBase): + """ + Writes samples to one or more analog output channels in an NI-DAQmx + task. + """ + + def write_many_sample(self, data, timeout=10.0): + """ + Writes one or more floating-point samples to one or more analog + output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of + floating-point samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_analog_f64( + self._handle, data.shape[1], auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample(self, data, timeout=10): + """ + Writes a single floating-point sample to one or more analog + output channels in a task. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of + floating-point samples to write to the task. + + Each element of the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_analog_f64( + self._handle, 1, auto_start, timeout,FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/src/handwritten/stream_writers/analog_single_channel_writer.py b/src/handwritten/stream_writers/analog_single_channel_writer.py new file mode 100644 index 000000000..e5334140d --- /dev/null +++ b/src/handwritten/stream_writers/analog_single_channel_writer.py @@ -0,0 +1,82 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogSingleChannelWriter(ChannelWriterBase): + """ + Writes samples to an analog output channel in an NI-DAQmx task. + """ + + def write_many_sample(self, data, timeout=10.0): + """ + Writes one or more floating-point samples to a single analog + output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of + floating-point samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_analog_f64( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample(self, data, timeout=10): + """ + Writes a single floating-point sample to a single analog output + channel in a task. + + Args: + data (float): Specifies the floating-point sample to write + to the task. + auto_start (Optional[bool]): Specifies if this method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_analog_scalar_f64( + self._handle, auto_start, timeout, data) + diff --git a/src/handwritten/stream_writers/analog_unscaled_writer.py b/src/handwritten/stream_writers/analog_unscaled_writer.py new file mode 100644 index 000000000..197fe297f --- /dev/null +++ b/src/handwritten/stream_writers/analog_unscaled_writer.py @@ -0,0 +1,190 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class AnalogUnscaledWriter(ChannelWriterBase): + """ + Writes unscaled samples to one or more analog output channels in + an NI-DAQmx task. + """ + + def write_int16(self, data, timeout=10.0): + """ + Writes one or more unscaled 16-bit integer samples to one or + more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 16-bit integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_i16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_int32(self, data, timeout=10.0): + """ + Writes one or more unscaled 32-bit integer samples to one or + more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 32-bit integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_i32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_uint16(self, data, timeout=10.0): + """ + Writes one or more unscaled 16-bit unsigned integer samples to + one or more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 16-bit unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_u16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_uint32(self, data, timeout=10.0): + """ + Writes one or more unscaled 32-bit unsigned integer samples to + one or more analog output channels in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of unscaled + 32-bit unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_binary_u32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/src/handwritten/stream_writers/counter_writer.py b/src/handwritten/stream_writers/counter_writer.py new file mode 100644 index 000000000..2c75a3720 --- /dev/null +++ b/src/handwritten/stream_writers/counter_writer.py @@ -0,0 +1,254 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class CounterWriter(ChannelWriterBase): + """ + Writes samples to a counter output channel in an NI-DAQmx task. + """ + + def write_many_sample_pulse_frequency( + self, frequencies, duty_cycles, timeout=10.0): + """ + Writes one or more pulse samples in terms of frequency to a + single counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + frequencies (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the frequency portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + duty_cycles (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the duty cycle portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(frequencies, False, True) + self._verify_array(duty_cycles, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_freq( + self._handle, frequencies.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, frequencies, duty_cycles) + + def write_many_sample_pulse_ticks( + self, high_ticks, low_ticks, timeout=10.0): + """ + Writes one or more pulse samples in terms of ticks to a single + counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + high_ticks (numpy.ndarray): Contains a 1D NumPy array of + 32-bit unsigned integer values that holds the high ticks + portion of the pulse samples to write to the task. Each + element of the array corresponds to a sample to write. + low_ticks (numpy.ndarray): Contains a 1D NumPy array of + 32-bit unsigned integer values that holds the low ticks + portion of the pulse samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(high_ticks, False, True) + self._verify_array(low_ticks, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_ticks( + self._handle, high_ticks.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_ticks, low_ticks) + + def write_many_sample_pulse_time( + self, high_times, low_times, timeout=10.0): + """ + Writes one or more pulse samples in terms of time to a single + counter output channel in a task. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + high_times (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the high time portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + low_times (numpy.ndarray): Contains a 1D NumPy array of + floating-point values that holds the low time portion + of the pulse samples to write to the task. Each element + of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(high_times, False, True) + self._verify_array(low_times, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_ctr_time( + self._handle, high_times.shape[0], auto_start, timeout, + FillMode.GROUP_BY_CHANNEL.value, high_times, low_times) + + def write_one_sample_pulse_frequency( + self, frequency, duty_cycle, timeout=10): + """ + Writes a new pulse frequency and duty cycle to a single counter + output channel in a task. + + Args: + frequency (float): Specifies at what frequency to generate + pulses. + duty_cycle (float): Specifies the width of the pulse divided + by the pulse period. NI-DAQmx uses this ratio combined + with frequency to determine pulse width and the interval + between pulses. + auto_start (Optional[bool]): Specifies if this method + automatically starts the task if you did not explicitly + start it with the DAQmx Start Task method. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_freq_scalar( + self._handle, auto_start, timeout, frequency, duty_cycle) + + def write_one_sample_pulse_ticks( + self, high_ticks, low_ticks, timeout=10): + """ + Writes a new pulse high tick count and low tick count to a + single counter output channel in a task. + + Args: + high_ticks (float): Specifies the number of ticks the pulse + is high. + low_ticks (float): Specifies the number of ticks the pulse + is low. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_ticks_scalar( + self._handle, auto_start, timeout, high_ticks, low_ticks) + + def write_one_sample_pulse_time( + self, high_time, low_time, timeout=10): + """ + Writes a new pulse high time and low time to a single counter + output channel in a task. + + Args: + high_time (float): Specifies the amount of time the pulse + is high. + low_time (float): Specifies the amount of time the pulse + is low. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_ctr_time_scalar( + self._handle, auto_start, timeout, high_time, low_time) diff --git a/src/handwritten/stream_writers/digital_multi_channel_writer.py b/src/handwritten/stream_writers/digital_multi_channel_writer.py new file mode 100644 index 000000000..15224fd0d --- /dev/null +++ b/src/handwritten/stream_writers/digital_multi_channel_writer.py @@ -0,0 +1,330 @@ +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class DigitalMultiChannelWriter(ChannelWriterBase): + """ + Writes samples to one or more digital output channels in an NI-DAQmx + task. + """ + + def write_many_sample_port_byte(self, data, timeout=10.0): + """ + Writes one or more 8-bit unsigned integer samples to one or more + digital output channels in a task. + + Use this method for devices with up to 8 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 8-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u8( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint16(self, data, timeout=10.0): + """ + Writes one or more 16-bit unsigned integer samples to one or + more digital output channels in a task. + + Use this method for devices with up to 16 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 16-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u16( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint32(self, data, timeout=10.0): + """ + Writes one or more 32-bit unsigned integer samples to one or + more digital output channels in a task. + + Use this method for devices with up to 32 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of 32-bit + unsigned integer samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a sample to write to each channel. + The order of the channels in the array corresponds to + the order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote to each channel in the task. + """ + self._verify_array(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u32( + self._handle, data.shape[1], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_multi_line(self, data, timeout=10): + """ + Writes a single boolean sample to one or more digital output + channels in a task. The channel can contain multiple digital + lines. + + Args: + data (numpy.ndarray): Contains a 2D NumPy array of boolean + samples to write to the task. + + Each row corresponds to a channel in the task. Each + column corresponds to a line from each channel. The + order of the channels in the array corresponds to the + order in which you add the channels to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, True, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_one_line(self, data, timeout=10): + """ + Writes a single boolean sample to one or more digital output + channels in a task. The channel can contain only one digital + line. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of boolean + samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_byte(self, data, timeout=10): + """ + Writes a single 8-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 8-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u8( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_uint16(self, data, timeout=10): + """ + Writes a single 16-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 16-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u16( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_port_uint32(self, data, timeout=10): + """ + Writes a single 32-bit unsigned integer sample to one or more + digital output channels in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 32-bit + unsigned integer samples to write to the task. + + Each element in the array corresponds to a channel in + the task. The order of the channels in the array + corresponds to the order in which you add the channels + to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array(data, True, False) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_u32( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) diff --git a/src/handwritten/stream_writers/digital_single_channel_writer.py b/src/handwritten/stream_writers/digital_single_channel_writer.py new file mode 100644 index 000000000..0edbacdb9 --- /dev/null +++ b/src/handwritten/stream_writers/digital_single_channel_writer.py @@ -0,0 +1,294 @@ +import numpy + +from nidaqmx.constants import FillMode + +from ._channel_writer_base import ChannelWriterBase, AUTO_START_UNSET + + +class DigitalSingleChannelWriter(ChannelWriterBase): + """ + Writes samples to a single digital output channel in an NI-DAQmx + task. + """ + + def write_many_sample_port_byte(self, data, timeout=10.0): + """ + Writes one or more 8-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 8 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 8-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u8( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint16(self, data, timeout=10.0): + """ + Writes one or more 16-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 16 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 16-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u16( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_many_sample_port_uint32(self, data, timeout=10.0): + """ + Writes one or more 32-bit unsigned integer samples to a single + digital output channel in a task. + + Use this method for devices with up to 32 lines per port. + + If the task uses on-demand timing, this method returns only + after the device generates all samples. On-demand is the default + timing type if you do not use the timing property on the task to + configure a sample timing type. If the task uses any timing type + other than on-demand, this method returns immediately and does + not wait for the device to generate all samples. Your + application must determine if the task is done to ensure that + the device generated all samples. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of 32-bit + unsigned integer samples to write to the task. Each + element of the array corresponds to a sample to write. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + Returns: + int: + + Specifies the actual number of samples this method + successfully wrote. + """ + self._verify_array(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else False) + + return self._interpreter.write_digital_u32( + self._handle, data.shape[0], auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_multi_line(self, data, timeout=10): + """ + Writes a single boolean sample to a single digital output + channel in a task. The channel can contain multiple digital + lines. + + Args: + data (numpy.ndarray): Contains a 1D NumPy array of boolean + samples to write to the task. Each element of the array + corresponds to a line in the channel. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + self._verify_array_digital_lines(data, False, True) + + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, data) + + def write_one_sample_one_line(self, data, timeout=10): + """ + Writes a single boolean sample to a single digital output + channel in a task. The channel can contain only one digital + line. + + Args: + data (int): Specifies the boolean sample to write to the + task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=bool) + + return self._interpreter.write_digital_lines( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_byte(self, data, timeout=10): + """ + Writes a single 8-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 8 lines per port. + + Args: + data (int): Specifies the 8-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=numpy.uint8) + + return self._interpreter.write_digital_u8( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_uint16(self, data, timeout=10): + """ + Writes a single 16-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 16 lines per port. + + Args: + data (int): Specifies the 16-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + numpy_array = numpy.asarray([data], dtype=numpy.uint16) + + return self._interpreter.write_digital_u16( + self._handle, 1, auto_start, timeout, FillMode.GROUP_BY_CHANNEL.value, numpy_array) + + def write_one_sample_port_uint32(self, data, timeout=10): + """ + Writes a single 32-bit unsigned integer sample to a single + digital output channel in a task. + + Use this method for devices with up to 32 lines per port. + + Args: + data (int): Specifies the 32-bit unsigned integer sample to + write to the task. + timeout (Optional[float]): Specifies the amount of time in + seconds to wait for the method to write all samples. + NI-DAQmx performs a timeout check only if the method + must wait before it writes data. This method returns an + error if the time elapses. The default timeout is 10 + seconds. If you set timeout to + nidaqmx.constants.WAIT_INFINITELY, the method waits + indefinitely. If you set timeout to 0, the method tries + once to write the submitted samples. If the method could + not write all the submitted samples, it returns an error + and the number of samples successfully written. + """ + auto_start = (self._auto_start if self._auto_start is not + AUTO_START_UNSET else True) + + return self._interpreter.write_digital_scalar_u32( + self._handle, auto_start, timeout, data)