clade / PyDAQmx

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

Convert all the DAQmxError.* constants into DAQmxError subclasses #35

Closed eric-wieser closed 8 years ago

eric-wieser commented 8 years ago

Something like:

class DAQException(Exception): pass
class DAQError(DAQException):
    _by_code = {}
class DAQWarning(Warning, DAQException):
    _by_code = {}

for c in constant_list:
    code = getattr(daq_constants, c)
    if c.startswith('DAQmxError'):
         errname = c[10:]
         globals()[errname + 'Error'] = type(errname + 'Error', (DAQmxError,), dict(code=code})
    elif c.startswith('DAQmxWarning'):
         errname = c[12:]
         globals()[errname + 'Warning'] = type(errname + 'Warning', (DAQWarning,), dict(code=code})

With the following changed error wrapper:

    def catch_error_default(f):
        def mafunction(*arg):
            error = f(*arg)
            if error<0:
                errBuff = ctypes.create_string_buffer(2048)
                mx.DAQmxGetExtendedErrorInfo(errBuff, 2048)
                raise DAQError._by_code[error](errBuff.value.decode("utf-8"))
            elif error>0:
                errBuff = ctypes.create_string_buffer(2048)
                mx.DAQmxGetErrorString(error, errBuff, 2048);
                warnings.warn(DAQWarning._by_code[error](errBuff.value.decode("utf-8")))
clade commented 8 years ago

Commit https://github.com/clade/PyDAQmx/commit/b6679c9 solve this issue.

Errors are defined in the DAQmxFunctions module and can be imported from the PyDAQmx package (for example PyDAQmx.InvalidDeviceIDError).

eric-wieser commented 8 years ago

Nice!