Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Using SCAN_Op in Python with ctypes

Solved!
Go to solution

I am trying to acquire 2 channels of analog data using SCAN_Op in Python. I call SCAN_Op directly after using ctypes to convert the function parameters. I only get data from one channel. I am looking for examples, or help.

 

ni=windll.nidaq32
c_intVector = numChans*c_int
sampleChans = c_intVector()
sampleGains = c_intVector()
for i in range(numChans):
            sampleChans[i] = chans[i]
            sampleGains[i] = gains[i]

self.DAQ_OpStatus = ni.SCAN_Op(sampleDeviceNumber, sampleNumChans, sampleChans, sampleGains, sampleBuffer, sampleNum, sampleRate, sampleScanRate)



0 Kudos
Message 1 of 5
(3,909 Views)

ERamer, 

 

What hardware and drivers are you using to acquire 2 channels of analog data? 

 

I found another forum post that seems to have a similar issue here

Maggie
National Instruments
Applications Engineer
ni.com/support
0 Kudos
Message 2 of 5
(3,891 Views)

I am using a Windows XP computer and the windll.nidaq32 driver.The hardware is a DACPad-6020E. The programming language is Python and I am using ctypes to convert the Python parameters to the SCAN_Op function to int16. I think the problem is constructing arrays of int16 for the channel id's and the gain.

0 Kudos
Message 3 of 5
(3,889 Views)

You should probably use c_int16 arrays instead of c_int:

 

c_intVector = numChans*c_int16

Message 4 of 5
(3,875 Views)
Solution
Accepted by topic author ERamer

Theller, you are correct. Here is the code that works:

 

    def SCAN_VOp (self, deviceNumber=1, numChans=2, chans=(0, 1), gains=(-1, -1), num=2, rate=1000.0, scanRate=0.0, numMuxBrds=0):
         #print "Entering DAQ_VOp"
        sampleDeviceNumber = c_int16(deviceNumber)
        sampleNumChans = c_int16(numChans)
        c_int16Vector = numChans*c_int16
        sampleChans = c_int16Vector()
        sampleGains = c_int16Vector()
        for i in range(numChans):
            sampleChans[i] = chans[i]
            sampleGains[i] = gains[i]
        sampleBuffer = create_string_buffer(2*num)
        sampleNum = c_ulong(num)
        sampleRate = c_double(rate)
        sampleScanRate = c_double(scanRate)
        sampleNumMuxBrds = c_int16(numMuxBrds)
         #When DAQ_Op returns with an error number equal to zero, buffer contains the acquired data.
        self.DAQ_OpStatus = ni.SCAN_Op(sampleDeviceNumber, sampleNumChans, sampleChans, sampleGains, sampleBuffer, sampleNum, sampleRate, sampleScanRate)
        self.DAQ_OpStatus = ni.SCAN_Demux (sampleBuffer, sampleNum, sampleNumChans, sampleNumMuxBrds)

        #convert buffer to scaled array
        br = sampleBuffer.raw
        fmt = 'h'*num
        unpacked = unpack(fmt, br)
        unpackedarray = array(unpacked)
        ScaledArray = unpackedarray * ((self.max - self.min)/(2 ** self.bits))
        #print "Leaving DAQ_VOp"
        return self.DAQ_OpStatus, ScaledArray


0 Kudos
Message 5 of 5
(3,830 Views)