Skip to content

Commit

Permalink
add new read and write exception classes (#156)
Browse files Browse the repository at this point in the history
* add new write exception class

* changelog

* add read exception and tests

* Contributing

* copy pasta
  • Loading branch information
zhindes authored Feb 22, 2022
1 parent b72380c commit d5f9b14
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ All notable changes to this project will be documented in this file.
* [131: task.write for COUNTER_OUTPUT - UsageTypeCO.PULSE_FREQUENCY has frequency and duty cycle reversed](https://github.com/ni/nidaqmx-python/issues/131)
* [124: nidaqmx_examples/system_properties.py errors out as of version 0.5.7](https://github.com/ni/nidaqmx-python/issues/124)
* [151: Write functions require writable numpy array](https://github.com/ni/nidaqmx-python/issues/151)
* [154: Problem with task.write() when not enough buffer free](https://github.com/ni/nidaqmx-python/issues/154)
* ### Major Changes
* Scrubbed all examples to ensure they all function correctly and use DAQmx best practices.
* Added `DaqReadError` and `DaqWriteError` subclasses of `DaqError` that provide important metadata for partial reads and writes.
* Linux is officially supported.

## 0.6.0
Expand Down
2 changes: 1 addition & 1 deletion nidaqmx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nidaqmx.errors import DaqError, DaqWarning, DaqResourceWarning
from nidaqmx.errors import DaqError, DaqReadError, DaqWriteError, DaqWarning, DaqResourceWarning
from nidaqmx.scale import Scale
from nidaqmx.task import Task
from nidaqmx._task_modules.read_functions import CtrFreq, CtrTick, CtrTime
Expand Down
34 changes: 17 additions & 17 deletions nidaqmx/_task_modules/read_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _read_analog_f_64(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand Down Expand Up @@ -72,7 +72,7 @@ def _read_binary_i_16(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -97,7 +97,7 @@ def _read_binary_u_16(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -122,7 +122,7 @@ def _read_binary_i_32(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -147,7 +147,7 @@ def _read_binary_u_32(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -172,7 +172,7 @@ def _read_digital_u_8(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -197,7 +197,7 @@ def _read_digital_u_16(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -222,7 +222,7 @@ def _read_digital_u_32(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand Down Expand Up @@ -267,7 +267,7 @@ def _read_digital_lines(
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read),
ctypes.byref(num_bytes_per_samp), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

ReadDigitalLinesReturnData = (
collections.namedtuple(
Expand Down Expand Up @@ -295,7 +295,7 @@ def _read_counter_f_64(task_handle, read_array, num_samps_per_chan, timeout):
task_handle, num_samps_per_chan, timeout,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -317,7 +317,7 @@ def _read_counter_u_32(task_handle, read_array, num_samps_per_chan, timeout):
task_handle, num_samps_per_chan, timeout,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -342,7 +342,7 @@ def _read_counter_f_64_ex(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -367,7 +367,7 @@ def _read_counter_u_32_ex(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand Down Expand Up @@ -429,7 +429,7 @@ def _read_ctr_freq(
task_handle, num_samps_per_chan, timeout, interleaved.value,
freq, duty_cycle, numpy.prod(freq.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -455,7 +455,7 @@ def _read_ctr_time(
task_handle, num_samps_per_chan, timeout, interleaved.value,
high_time, low_time, numpy.prod(high_time.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand All @@ -481,7 +481,7 @@ def _read_ctr_ticks(
task_handle, num_samps_per_chan, timeout, interleaved.value,
high_tick, low_tick, numpy.prod(high_tick.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samps_per_chan_read.value)

return samps_per_chan_read.value

Expand Down Expand Up @@ -577,6 +577,6 @@ def _read_raw(task_handle, read_array, num_samps_per_chan, timeout):
task_handle, num_samps_per_chan, timeout, read_array,
read_array.nbytes, ctypes.byref(samples_read),
ctypes.byref(number_of_bytes_per_sample), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_read=samples_read.value)

return samples_read.value, number_of_bytes_per_sample.value
44 changes: 22 additions & 22 deletions nidaqmx/_task_modules/write_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _write_analog_f_64(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand Down Expand Up @@ -63,7 +63,7 @@ def _write_binary_i_16(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -87,7 +87,7 @@ def _write_binary_u_16(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -111,7 +111,7 @@ def _write_binary_i_32(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -135,7 +135,7 @@ def _write_binary_u_32(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -159,7 +159,7 @@ def _write_digital_u_8(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -183,7 +183,7 @@ def _write_digital_u_16(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand All @@ -207,7 +207,7 @@ def _write_digital_u_32(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value

Expand Down Expand Up @@ -245,15 +245,15 @@ def _write_digital_lines(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value


def _write_ctr_freq(
task_handle, freq, duty_cycle, num_samps_per_chan, auto_start, timeout,
data_layout=FillMode.GROUP_BY_CHANNEL):
num_samps_per_chan_written = ctypes.c_int()
samps_per_chan_written = ctypes.c_int()

cfunc = lib_importer.windll.DAQmxWriteCtrFreq
if cfunc.argtypes is None:
Expand All @@ -269,10 +269,10 @@ def _write_ctr_freq(
error_code = cfunc(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, freq, duty_cycle,
ctypes.byref(num_samps_per_chan_written), None)
check_for_error(error_code)
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return num_samps_per_chan_written.value
return samps_per_chan_written.value


def _write_ctr_freq_scalar(task_handle, freq, duty_cycle, auto_start, timeout):
Expand All @@ -292,7 +292,7 @@ def _write_ctr_freq_scalar(task_handle, freq, duty_cycle, auto_start, timeout):
def _write_ctr_time(
task_handle, high_time, low_time, num_samps_per_chan, auto_start,
timeout, data_layout=FillMode.GROUP_BY_CHANNEL):
num_samps_per_chan_written = ctypes.c_int()
samps_per_chan_written = ctypes.c_int()

cfunc = lib_importer.windll.DAQmxWriteCtrTime
if cfunc.argtypes is None:
Expand All @@ -308,10 +308,10 @@ def _write_ctr_time(
error_code = cfunc(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, high_time, low_time,
ctypes.byref(num_samps_per_chan_written), None)
check_for_error(error_code)
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return num_samps_per_chan_written.value
return samps_per_chan_written.value


def _write_ctr_time_scalar(
Expand All @@ -334,7 +334,7 @@ def _write_ctr_time_scalar(
def _write_ctr_ticks(
task_handle, high_tick, low_tick, num_samps_per_chan, auto_start,
timeout, data_layout=FillMode.GROUP_BY_CHANNEL):
num_samps_per_chan_written = ctypes.c_int()
samps_per_chan_written = ctypes.c_int()

cfunc = lib_importer.windll.DAQmxWriteCtrTicks
if cfunc.argtypes is None:
Expand All @@ -350,10 +350,10 @@ def _write_ctr_ticks(
error_code = cfunc(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, high_tick, low_tick,
ctypes.byref(num_samps_per_chan_written), None)
check_for_error(error_code)
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return num_samps_per_chan_written.value
return samps_per_chan_written.value


def _write_ctr_ticks_scalar(
Expand Down Expand Up @@ -389,6 +389,6 @@ def _write_raw(
error_code = cfunc(
task_handle, num_samps_per_chan, auto_start, timeout, numpy_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
check_for_error(error_code, samps_per_chan_written=samps_per_chan_written.value)

return samps_per_chan_written.value
Loading

0 comments on commit d5f9b14

Please sign in to comment.