Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Reset sample clock rate

Hi,
 
How do I reset the clock sample rate after a task has been created/started?
 
I have a C++ program using the NIDAQmx drivers to control a NI4461 PCI board.
 
Right now the program has a loop to send sine waves at different frequencies.  I'd like to be able to change the clock's sample rate so that the sampling is faster at higher frequencies.
 
Reading previous posts, it sounds like it's better not to start/stop tasks in a loop, but fitting all the frequencies in a single array would take up too much memory.  Once I can get the program running, I may try to break the frequencies up into blocks and run each block separately to see if this would make things faster.  Either way, I don't see how to reset the sample clock rate.  I tried just calling DAQmxCfgSampClkTiming() with a new sample rate (for both my write and read tasks) and also calling DAQmxSetSampClkRate() and DAQmxResetSampClkRate() but get this error :
 
read taskH=1842367336
Measurements: Requested Sample Clock Rate is not available because this task shares the Sample Clock Source or the Sample Clock Timebase with another task. The other task has already programmed one of those properties in a manner inconsistent with the requested Sample Clock Rate.
Specify a Sample Clock Rate consistent with the settings in the other task, or change the settings in the other task. Refer to documentation for more detailed information.
 
Here's an example of some of the code :
 
for (int i=0; i<nfrequencies; i++) {
....
freq = freqArr[i];
samplesPerSecond = sampleRate*log10(freq);
nsamples = sampleRate*nwaves/freq;
 
 // configure write and read timing
DAQmxCfgSampClkTiming(writeTask, NULL, samplesPerSecond, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, samplesPerChannel);
DAQmxCfgSampClkTiming(readTask, NULL, samplesPerSecond, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, samplesPerChannel);
// configure read trigger
char source[] = "ai0";
DAQmxCfgAnlgEdgeStartTrig (readTask, (const char*)source, DAQmx_Val_RisingSlope, level);
 
 // start the read task
DAQmxStartTask(readTask); 
 // write samples
DAQmxCfgOutputBuffer(writeTask, nsamples);
DAQmxWriteAnalogF64(writeTask, nsamples, false, 10, DAQmx_Val_GroupByScanNumber, samples, &nsamplesWritten, NULL);
 
 // start the output signal task
DAQmxStartTask(writeTask); 
 
while (!isDone) {
   rc = DAQmxIsTaskDone(writeTask, &isDone);
 }
 
 // start reading cap probe and current probe samples
DAQmxReadAnalogF64(readTask, nsamples/2, 10, DAQmx_Val_GroupByChannel, samples, nsamples, &nsamplesRead, NULL);
 
DAQmxStopTask(writeTask);
DAQmxStopTask(readTask);
}
 
Thanks for any help!
 
Nia
0 Kudos
Message 1 of 9
(4,611 Views)

Hi Nia,

Thank you for posting to the National Instruments Discussion Forums.

You should be able to stop the task, reset the timing parameters using DAQmxCfgSampClkTiming and then re-starting the task.

Did you see this error when you tried to reset the sampling rate while your acquisition was running?

 

Abhinav T.
Applications Engineering
National Instruments India

LabVIEW Introduction Course - Six Hours
Getting Started with NI-DAQmx
Measurement Fundamentals
0 Kudos
Message 2 of 9
(4,578 Views)

Hi Abhinav,

No, I didn't try stopping the task, resetting the sample rate, then starting the task again.  From previous posts, it sounded like stopping/starting the task was not an efficient solution.  But if this is the only way to reset the sample rate, I will do that.

Thanks,

Nia

0 Kudos
Message 3 of 9
(4,575 Views)
Hi Abhinav,
 
I have a loop that :
 
1) Calls DAQmxCfgSampClkTiming for the read and write tasks
2) Starts the read and write tasks
3) Stops the read and write tasks
 
I see the error when I run this loop.
 
Do I also need to Create and Clear the tasks in the loop?
 
Thanks.
 
 

Message Edited by NF1 on 05-21-2007 04:40 PM

0 Kudos
Message 4 of 9
(4,569 Views)

Nia,

You shouldn't need to clear the task. Can you try resetting the sample clock for only the read task? Also can you post a snippet of the DAQmx code you are using to reset the sampling rate?

Thanks,

Abhinav T.
Applications Engineering
National Instruments India

LabVIEW Introduction Course - Six Hours
Getting Started with NI-DAQmx
Measurement Fundamentals
0 Kudos
Message 5 of 9
(4,555 Views)

Hi Abhinav,

There's a snippet of code in my original post, if you can refer to that.

If I just call DAQmxCfgSampClkTiming() for the read task (and move the call by the write task outside of the loop) I don't get an error, but the code runs *much* slower. 

I did notice once that I was able to change the sample rate for both the read and write tasks one time, but got an error when I tried changing the sample rate a second iteration.  I'm wondering if this was a fluke (mistake on my part), a timing issue, or something else.  I may try calling DAQmxCfgSampClkTiming() outside of the loop with the highest sample rate I want to use, then see if I'm able to reset the sample rate in my loop.  Any thoughts?

Thanks,

Nia

0 Kudos
Message 6 of 9
(4,537 Views)

Nia,

Few things you might want to try:

- I saw that you were configuring the output buffer after starting the task - can you try calling that before you start the task?

- Sample both channels at the maximum possible rate and then decimate the input values

 

Abhinav T.
Applications Engineering
National Instruments India

LabVIEW Introduction Course - Six Hours
Getting Started with NI-DAQmx
Measurement Fundamentals
0 Kudos
Message 7 of 9
(4,514 Views)
 

Message Edited by NF1 on 05-25-2007 05:25 PM

0 Kudos
Message 8 of 9
(4,500 Views)

Hi Abhinav,

It looks like the resources for the tasks need to be unreserved in order to reset the sample rate within the loop.  Calling DAQmxTaskControl() with action set to DAQmx_Val_Task_Unreserve within the loop before the call to DAQmxCfgSampClkTiming() does the trick.

Thanks for you help,

Nia

Message Edited by NF1 on 05-30-2007 11:50 AM

Message Edited by NF1 on 05-30-2007 11:50 AM

0 Kudos
Message 9 of 9
(4,435 Views)