01-22-2013 03:18 PM
Hello,
I am trying to write an aquisition program for digital input coming from a hall sensor. I will be passing a magnet over a hall sensor on a motor and want to record the number of rotations. I have pasted together a script based on various websites, but it isn't functioning the way I want. I would like to be able to collect data points indefinately, or fixed, and then print out the resulting count. I have attepted to use the ctypes wrapper in Python. The NI functions remain unchanged though, so if you don't know Python it shouldn't be a problem to read.
The code:
# C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Analog In\Measure Voltage\Acq-Int Clk\Acq-IntClk.c
import ctypes
import numpy
nidaq = ctypes.windll.nicaiu # load the DLL
##############################
# Setup some typedefs and constants
# to correspond with values in
# C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\NIDAQmx.h
# the typedefs
int32 = ctypes.c_long
uInt32 = ctypes.c_ulong
uInt64 = ctypes.c_ulonglong
float64 = ctypes.c_double
TaskHandle = uInt32
# the constants
DAQmx_Val_ChanForAllLines = int32(0)
##############################
def CHK(err):
"""a simple error checking routine"""
if err < 0:
buf_size = 200
buf = ctypes.create_string_buffer('\000' * buf_size)
nidaq.DAQmxGetErrorString(err,ctypes.byref(buf),buf_size)
raise RuntimeError('nidaq call failed with error %d: %s'%(err,repr(buf.value)))
# initialize variables
taskHandle = TaskHandle(0)
max_num_samples = 1000
# array to gather points
data = numpy.zeros((max_num_samples,),dtype=numpy.float64)
# now, on with the program
CHK(nidaq.DAQmxCreateTask("",ctypes.byref(taskHandle)))
CHK(nidaq.DAQmxCreateDIChan(taskHandle,'Dev1/port1/line1',"", DAQmx_Val_ChanForAllLines))
CHK(nidaq.DAQmxStartTask(taskHandle))
read = int32()
CHK(nidaq.DAQmxReadDigitalU32(taskHandle,int32(-1),float64(1),DAQmx_Val_GroupByChannel,data.ctypes.data,max_num_samples,ctypes.byref(read),None))
print "Acquired %d points"%(read.value)
if taskHandle.value != 0:
nidaq.DAQmxStopTask(taskHandle)
nidaq.DAQmxClearTask(taskHandle)
print "End of program, press Enter key to quit"
raw_input()
This script returns "Acquired 1 points" immediately, regardless of how many times the hall sensor has been switched.
I apologize for the sloppy code, but I understand very little of the underlying principles of these DAQ functions, despite reading their documentation. Seeing as how the code does not return an error, I am hoping there is just a minor tweak or two that will be farily obvious to someone with more experience.
Thanks
01-23-2013 11:18 AM
Hi paulias,
Currently you are reading one sample of data on demand since the sampling mode is never set. You will probably want to use DAQmxCfgSampClkTiming to configure the sampling rate and sampling mode. If you installed the examples with NI-DAQmx you can look at the Digital Read Values examples.
01-24-2013 10:17 AM
Thanks for the reply!
My device doesn't support timed functions (I recently learned), so the timing functions aren't accepted. I tried running the read function in a loop, but the data array would not accumulate the points, they (points) would always occupy the first place in the array. I have dealt with the issue by accumulating the points in Python list, but I am still wondering why additional calls to the read function didn't add additional points into the array. Why would this be?
Thanks again,
Paul
01-25-2013 11:19 AM
I guess that would depend on how you are adding the data points to the array.