From b6d779136851753af654f7112c3677bc48b4bd97 Mon Sep 17 00:00:00 2001 From: clade Date: Mon, 18 Oct 2021 18:20:44 +0200 Subject: [PATCH] All files compatible with Python 2 and Python 3 (remove 2to3) --- PyDAQmx/DAQmxCallBack.py | 94 +++++----- PyDAQmx/DAQmxConfig.py | 4 +- PyDAQmx/DAQmxConstants.py | 4 +- PyDAQmx/DAQmxFunctions.py | 8 +- PyDAQmx/DAQmxTypes.py | 2 +- PyDAQmx/Task.py | 10 +- PyDAQmx/__init__.py | 16 +- PyDAQmx/example/AnalogInput_acq_IntClk.py | 176 +++++++++--------- .../example/ContinuousPulseTrainGeneration.py | 82 ++++---- PyDAQmx/example/__init__.py | 8 +- PyDAQmx/example/callback.py | 128 ++++++------- PyDAQmx/example/callback_Task.py | 82 ++++---- PyDAQmx/example/callback_Task_synchronous.py | 6 +- PyDAQmx/example/callback_oo.py | 130 ++++++------- PyDAQmx/example/callback_test_unregister.py | 118 ++++++------ PyDAQmx/example/test/__init__.py | 2 +- setup.py | 5 +- 17 files changed, 438 insertions(+), 437 deletions(-) diff --git a/PyDAQmx/DAQmxCallBack.py b/PyDAQmx/DAQmxCallBack.py index 6d19695..fcc51c4 100644 --- a/PyDAQmx/DAQmxCallBack.py +++ b/PyDAQmx/DAQmxCallBack.py @@ -1,47 +1,47 @@ -import weakref -import DAQmxConfig - -""" This package import helper functions to pass data to call back functions - -See examples -""" - -#if DAQmxConfig.NIDAQmxBase: -# raise NotImplementedError, 'Call back function are not available with the NIDAQmxBase driver' - -# NIDAQmx allow to pass a pointer to a data structure in callback function -# If this function is implemented in Python, we would like this data to be a Python object. -# This object will not (and will not) be used in C but we want to pass a reference to it. -# A workaround is to use the weakref module -# -# Define a weakref dictionnary to be able to remember objects and retrieve them by ID -_id2obj_dict = weakref.WeakValueDictionary() - -def create_callbackdata_id(obj): - """ - Uses the weakref module to create and store a reference to obj - - output value: reference to the object - - It is not possible to directly uses python object through a Callback function because - with ctypes there is no pointer to python object. - This function store in a dictionary a reference to an object - This object can be retrieved using the get_callbackdata_from_id function - - For python object that cannot be weakreferenced, one can creat a dummy class to wrap - the python object : - def MyList(list) - pass - - data = MyList() - id = create_callbackdata_id(data) - - """ - oid = id(obj) - _id2obj_dict[oid] = obj - return oid - -def get_callbackdata_from_id(oid): - """Retrieve an object stored using create_callbackdata_id - """ - return _id2obj_dict[oid] +import weakref +from . import DAQmxConfig + +""" This package import helper functions to pass data to call back functions + +See examples +""" + +#if DAQmxConfig.NIDAQmxBase: +# raise NotImplementedError, 'Call back function are not available with the NIDAQmxBase driver' + +# NIDAQmx allow to pass a pointer to a data structure in callback function +# If this function is implemented in Python, we would like this data to be a Python object. +# This object will not (and will not) be used in C but we want to pass a reference to it. +# A workaround is to use the weakref module +# +# Define a weakref dictionnary to be able to remember objects and retrieve them by ID +_id2obj_dict = weakref.WeakValueDictionary() + +def create_callbackdata_id(obj): + """ + Uses the weakref module to create and store a reference to obj + + output value: reference to the object + + It is not possible to directly uses python object through a Callback function because + with ctypes there is no pointer to python object. + This function store in a dictionary a reference to an object + This object can be retrieved using the get_callbackdata_from_id function + + For python object that cannot be weakreferenced, one can creat a dummy class to wrap + the python object : + def MyList(list) + pass + + data = MyList() + id = create_callbackdata_id(data) + + """ + oid = id(obj) + _id2obj_dict[oid] = obj + return oid + +def get_callbackdata_from_id(oid): + """Retrieve an object stored using create_callbackdata_id + """ + return _id2obj_dict[oid] diff --git a/PyDAQmx/DAQmxConfig.py b/PyDAQmx/DAQmxConfig.py index 1048ba2..0757531 100644 --- a/PyDAQmx/DAQmxConfig.py +++ b/PyDAQmx/DAQmxConfig.py @@ -92,7 +92,7 @@ def get_lib(): # If the DAQmxConfigTest has been imported, then uses the value from this file # This can be used to try different version or compile the module on a plateform where # DAQmx is not installed -if "DAQmxConfigTest" in sys.modules.keys(): +if "DAQmxConfigTest" in list(sys.modules.keys()): from DAQmxConfigTest import * if lib_name is None: def get_lib(): @@ -104,5 +104,5 @@ def __getattr__(self, name): return DAQlib, DAQlib_variadic if dot_h_file is None or not os.path.exists(dot_h_file): - raise NotImplementedError, "Location of niDAQmx library and include file unknown on %s - if you find out, please let the PyDAQmx project know" % (sys.platform) + raise NotImplementedError("Location of niDAQmx library and include file unknown on %s - if you find out, please let the PyDAQmx project know" % (sys.platform)) diff --git a/PyDAQmx/DAQmxConstants.py b/PyDAQmx/DAQmxConstants.py index 747992b..521e7de 100644 --- a/PyDAQmx/DAQmxConstants.py +++ b/PyDAQmx/DAQmxConstants.py @@ -4,7 +4,7 @@ """ import re -from DAQmxConfig import dot_h_file +from .DAQmxConfig import dot_h_file include_file = open(dot_h_file,'r') #Open NIDAQmx.h file @@ -18,7 +18,7 @@ for copyright_line in preamble: if "Copyright" in copyright_line: - DAQmx_copyright_year = max(map(int, re.findall('\d\d\d\d', copyright_line))) + DAQmx_copyright_year = max([int(elm) for elm in re.findall('\d\d\d\d', copyright_line)]) break else: DAQmx_copyright_year = 2003 diff --git a/PyDAQmx/DAQmxFunctions.py b/PyDAQmx/DAQmxFunctions.py index cd6da27..52526bc 100644 --- a/PyDAQmx/DAQmxFunctions.py +++ b/PyDAQmx/DAQmxFunctions.py @@ -3,10 +3,10 @@ from ctypes import * import warnings -import DAQmxConfig -from DAQmxTypes import * -from DAQmxConstants import constant_list -import DAQmxConstants +from . import DAQmxConfig +from .DAQmxTypes import * +from .DAQmxConstants import constant_list +from . import DAQmxConstants class DAQException(Exception): """Exception raised from the NIDAQ. diff --git a/PyDAQmx/DAQmxTypes.py b/PyDAQmx/DAQmxTypes.py index 55929c7..86931ce 100644 --- a/PyDAQmx/DAQmxTypes.py +++ b/PyDAQmx/DAQmxTypes.py @@ -2,7 +2,7 @@ from ctypes import * import ctypes -from DAQmxConstants import DAQmx_copyright_year +from .DAQmxConstants import DAQmx_copyright_year # New types definitions # Correspondance between the name used in the NiDAQmx.h file and ctypes diff --git a/PyDAQmx/Task.py b/PyDAQmx/Task.py index 60868c2..2c4d55f 100644 --- a/PyDAQmx/Task.py +++ b/PyDAQmx/Task.py @@ -1,6 +1,6 @@ -from DAQmxTypes import TaskHandle -import DAQmxFunctions -from DAQmxFunctions import * +from .DAQmxTypes import TaskHandle +from . import DAQmxFunctions +from .DAQmxFunctions import * import ctypes @@ -17,7 +17,7 @@ task_function_list = [name for name in task_function_list if name not in ['DAQmxClearTask']] try : - from DAQmxCallBack import * + from .DAQmxCallBack import * _callback = True except NotImplementedError: _callback = False @@ -98,7 +98,7 @@ def SignalCallback_py(taskHandle, signalID, self_id): class CallbackParent(object): def __getattr__(self, name): if name in ['AutoRegisterEveryNSamplesEvent', 'AutoRegisterDoneEvent', 'AutoRegisterSignalEvent']: - raise NotImplementedError, 'Callback methods are not available' + raise NotImplementedError('Callback methods are not available') return super(CallbackParent, self).__getattr__(name) diff --git a/PyDAQmx/__init__.py b/PyDAQmx/__init__.py index 7773841..2ce0778 100644 --- a/PyDAQmx/__init__.py +++ b/PyDAQmx/__init__.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- -import DAQmxConfig +from . import DAQmxConfig -from DAQmxTypes import * -from DAQmxConstants import * -from DAQmxFunctions import * -from Task import Task +from .DAQmxTypes import * +from .DAQmxConstants import * +from .DAQmxFunctions import * +from .Task import Task -import DAQmxConstants -import DAQmxFunctions +from . import DAQmxConstants +from . import DAQmxFunctions all_types = ['int8', 'uInt8', 'int16', 'uInt16', 'int32', 'uInt32', 'float32', 'float64', 'int64', 'uInt64', 'bool32', 'TaskHandle', 'CalHandle', 'DAQmxEveryNSamplesEventCallbackPtr', 'DAQmxDoneEventCallbackPtr', 'DAQmxSignalEventCallbackPtr', 'CtypesString'] @@ -24,7 +24,7 @@ globals()[new_name] = globals()[name] ##### WARNING, version should also be modified in the setup.py -__version_info__ = (1, 4, 4) +__version_info__ = (1, 4, 5) __version__ = '.'.join(str(num) for num in __version_info__) __author__ =u'Pierre Cladé' diff --git a/PyDAQmx/example/AnalogInput_acq_IntClk.py b/PyDAQmx/example/AnalogInput_acq_IntClk.py index 6ff8a8d..94f47c9 100644 --- a/PyDAQmx/example/AnalogInput_acq_IntClk.py +++ b/PyDAQmx/example/AnalogInput_acq_IntClk.py @@ -1,88 +1,88 @@ -import PyDAQmx -from PyDAQmx import Task, DAQmxResetDevice, int32 -from ctypes import byref -import numpy - - -class AIParameters(object): - limits = (-10, 10) - physicalChannel = ["/Dev1/ai0"] - - def __init__(self, sample_rate, sample_number, channels=None, limits=None): - self.sampleRate = sample_rate - self.sampleNumber = sample_number - if limits is None: - limits = self.limits - self.limit_inf= limits[0] - self.limit_sup= limits[1] - if channels is not None: - if type(channels) is str: - physicalChannel = [channels] - self.physicalChannel = channels - - @property - def device_name(self): - device_name = self.physicalChannel[0].split('/')[0] - if device_name == '' : - device_name = self.physicalChannel[0].split('/')[1] - return device_name - -class Trigger(object): - def __init__(self, terminal): - self.terminal = terminal - -class RisingTrigger(Trigger): - direction = PyDAQmx.DAQmx_Val_Rising - -class FallingTrigger(Trigger): - direction = PyDAQmx.DAQmx_Val_Falling - - -class AIVoltageChan(Task): - def __init__(self, ai_param, reset=True, terminalConfig=PyDAQmx.DAQmx_Val_RSE, trigger=None): - if reset: - DAQmxResetDevice(ai_param.device_name) - super(AIVoltageChan, self).__init__() - self.sampleNumber = ai_param.sampleNumber - self.sampleRate = ai_param.sampleRate - self.limit_inf = ai_param.limit_inf - self.limit_sup = ai_param.limit_sup - self.physicalChannel = ai_param.physicalChannel - self.numberOfChannel = len(ai_param.physicalChannel) - if isinstance(terminalConfig, basestring): - terminalConfig = getattr(PyDAQmx, terminalConfig) - self.terminalConfig = terminalConfig - self.trigger = trigger - self.configure() - def configure(self): - channel_string = ','.join(self.physicalChannel) - self.CreateAIVoltageChan(channel_string,"",self.terminalConfig, - self.limit_inf,self.limit_sup, - PyDAQmx.DAQmx_Val_Volts,None) - def start(self): - n = self.sampleNumber - sampleRate = self.sampleRate - self.CfgSampClkTiming("",sampleRate,PyDAQmx.DAQmx_Val_Rising,PyDAQmx.DAQmx_Val_FiniteSamps,n) - if self.trigger is not None: - self.CfgDigEdgeRefTrig(self.trigger.terminal,self.trigger.direction,10) - self.StartTask() - def read(self): - n = self.sampleNumber - data = numpy.zeros((n,self.numberOfChannel), dtype=numpy.float64) - read = int32() - self.ReadAnalogF64(n,10.0,PyDAQmx.DAQmx_Val_GroupByScanNumber,data,n*self.numberOfChannel,byref(read),None) - return data - def stop(self): - self.StopTask() - def wait(self, timeout=10): - self.WaitUntilTaskDone(timeout) - - -if __name__=="__main__": - ai = AIVoltageChan(ai_param=AIParameters(100000, 10000, ['/dev1/ai0', '/dev1/ai1']), - terminalConfig="DAQmx_Val_PseudoDiff", - trigger=RisingTrigger('/dev1/PFI0')) - ai.start() - ai.wait() - ai.read() - ai.stop() +import PyDAQmx +from PyDAQmx import Task, DAQmxResetDevice, int32 +from ctypes import byref +import numpy + + +class AIParameters(object): + limits = (-10, 10) + physicalChannel = ["/Dev1/ai0"] + + def __init__(self, sample_rate, sample_number, channels=None, limits=None): + self.sampleRate = sample_rate + self.sampleNumber = sample_number + if limits is None: + limits = self.limits + self.limit_inf= limits[0] + self.limit_sup= limits[1] + if channels is not None: + if type(channels) is str: + physicalChannel = [channels] + self.physicalChannel = channels + + @property + def device_name(self): + device_name = self.physicalChannel[0].split('/')[0] + if device_name == '' : + device_name = self.physicalChannel[0].split('/')[1] + return device_name + +class Trigger(object): + def __init__(self, terminal): + self.terminal = terminal + +class RisingTrigger(Trigger): + direction = PyDAQmx.DAQmx_Val_Rising + +class FallingTrigger(Trigger): + direction = PyDAQmx.DAQmx_Val_Falling + + +class AIVoltageChan(Task): + def __init__(self, ai_param, reset=True, terminalConfig=PyDAQmx.DAQmx_Val_RSE, trigger=None): + if reset: + DAQmxResetDevice(ai_param.device_name) + super(AIVoltageChan, self).__init__() + self.sampleNumber = ai_param.sampleNumber + self.sampleRate = ai_param.sampleRate + self.limit_inf = ai_param.limit_inf + self.limit_sup = ai_param.limit_sup + self.physicalChannel = ai_param.physicalChannel + self.numberOfChannel = len(ai_param.physicalChannel) + if isinstance(terminalConfig, str): + terminalConfig = getattr(PyDAQmx, terminalConfig) + self.terminalConfig = terminalConfig + self.trigger = trigger + self.configure() + def configure(self): + channel_string = ','.join(self.physicalChannel) + self.CreateAIVoltageChan(channel_string,"",self.terminalConfig, + self.limit_inf,self.limit_sup, + PyDAQmx.DAQmx_Val_Volts,None) + def start(self): + n = self.sampleNumber + sampleRate = self.sampleRate + self.CfgSampClkTiming("",sampleRate,PyDAQmx.DAQmx_Val_Rising,PyDAQmx.DAQmx_Val_FiniteSamps,n) + if self.trigger is not None: + self.CfgDigEdgeRefTrig(self.trigger.terminal,self.trigger.direction,10) + self.StartTask() + def read(self): + n = self.sampleNumber + data = numpy.zeros((n,self.numberOfChannel), dtype=numpy.float64) + read = int32() + self.ReadAnalogF64(n,10.0,PyDAQmx.DAQmx_Val_GroupByScanNumber,data,n*self.numberOfChannel,byref(read),None) + return data + def stop(self): + self.StopTask() + def wait(self, timeout=10): + self.WaitUntilTaskDone(timeout) + + +if __name__=="__main__": + ai = AIVoltageChan(ai_param=AIParameters(100000, 10000, ['/dev1/ai0', '/dev1/ai1']), + terminalConfig="DAQmx_Val_PseudoDiff", + trigger=RisingTrigger('/dev1/PFI0')) + ai.start() + ai.wait() + ai.read() + ai.stop() diff --git a/PyDAQmx/example/ContinuousPulseTrainGeneration.py b/PyDAQmx/example/ContinuousPulseTrainGeneration.py index 3d3c33d..b8ae00b 100644 --- a/PyDAQmx/example/ContinuousPulseTrainGeneration.py +++ b/PyDAQmx/example/ContinuousPulseTrainGeneration.py @@ -1,41 +1,41 @@ -# coding= latin-1 - -from PyDAQmx.DAQmxFunctions import * -from PyDAQmx.DAQmxConstants import * - -class ContinuousPulseTrainGeneration(): - """ Class to create a continuous pulse train on a counter - - Usage: pulse = ContinuousTrainGeneration(period [s], - duty_cycle (default = 0.5), counter (default = "dev1/ctr0"), - reset = True/False) - pulse.start() - pulse.stop() - pulse.clear() - """ - def __init__(self, period=1., duty_cycle=0.5, counter="Dev1/ctr0", reset=False): - if reset: - DAQmxResetDevice(counter.split('/')[0]) - taskHandle = TaskHandle(0) - DAQmxCreateTask("",byref(taskHandle)) - DAQmxCreateCOPulseChanFreq(taskHandle,counter,"",DAQmx_Val_Hz,DAQmx_Val_Low, - 0.0,1/float(period),duty_cycle) - DAQmxCfgImplicitTiming(taskHandle,DAQmx_Val_ContSamps,1000) - self.taskHandle = taskHandle - def start(self): - DAQmxStartTask(self.taskHandle) - def stop(self): - DAQmxStopTask(self.taskHandle) - def clear(self): - DAQmxClearTask(self.taskHandle) - - -if __name__=="__main__": - pulse_gene1 = ContinuousPulseTrainGeneration(1.,0.5, "dev1/ctr0", reset=True) - pulse_gene1.start() - a = raw_input("Generating pulse train. Press Enter to interrupt\n") - pulse_gene1.stop() - pulse_gene1.clear() - - - +# coding= latin-1 + +from PyDAQmx.DAQmxFunctions import * +from PyDAQmx.DAQmxConstants import * + +class ContinuousPulseTrainGeneration(): + """ Class to create a continuous pulse train on a counter + + Usage: pulse = ContinuousTrainGeneration(period [s], + duty_cycle (default = 0.5), counter (default = "dev1/ctr0"), + reset = True/False) + pulse.start() + pulse.stop() + pulse.clear() + """ + def __init__(self, period=1., duty_cycle=0.5, counter="Dev1/ctr0", reset=False): + if reset: + DAQmxResetDevice(counter.split('/')[0]) + taskHandle = TaskHandle(0) + DAQmxCreateTask("",byref(taskHandle)) + DAQmxCreateCOPulseChanFreq(taskHandle,counter,"",DAQmx_Val_Hz,DAQmx_Val_Low, + 0.0,1/float(period),duty_cycle) + DAQmxCfgImplicitTiming(taskHandle,DAQmx_Val_ContSamps,1000) + self.taskHandle = taskHandle + def start(self): + DAQmxStartTask(self.taskHandle) + def stop(self): + DAQmxStopTask(self.taskHandle) + def clear(self): + DAQmxClearTask(self.taskHandle) + + +if __name__=="__main__": + pulse_gene1 = ContinuousPulseTrainGeneration(1.,0.5, "dev1/ctr0", reset=True) + pulse_gene1.start() + a = input("Generating pulse train. Press Enter to interrupt\n") + pulse_gene1.stop() + pulse_gene1.clear() + + + diff --git a/PyDAQmx/example/__init__.py b/PyDAQmx/example/__init__.py index ca38e34..90c9a34 100644 --- a/PyDAQmx/example/__init__.py +++ b/PyDAQmx/example/__init__.py @@ -1,4 +1,4 @@ -from ContinuousPulseTrainGeneration import ContinuousPulseTrainGeneration -from MultiChannelAnalogInput import MultiChannelAnalogInput -from callback_Task_synchronous import CallbackTaskSynchronous -from callback_test_unregister import CallbackWithUnregister +from .ContinuousPulseTrainGeneration import ContinuousPulseTrainGeneration +from .MultiChannelAnalogInput import MultiChannelAnalogInput +from .callback_Task_synchronous import CallbackTaskSynchronous +from .callback_test_unregister import CallbackWithUnregister diff --git a/PyDAQmx/example/callback.py b/PyDAQmx/example/callback.py index a959eed..ca485cb 100644 --- a/PyDAQmx/example/callback.py +++ b/PyDAQmx/example/callback.py @@ -1,64 +1,64 @@ -from PyDAQmx import * -from PyDAQmx.DAQmxCallBack import * -from numpy import zeros - -"""This example is a PyDAQmx version of the ContAcq_IntClk.c example -It illustrates the use of callback functions - -This example demonstrates how to acquire a continuous amount of -data using the DAQ device's internal clock. It incrementally stores the data -in a Python list. -""" - - - -# one cannot create a weakref to a list directly -# but the following works well -class MyList(list): - pass - -# list where the data are stored -data = MyList() -id_a = create_callbackdata_id(data) - - - -# Define two Call back functions -def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): - callbackdata = get_callbackdata_from_id(callbackData_ptr) - read = int32() - data = zeros(1000) - DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,byref(read),None) - callbackdata.extend(data.tolist()) - print "Acquired total %d samples"%len(data) - return 0 # The function should return an integer - -# Convert the python function to a CFunction -EveryNCallback = DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py) - -def DoneCallback_py(taskHandle, status, callbackData): - print "Status",status.value - return 0 # The function should return an integer - -# Convert the python function to a CFunction -DoneCallback = DAQmxDoneEventCallbackPtr(DoneCallback_py) - - -# Beginning of the script - -DAQmxResetDevice('dev1') - -taskHandle=TaskHandle() -DAQmxCreateTask("",byref(taskHandle)) -DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) -DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) - -DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a) -DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,None) - -DAQmxStartTask(taskHandle) - -raw_input('Acquiring samples continuously. Press Enter to interrupt\n') - -DAQmxStopTask(taskHandle) -DAQmxClearTask(taskHandle) +from PyDAQmx import * +from PyDAQmx.DAQmxCallBack import * +from numpy import zeros + +"""This example is a PyDAQmx version of the ContAcq_IntClk.c example +It illustrates the use of callback functions + +This example demonstrates how to acquire a continuous amount of +data using the DAQ device's internal clock. It incrementally stores the data +in a Python list. +""" + + + +# one cannot create a weakref to a list directly +# but the following works well +class MyList(list): + pass + +# list where the data are stored +data = MyList() +id_a = create_callbackdata_id(data) + + + +# Define two Call back functions +def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): + callbackdata = get_callbackdata_from_id(callbackData_ptr) + read = int32() + data = zeros(1000) + DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,byref(read),None) + callbackdata.extend(data.tolist()) + print("Acquired total %d samples"%len(data)) + return 0 # The function should return an integer + +# Convert the python function to a CFunction +EveryNCallback = DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py) + +def DoneCallback_py(taskHandle, status, callbackData): + print("Status",status.value) + return 0 # The function should return an integer + +# Convert the python function to a CFunction +DoneCallback = DAQmxDoneEventCallbackPtr(DoneCallback_py) + + +# Beginning of the script + +DAQmxResetDevice('dev1') + +taskHandle=TaskHandle() +DAQmxCreateTask("",byref(taskHandle)) +DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) +DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) + +DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a) +DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,None) + +DAQmxStartTask(taskHandle) + +input('Acquiring samples continuously. Press Enter to interrupt\n') + +DAQmxStopTask(taskHandle) +DAQmxClearTask(taskHandle) diff --git a/PyDAQmx/example/callback_Task.py b/PyDAQmx/example/callback_Task.py index 49aab65..4220975 100644 --- a/PyDAQmx/example/callback_Task.py +++ b/PyDAQmx/example/callback_Task.py @@ -1,41 +1,41 @@ -from PyDAQmx import * -from PyDAQmx.DAQmxCallBack import * -from numpy import zeros - -"""This example is a PyDAQmx version of the ContAcq_IntClk.c example -It illustrates the use of callback functions - -This example demonstrates how to acquire a continuous amount of -data using the DAQ device's internal clock. It incrementally stores the data -in a Python list. - -This example is also an example of the object oriented uses of PyDAQmx -""" - -class CallbackTask(Task): - def __init__(self): - Task.__init__(self) - self.data = zeros(1000) - self.a = [] - self.CreateAIVoltageChan("Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) - self.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) - self.AutoRegisterEveryNSamplesEvent(DAQmx_Val_Acquired_Into_Buffer,1000,0) - self.AutoRegisterDoneEvent(0) - def EveryNCallback(self): - read = int32() - self.ReadAnalogF64(1000,10.0,DAQmx_Val_GroupByScanNumber,self.data,1000,byref(read),None) - self.a.extend(self.data.tolist()) - print self.data[0] - return 0 # The function should return an integer - def DoneCallback(self, status): - print "Status",status.value - return 0 # The function should return an integer - - -task=CallbackTask() -task.StartTask() - -raw_input('Acquiring samples continuously. Press Enter to interrupt\n') - -task.StopTask() -task.ClearTask() +from PyDAQmx import * +from PyDAQmx.DAQmxCallBack import * +from numpy import zeros + +"""This example is a PyDAQmx version of the ContAcq_IntClk.c example +It illustrates the use of callback functions + +This example demonstrates how to acquire a continuous amount of +data using the DAQ device's internal clock. It incrementally stores the data +in a Python list. + +This example is also an example of the object oriented uses of PyDAQmx +""" + +class CallbackTask(Task): + def __init__(self): + Task.__init__(self) + self.data = zeros(1000) + self.a = [] + self.CreateAIVoltageChan("Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) + self.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) + self.AutoRegisterEveryNSamplesEvent(DAQmx_Val_Acquired_Into_Buffer,1000,0) + self.AutoRegisterDoneEvent(0) + def EveryNCallback(self): + read = int32() + self.ReadAnalogF64(1000,10.0,DAQmx_Val_GroupByScanNumber,self.data,1000,byref(read),None) + self.a.extend(self.data.tolist()) + print(self.data[0]) + return 0 # The function should return an integer + def DoneCallback(self, status): + print("Status",status.value) + return 0 # The function should return an integer + + +task=CallbackTask() +task.StartTask() + +input('Acquiring samples continuously. Press Enter to interrupt\n') + +task.StopTask() +task.ClearTask() diff --git a/PyDAQmx/example/callback_Task_synchronous.py b/PyDAQmx/example/callback_Task_synchronous.py index 6b918c8..843e561 100644 --- a/PyDAQmx/example/callback_Task_synchronous.py +++ b/PyDAQmx/example/callback_Task_synchronous.py @@ -33,7 +33,7 @@ def EveryNCallback(self): self._newdata_event.set() return 0 # The function should return an integer def DoneCallback(self, status): - print "Status",status.value + print("Status",status.value) return 0 # The function should return an integer def get_data(self, blocking=True, timeout=None): if blocking: @@ -48,10 +48,10 @@ def get_data(self, blocking=True, timeout=None): task=CallbackTaskSynchronous() task.StartTask() - print "Acquiring 10 * 1000 samples in continuous mode." + print("Acquiring 10 * 1000 samples in continuous mode.") for _ in range(10): task.get_data(timeout=10.0) - print "Acquired %d points" % task.read.value + print("Acquired %d points" % task.read.value) task.StopTask() task.ClearTask() diff --git a/PyDAQmx/example/callback_oo.py b/PyDAQmx/example/callback_oo.py index fc718e3..4cafcab 100644 --- a/PyDAQmx/example/callback_oo.py +++ b/PyDAQmx/example/callback_oo.py @@ -1,65 +1,65 @@ -from PyDAQmx import * -from PyDAQmx.DAQmxCallBack import * -from numpy import zeros - -"""This example is a PyDAQmx version of the ContAcq_IntClk.c example -It illustrates the use of callback functions - -This example demonstrates how to acquire a continuous amount of -data using the DAQ device's internal clock. It incrementally store the data -in a Python list. - -This example is also an example of the object oriented uses of PyDAQmx -""" - - - -# one cannot create a weakref to a list directly -# but the following works well -class MyList(list): - pass - -# list where the data are stored -data = MyList() -id_a = create_callbackdata_id(data) - - - -# Define two Call back functions -def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): - callbackdata = get_callbackdata_from_id(callbackData_ptr) - read = int32() - data = zeros(1000) - DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,byref(read),None) - callbackdata.extend(data.tolist()) - print "Acquired total %d samples"%len(callbackdata) - return 0 # The function should return an integer - -# Convert the python function to a CFunction -EveryNCallback = DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py) - -def DoneCallback_py(taskHandle, status, callbackData): - print "Status",status.value - return 0 # The function should return an integer - -# Convert the python function to a CFunction -DoneCallback = DAQmxDoneEventCallbackPtr(DoneCallback_py) - - -# Beginning of the script - -#DAQmxResetDevice('dev1') - -task=Task() -task.CreateAIVoltageChan("Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) -task.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) - -task.RegisterEveryNSamplesEvent(DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a) -task.RegisterDoneEvent(0,DoneCallback,None) - -task.StartTask() - -raw_input('Acquiring samples continuously. Press Enter to interrupt\n') - -task.StopTask() -task.ClearTask() +from PyDAQmx import * +from PyDAQmx.DAQmxCallBack import * +from numpy import zeros + +"""This example is a PyDAQmx version of the ContAcq_IntClk.c example +It illustrates the use of callback functions + +This example demonstrates how to acquire a continuous amount of +data using the DAQ device's internal clock. It incrementally store the data +in a Python list. + +This example is also an example of the object oriented uses of PyDAQmx +""" + + + +# one cannot create a weakref to a list directly +# but the following works well +class MyList(list): + pass + +# list where the data are stored +data = MyList() +id_a = create_callbackdata_id(data) + + + +# Define two Call back functions +def EveryNCallback_py(taskHandle, everyNsamplesEventType, nSamples, callbackData_ptr): + callbackdata = get_callbackdata_from_id(callbackData_ptr) + read = int32() + data = zeros(1000) + DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,byref(read),None) + callbackdata.extend(data.tolist()) + print("Acquired total %d samples"%len(callbackdata)) + return 0 # The function should return an integer + +# Convert the python function to a CFunction +EveryNCallback = DAQmxEveryNSamplesEventCallbackPtr(EveryNCallback_py) + +def DoneCallback_py(taskHandle, status, callbackData): + print("Status",status.value) + return 0 # The function should return an integer + +# Convert the python function to a CFunction +DoneCallback = DAQmxDoneEventCallbackPtr(DoneCallback_py) + + +# Beginning of the script + +#DAQmxResetDevice('dev1') + +task=Task() +task.CreateAIVoltageChan("Dev1/ai0","",DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,None) +task.CfgSampClkTiming("",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) + +task.RegisterEveryNSamplesEvent(DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,id_a) +task.RegisterDoneEvent(0,DoneCallback,None) + +task.StartTask() + +input('Acquiring samples continuously. Press Enter to interrupt\n') + +task.StopTask() +task.ClearTask() diff --git a/PyDAQmx/example/callback_test_unregister.py b/PyDAQmx/example/callback_test_unregister.py index e4fc244..072ffc8 100644 --- a/PyDAQmx/example/callback_test_unregister.py +++ b/PyDAQmx/example/callback_test_unregister.py @@ -1,59 +1,59 @@ -# -*- coding: utf-8 -*- -import PyDAQmx as daqmx -from PyDAQmx.DAQmxCallBack import * -import numpy as np -from ctypes import cast - -# Sample code to test changing the callback function for a daqmx acquisition -# task. -# -# Author: Erik Matlin, 9/9/2014 -# Modified by Pierre Cladé 23/9/2014 - -class CallbackWithUnregister(object): - def __init__(self, device, nSamples=1000): - self.task = daqmx.Task() - self.task.EveryNCallback1 = self.EveryNCallback1 - self.task.EveryNCallback2 = self.EveryNCallback2 - - self.task.CreateAIVoltageChan("/".join((device,'ai0')), "a", - daqmx.DAQmx_Val_Cfg_Default, -10, 10, daqmx.DAQmx_Val_Volts, None) - - - def start(self, nSamples, callbackname): - # configure timing - self.task.CfgSampClkTiming("", 10000, daqmx.DAQmx_Val_Rising, - daqmx.DAQmx_Val_ContSamps, nSamples) - self.nSamples = nSamples - - self.task.AutoRegisterEveryNSamplesEvent(daqmx.DAQmx_Val_Acquired_Into_Buffer, nSamples, 0, name=callbackname) - self.task.StartTask() - - def stop(self): - self.task.StopTask() - - def EveryNCallback1(self): - read = daqmx.int32(0) - data = np.zeros(self.nSamples) - self.task.ReadAnalogF64(self.nSamples, 1.0, - daqmx.DAQmx_Val_GroupByScanNumber, data, data.size, - daqmx.byref(read), None) - - def EveryNCallback2(self): - read = daqmx.int32(0) - data = np.zeros(self.nSamples) - self.task.ReadAnalogF64(self.nSamples, 1.0, - daqmx.DAQmx_Val_GroupByScanNumber, data, data.size, - daqmx.byref(read), None) - - -if __name__ == '__main__': - from time import sleep - b = CallbackWithUnregister("TestDevice") - for i,nSamples in enumerate([1000, 2000, 5000]): - func_name = 'EveryNCallback{0}'.format([1,2][i%2]) - b.start(nSamples, func_name) - print "started!" - sleep(.5) - b.stop() - print "stop" +# -*- coding: utf-8 -*- +import PyDAQmx as daqmx +from PyDAQmx.DAQmxCallBack import * +import numpy as np +from ctypes import cast + +# Sample code to test changing the callback function for a daqmx acquisition +# task. +# +# Author: Erik Matlin, 9/9/2014 +# Modified by Pierre Cladé 23/9/2014 + +class CallbackWithUnregister(object): + def __init__(self, device, nSamples=1000): + self.task = daqmx.Task() + self.task.EveryNCallback1 = self.EveryNCallback1 + self.task.EveryNCallback2 = self.EveryNCallback2 + + self.task.CreateAIVoltageChan("/".join((device,'ai0')), "a", + daqmx.DAQmx_Val_Cfg_Default, -10, 10, daqmx.DAQmx_Val_Volts, None) + + + def start(self, nSamples, callbackname): + # configure timing + self.task.CfgSampClkTiming("", 10000, daqmx.DAQmx_Val_Rising, + daqmx.DAQmx_Val_ContSamps, nSamples) + self.nSamples = nSamples + + self.task.AutoRegisterEveryNSamplesEvent(daqmx.DAQmx_Val_Acquired_Into_Buffer, nSamples, 0, name=callbackname) + self.task.StartTask() + + def stop(self): + self.task.StopTask() + + def EveryNCallback1(self): + read = daqmx.int32(0) + data = np.zeros(self.nSamples) + self.task.ReadAnalogF64(self.nSamples, 1.0, + daqmx.DAQmx_Val_GroupByScanNumber, data, data.size, + daqmx.byref(read), None) + + def EveryNCallback2(self): + read = daqmx.int32(0) + data = np.zeros(self.nSamples) + self.task.ReadAnalogF64(self.nSamples, 1.0, + daqmx.DAQmx_Val_GroupByScanNumber, data, data.size, + daqmx.byref(read), None) + + +if __name__ == '__main__': + from time import sleep + b = CallbackWithUnregister("TestDevice") + for i,nSamples in enumerate([1000, 2000, 5000]): + func_name = 'EveryNCallback{0}'.format([1,2][i%2]) + b.start(nSamples, func_name) + print("started!") + sleep(.5) + b.stop() + print("stop") diff --git a/PyDAQmx/example/test/__init__.py b/PyDAQmx/example/test/__init__.py index 61fa485..6f01d48 100644 --- a/PyDAQmx/example/test/__init__.py +++ b/PyDAQmx/example/test/__init__.py @@ -11,7 +11,7 @@ # This function is called by unittest.main # Do no delete !!!! def load_tests(loader, standard_tests, pattern): - for name, elm in globals().items(): + for name, elm in list(globals().items()): if name.startswith('test') and isinstance(elm, ModuleType): suite = loader.loadTestsFromModule(elm) standard_tests.addTests(suite) diff --git a/setup.py b/setup.py index b67eb45..63334aa 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ from setuptools.command.test import test as TestCommand ##### WARNING version string should be modified also in the __init__.py -version = '1.4.4' +version = '1.4.5' import os directory = os.path.split(os.path.realpath(__file__))[0] @@ -114,6 +114,7 @@ def run_tests(self): =============== Main changes: +* 1.4.5 All files compatible with Python 2 and Python 3 (remove 2to3) * 1.4.4 New location introduced by DAQmx 19 * 1.4.3 Support for Centos 7 * 1.4.2 Proper version string @@ -179,7 +180,7 @@ def run_tests(self): 'Topic :: Software Development :: Libraries', 'Topic :: Software Development :: Libraries :: Python Modules'], packages=packages, - use_2to3=True, +# use_2to3=True, cmdclass = {'test': Test, 'test_example':TestExample}) auth_name = "Pierre Cladé"