Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

New to DAQ, need help writing Digital I/O method in python or C

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

0 Kudos
Message 1 of 4
(7,818 Views)

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.

Humphrey H.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 4
(7,808 Views)

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

0 Kudos
Message 3 of 4
(7,800 Views)

I guess that would depend on how you are adding the data points to the array.

Humphrey H.
Applications Engineer
National Instruments
0 Kudos
Message 4 of 4
(7,793 Views)