LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmx write and for loop

I am learning Labview and having trouble understanding how to call the DAQmx write in a for loop.  I created this simple test VI to demonstrate the problem I am having.  Hopefully, someone can provide some insight to this.  When I run this VI I get the error message: "Attempted to write a sample beyond the final sample generated. The generation has stopped, therefore the sample specified by the combination of position and offset will never be available."  I don't understand what this error message is trying to say.

 

barney99_0-1683740288467.png

Thanks

0 Kudos
Message 1 of 17
(2,031 Views)

You are using a Finite Sample mode and wire a value of 5 to the number of samples to the DAQmx Timing VI. It means that you are planning to output only 5 samples. The first DAQmx Write VI before the DAQmx Start VI already writes 5 samples thus you cannot call any DAQmx Write VI again.

 

What are you trying to do? Are you trying to update the data at runtime? Why don't you use the Continuous Sampling mode?

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 2 of 17
(2,017 Views)

Thanks for your reply.  

This is just a simple test VI I came up with to figure out the concepts.

In this example I create an array of 10 numbers consisting of 5 fives and 5 zeroes.

What I want to do is write this same array out over and over again N times in a for loop.

For my actual VI, I need to write one array and then write a digital IO on one channel (to toggle direction), so that is why I need to write the same array in a for loop.  How can I do this?

0 Kudos
Message 3 of 17
(2,006 Views)

@barney99 wrote:

For my actual VI, I need to write one array and then write a digital IO on one channel (to toggle direction), so that is why I need to write the same array in a for loop.  How can I do this?


Are you trying to use both AO and DO? Or you just want to send a PWM over DO?

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 4 of 17
(1,981 Views)

I write the array out on AO and after that is complete I will toggle a digital line (DO).

I will repeat the above N times in a for loop.

Note: the digital line part is not shown on the simple test VI because I am trying to get the AO working first.

0 Kudos
Message 5 of 17
(1,975 Views)

@barney99 wrote:

I write the array out on AO and after that is complete I will toggle a digital line (DO).

I will repeat the above N times in a for loop.

Note: the digital line part is not shown on the simple test VI because I am trying to get the AO working first.


How long are you going to toggle the digital line? What is the delay time between the loop? Why are you not using the continuous mode? 

A waveform diagram would be beneficial for us to understand what are you trying to do.

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 6 of 17
(1,947 Views)

Referring back to my test VI, I am writing an array of data (10 values in this simple example).  I will change the state of a digital line (DO) after writing each array of 10 values.  In a perfect world, I would change the state (High or Low) of the digital line instantaneously.  If there is a way to do this with continuous mode, please suggest how this can be accomplished.  At this point in time given my test VI, I can't even write a finite block of data in a loop.

0 Kudos
Message 7 of 17
(1,895 Views)

Perhaps a better approach would be synchronizing both analog and digital outputs. See Synchronizing Analog Output and Digital Output Signals Using LabVIEW with DAQmx

Since they are using the sample clock, you should write the same number of data points for both tasks. In your case, for each set of data, write:

1. analog: 5 5 5 5 5 0 0 0 0 0 

2. digital:  0 0 0 0 0 0 0 0 0 1

If you want this pattern to run indefinitely until the user stops it, use continuous mode. Otherwise, use finite mode.

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 8 of 17
(1,884 Views)

This seems more complicated than it should be.

 

I don't want to change the state of the DO line at the same time that I am writing the AO waveform.  I want to write the entire AO waveform and THEN change the DO line.  And then repeat this over and over in a loop.  I am thinking that a flat sequence structure inside the for loop would accomplish this with the write AO in the first frame and the write DO in the second frame.  But I still don't know how to write a finite number of samples in a loop!

 

Going back to my original question about the test VI, how can I write a finite number of samples in a loop - or is this not possible?  The VI I attached does not work.  Does anyone know how to make this work?  One way to force it to work is to start the task, wait for it to finish and then stop the task inside the loop but from previous posts, this is not the correct way to do it because it takes too long.

 

If using Finite Samples is not possible, then is there a way to start and stop continuous samples based on the number of samples written?  

 

THanks for the replies.

0 Kudos
Message 9 of 17
(1,875 Views)

When using the sample clock timing, the signal is not output immediately when you call the DAQmx Write VI. Instead, the data points are written into the onboard buffer and sent out gradually on each sample clock tick.

If you want the AO and DO output to be exactly timed, using the synchronization method mentioned earlier is the correct approach.

However, if you don't mind the random delay between the AO and DO (which can be at the level of 10 to 100ms because you are using a USB DAQ), you can place everything inside a for loop.

ZYOng_0-1683918947208.png

 

-------------------------------------------------------
Applications Engineer | TME Systems
0 Kudos
Message 10 of 17
(1,821 Views)