03-05-2007 11:15 AM
I have an application to run waveforms on several channels, and my application is using 100% CPU while it is outputing the waveforms. I have done my best to optimize my code to reduce CPU and memory usage, but I haven't been able to fix it. The loop below is where the CPU jumps up to 100%. What is happening in this loop is that i am using the DAQmx circular buffer to output my waveforms. I initially load data into the buffer and then go to this while loop to determine when the next bufferful of data can be written until the entire waveform is complete. My code spends a lot of time in this while loop, so if there is any chance i can reduce the CPU usage it would be great. Does anyone see any major flaws that might help me fix this issue? I read on some knowledge group post somewhere that earlier versions of NIDAQmx used a lot of the CPU.
Code Info:
Written in VB6.0
NIDAQmx version: 7.5
NI Card: PCI-6711
While (LoopCount <= BufferCount)
If (taskIsRunning = False) Then Exit Sub
status = DAQmxGetWriteSpaceAvail(taskHandle, WriteSpaceAvail)
status = DAQmxGetWriteTotalSampPerChanGenerated(taskHandle, SamplesGenerated)
If (status < 0) Then Call DAQmxErrChk(status)
If (NumChans <> 3) Then
If (WriteSpaceAvail >= SizeofHalfBuffer Or ((WriteSpaceAvail = 0) And (SamplesGenerated = 32768))) Then
status = DAQmxWriteAnalogF64(taskHandle, (SizeofHalfBuffer / NumChans), 0, 0, DAQmx_Val_GroupByScanNumber, HalfBuffer(0), sampsPerChanWritten, ByVal 0&)
If (status < 0) Then Call DAQmxErrChk(status)
LoopCount = LoopCount + 1
lngLastSamplesGenerated = SamplesGenerated
Call GenerateHalfBuffer(RemainingPoints, CLng(LoopCount))
RemainingPoints = RemainingPoints + SizeofHalfBuffer
End If
Else
If (WriteSpaceAvail >= SizeofThreeChannelHalfBuffer Or ((WriteSpaceAvail = 0) And (SamplesGenerated = 32768))) Then
DAQmxErrChk DAQmxWriteAnalogF64(taskHandle,(SizeofThreeChannelHalfBuffer/NumChans),0,0,DAQmx_Val_GroupByScanNumber,ThreeChannelHalfBuffer(0),sampsPerChanWritten,ByVal 0&)
LoopCount = LoopCount + 1
Call GenerateHalfBuffer(RemainingPoints, CLng(LoopCount))
RemainingPoints = RemainingPoints + SizeofThreeChannelHalfBuffer
End If
End If
DoEvents
Wend
03-06-2007 01:19 PM - edited 03-06-2007 01:19 PM
Hi,
Thank you for posting to the NI forums. If you’re just doing analog output, I would recommend starting with one of the examples that install with DAQmx. There is a good analog output example that can be found in the following location…
National Instruments\NI-DAQ\Examples\Visual Basic 6.0\Analog Out\Generate Voltage\Cont Gen Volt Wfm-Int Clk
You should be able to modify this example to handle multiple outputs, rather than manually viewing the buffer sizes to stop the while loop.
If this is not a viable option, then I would recommend adding some form of delay (around 10ms or so) to your current while loop. This will give the operating system time between iterations to handle other processes and take some of the burden off of your CPU.
In addition to this, I would also recommend upgrading your DAQmx driver.
Post back if you have further questions. Thanks!
Ed W.
Applications Engineer
National Instruments
Message Edited by Ed W on 03-06-2007 01:22 PM
03-06-2007 01:41 PM
03-07-2007 05:21 PM
Hi,
What was the error number that you received with that error? Often times, you can fix errors like these by changes the number of samples to read. Let me know about that, and I can get back to you with some more recommendations. Thanks!
Ed W.
Applications Engineer
National Instruments
03-29-2007 08:51 AM
03-30-2007 05:10 PM
Hi Gerry,
Can you reproduce the error to find the error number? It’d be difficult to offer recommendations for a solution without that information.
However, if the error message recommends increasing the buffer size, that’ll probably fix the problem. A good rule of thumb is to set the sample count to 10% of the sample rate in Hz. Make sure to multiply the sample rate parameter by the number of channels in your task to determine a good sample count.
I hope this helps. Post back if you have any more questions.
Ed W.
Applications Engineer
National Instruments
06-12-2007 12:36 PM