ni / nidaqmx-python

A Python API for interacting with NI-DAQmx
Other
413 stars 155 forks source link

LibraryInterpreter.read_raw and write_raw do not allow switching array dtypes #578

Open bkeryan opened 2 months ago

bkeryan commented 2 months ago

LibraryInterpreter.read_raw sets cfunc.argtypes based on the dtype of the array you pass in:

    def read_raw(self, task, num_samps_per_chan, timeout, read_array):
        samples_read = ctypes.c_int()
        number_of_bytes_per_sample = ctypes.c_int()

        cfunc = lib_importer.windll.DAQmxReadRaw
        if cfunc.argtypes is None:
            with cfunc.arglock:
                if cfunc.argtypes is None:
                    cfunc.argtypes = [
                        lib_importer.task_handle, ctypes.c_int, ctypes.c_double,
                        wrapped_ndpointer(dtype=read_array.dtype, flags=('C', 'W')),
                        ctypes.c_uint, ctypes.POINTER(ctypes.c_int),
                        ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)]
        ...

However, this is incorrect because cfunc.argtypes is only initialized once.

I haven't tested this yet, but I expect it to remember the dtype of the 1st array that you pass to task.in_stream.read_into() and reject any array with a different dtype.

I think the correct way to make ndpointer accept any array dtype is to specify dtype=None.