Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

read AI and DI simultaneously

Solved!
Go to solution
Hello,
I want to read AI and DI simultaneously(of course there should be some gap during sampling time and read time?)
I'm using USB-6361, NIMAX 19.6, python 3.7.6, windows 10.
I don't mind if you give me any idea not only for python but also any other text based method.
I wanna read 2 channels of AI (1MS/s/ch) and 2 channels of DI all together.
 
I used function generator to make sine wave(3.2V amplitude, 1kHz) and connected it to ai0. And connected sync to PFI0.
 
If I set timeout of ReadDigitalLines to 0, it seems it lost a lot of DI data (or maybe clock freq. is too high? I'm not sure of it. Actually I couldn't found any information about how DI works.) Like the plot below. (orange line is DI. I drew only 1 AI chan and 1 DI chan.)
Figure 1 2020-05-06 오후 6_22_21.png

If I set timeout of ReadDigitalLines to -1, it seems it reads DI data too much. (or maybe clock freq. is too low? Again, I'm not sure of it.) Like the plot below. It also breaks after it reads data only once.
Figure 1 2020-05-06 오후 6_22_03.png
And this is zoom-in plot.
Figure 1 2020-05-06 오후 6_21_46.png
Please let me know if somebody has any idea.
My code is as below.
 
nidaq.DAQmxCreateAIVoltageChan(AItaskHandle,b"Dev%d/ai0,Dev%d/ai2" %(num,num),"",DAQmx_Val_Diff,float64(-5.0),float64(5.0),DAQmx_Val_Volts,None)
nidaq.DAQmxCreateDIChan(DItaskHandle,b"Dev%d/PFI0,Dev%d/PFI1" %(num,num),"",DAQmx_Val_ChanPerLine)
nidaq.DAQmxCfgSampClkTiming(AItaskHandle,"",float64(sampleRate),DAQmx_Val_Rising,DAQmx_Val_ContSamps,uInt64(1000000))
 
nidaq.DAQmxStartTask( DItaskHandle )
nidaq.DAQmxStartTask( AItaskHandle )
 
while True:
  readAI = uInt32()
  readDI = uInt32()
  bytesPerSamps = uInt32()
    nidaq.DAQmxReadAnalogF64(AItaskHandle,samples_per_chan,float64(-1),DAQmx_Val_GroupByScanNumber,AIdata.ctypes.data,Length,ctypes.byref(readAI),None)
nidaq.DAQmxReadDigitalLines(DItaskHandle,samples_per_chan,float64(0),DAQmx_Val_GroupByScanNumber,DIdata.ctypes.data,Length,ctypes.byref(readDI),ctypes.byref(bytesPerSamps),None)
 
 
Someone suggested me to set sample clock for DI like this: nidaq.DAQmxCfgSampClkTiming(DItaskHandle,"Dev%d/ai/SampleClock" %num,float64(sampleRate),DAQmx_Val_Rising,DAQmx_Val_ContSamps,uInt64(1000000))
But it doesn't read DI in this case. (I heard that it works for M series PCI board. Mine is X series, USB.)
 
Thank you in advance!
 
0 Kudos
Message 1 of 5
(2,671 Views)
Solution
Accepted by topic author Serenade

Sorry, I really don't know the text-based API and syntax to comment on many details.  But there's 1 thing I *can * suggest:

 


@Serenade wrote:
Someone suggested me to set sample clock for DI like this: nidaq.DAQmxCfgSampClkTiming(DItaskHandle,"Dev%d/ai/SampleClock" %num,float64(sampleRate),DAQmx_Val_Rising,DAQmx_Val_ContSamps,uInt64(1000000))

Change that second argument to have a leading '/' character, such as this: "/Dev1/ai/SampleClock".   Your X-series device can definitely support that kind of usage.  You've already got the right task start sequence in place, starting AI last.

 

 

-Kevin P

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 2 of 5
(2,642 Views)

lol I added '/' and it works!! (and enough timeout.)

Thank you so much.

 

Could you allow me to ask one extra question?

Where did you found this solution?

 

 

0 Kudos
Message 3 of 5
(2,622 Views)

I didn't so much "find" it as remember it.  It helps that I do my DAQ in LabVIEW where channels and terminals are available from a dropdown list that contains the corresponding syntax.  (Terminals have a leading "/" character, channels don't.)

 

I've also been in a few similar threads in recent years.  Here's oneHere's another.  There've been others too.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 4 of 5
(2,616 Views)

I see!!

Indeed, it seems it's better to use labview. 

I was quite curious how people find the solution for this kind of problem.

 

Thank you for the reply and the links. 

0 Kudos
Message 5 of 5
(2,513 Views)