Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

AI & DI Simultaneously

Hello,

 

I'm using USB-6361 and want to get 2 channels AI and 4 channels DI simultaneously.

For example, 50,000 data for each channel, and they should be synchronized. The first AI data and the first DI should be acquired at the same time and the last data also.

 

I did it for same number of AI and DI channels as below, with python code.

 

        ...
        import ctypes
        ...
        self.nidaq = ctypes.windll.nicaiu

        self.int32 = ctypes.c_long
        self.uInt32 = ctypes.c_ulong
        self.uInt64 = ctypes.c_ulonglong
        self.float64 = ctypes.c_double
        self.char = ctypes.c_char
        self.char_p = ctypes.c_char_p
        self.DAQmx_Val_GroupByChannel = 0
        self.DAQmx_Val_GroupByScanNumber = 1
        self.DAQmx_Val_Acquired_Into_Buffer = 1
        self.DAQmx_Val_ChanPerLine = 0
        self.TaskHandle = self.uInt32
        self.AItaskHandle = self.TaskHandle( 0 )
        self.DItaskHandle = self.TaskHandle( 1 )
        ...

        samplesPerChanAI = int(maxNumSamples/3.)    
        samplesPerChanDI = int(maxNumSamples/3.)
        self.AIdata = numpy.zeros( ( maxNumSamples ), dtype=numpy.float64 )
        self.DIdata = numpy.zeros( ( maxNumSamples ), dtype=numpy.uint8 )
        AIdata_dummy = numpy.zeros( ( maxNumSamples ), dtype=numpy.float64 )
        Length = AIdata_dummy.size

        self.nidaq.DAQmxCreateTask("",ctypes.byref(self.AItaskHandle))
        self.nidaq.DAQmxCreateTask("",ctypes.byref(self.DItaskHandle))

        self.nidaq.DAQmxCreateAIVoltageChan(self.AItaskHandle, b"Dev%d/ai0,Dev%d/ai1,Dev%d/ai2" %(num, num, num), "", const.DAQmx_Val_Diff, self.float64(-5.0), self.float64(5.0), const.DAQmx_Val_Volts,None)
        self.nidaq.DAQmxCreateDIChan(self.DItaskHandle, b"Dev%d/port0/line0:2" %num, "", self.DAQmx_Val_ChanPerLine)
        self.nidaq.DAQmxCfgSampClkTiming(self.AItaskHandle, "", self.float64(sampleRate), const.DAQmx_Val_Rising, const.DAQmx_Val_ContSamps, self.uInt64(500000))
        self.nidaq.DAQmxCfgSampClkTiming(self.DItaskHandle, b"/Dev%d/ai/sampleClock" %num, self.float64(sampleRate), const.DAQmx_Val_Rising, const.DAQmx_Val_ContSamps, self.uInt64(500000))
        
        self.nidaq.DAQmxStartTask( self.DItaskHandle )
        self.nidaq.DAQmxStartTask( self.AItaskHandle )

        while True:
            bytesPerSamps = self.uInt32()
            self.nidaq.DAQmxReadAnalogF64(self.AItaskHandle, samplesPerChanAI, self.float64(-1), self.DAQmx_Val_GroupByScanNumber, self.AIdata.ctypes.data, Length, ctypes.byref(self.readAI), None)
            self.nidaq.DAQmxReadDigitalLines(self.DItaskHandle, samplesPerChanDI, self.float64(-1), self.DAQmx_Val_GroupByScanNumber, self.DIdata.ctypes.data, Length, ctypes.byref(self.readDI), ctypes.byref(bytesPerSamps), None)
            #break at some moment when the DAQ ends


        self.nidaq.DAQmxStopTask( self.DItaskHandle )
        self.nidaq.DAQmxClearTask( self.DItaskHandle )
        self.nidaq.DAQmxStopTask( self.AItaskHandle )
        self.nidaq.DAQmxClearTask( self.AItaskHandle )
        ...

 

However, when I change the number of DI channels and sampling rate for DI, the sync between AI and DI no longer matched. Total number of sample are same, for example AI 100,000 samples (50,000 for each channel) and DI 100,000 samples (25,000 for each channel) at each read. (sampling rate is 1MS/s for AI and 500kS/s for DI)

 

 

Please let me know if you have any idea or need further information.

 

Thank you in advance. 🙂

 

Best wishes,

JESuh

 

 

0 Kudos
Message 1 of 2
(622 Views)

Since the DI sampling clock is configured to use AI's sampling clock, you cannot have different rates for DI and AI.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 2
(605 Views)