clade / PyDAQmx

Interface to National Instrument NIDAQmx driver
Other
133 stars 55 forks source link

Task methods return None, so you can't get the size needed for a buffer. #61

Closed javinst closed 4 years ago

javinst commented 4 years ago

The method versions in the Task class of the DAQmx functions don't return, which is generally OK, since PyDAQmx already takes care of errors or warnings, but that doesn't allow to get the size of the buffer for functions with data and bufferSize via catch_error_buffer.

import PyDAQmx
import ctypes
out=ctypes.create_string_buffer(256)
task=PyDAQmx.Task()
n=task.GetTaskName(None,0)
print(n)

None
n=PyDAQmx.GetTaskName(task.taskHandle,None,0)
print(n)

16

So both versions don't have the same behaviour, but if we give the buffer, both fill it properly:


n=task.GetTaskName(out,len(out))

print(n)
print(out.value.decode())

None
_unnamedTask<3>

n=PyDAQmx.DAQmxGetTaskName(task.taskHandle,out,len(out))

print(n)
print(out.value.decode())

0
_unnamedTask<3>

Therefore is more an issue of consistency, since if one changes code to the object oriented interface, it could be confusing to found that the behaviour changes. I don't know if a modification so the methods return the error code could generate issues downstream, but since right now they not return (return None) I don't really see any issues if that changes:

for function_name in task_function_list:
        name = function_name[5:] # remove the DAQmx in front of the name
        func = getattr(DAQmxFunctions, function_name)
        arg_names = function_dict[function_name]['arg_name']
        doc = 'T.%s(%s) -> error.' %(name, ', '.join(arg_names[1:]))
        cmd = """def {0}(self, {1}):
        "{3}"
        return {2}(self.taskHandle, {1})"""
        exec(cmd.format(name, ', '.join(arg_names[1:]), function_name, doc))    

instead of the current:

for function_name in task_function_list:
        name = function_name[5:] # remove the DAQmx in front of the name
        func = getattr(DAQmxFunctions, function_name)
        arg_names = function_dict[function_name]['arg_name']
        doc = 'T.%s(%s) -> error.' %(name, ', '.join(arg_names[1:]))
        cmd = """def {0}(self, {1}):
        "{3}"
        {2}(self.taskHandle, {1})"""
        exec(cmd.format(name, ', '.join(arg_names[1:]), function_name, doc))    
clade commented 4 years ago

Thanks for your message. This is now implemented in the dev branch. Commit 669a3f49c64cc3544237c52ee5a124121a726bbb