02-19-2009 05:09 PM - edited 02-19-2009 05:15 PM
Hi all,
In our lab we have inherited previous code for data aquisition from a voltage source. It consists of data aquisition in a for loop. ()
However, there seems to be no way of placing the creation of tasks and cleanup of tasks outside the for loop: the tasks wire becomes an array, unlike this situation:
http://forums.ni.com/ni/attachments/ni/250/30849/1/Acq&Graph%20Voltage-Int%20Clk.vi
http://forums.ni.com/ni/board/message?board.id=250&message.id=30907&query.id=552736#M30907
I would like to have only the data acquisition within the for loop, (rather than waste time creating the tasks over and over again) but there seems to be some trick to clean up the task afterwards that escapes me (the task wire becomes an array see).
Solved! Go to Solution.
02-19-2009 05:31 PM
Right click the output tunnel on the for loop where the wire is broken and select 'Replace with Shift register'. That tunnel was set to autoindex so you were creating a array. Since you might be cleaning this up consider whether you need all the sequence structures inside that loop. ![]()
Hope this helps.
02-19-2009 05:31 PM
You need to right click on your tunnel that is [ ] and tell it to disable auto indexing.
By the way, you image doesn't show up because you inserted the image from the temporary preview area of the forums. You need to actually submit your message first with the image files attached. Then you can go back and edit the message and insert the images based on the their NI forum html links based on their permanent locations.
02-19-2009 07:00 PM
Ok, so, one point of confusion here:
when should one use a shift register instead of indexing? since both "solve" the "problem".
In this case, is a shift register simply redundant, and no indexing should be used?
It seems best to me to place the error wire on a shift register, but place the task wire in a tunnel with no indexing, since in this case the task is always the same...
02-19-2009 08:47 PM
zipmanx wrote:Ok, so, one point of confusion here:
when should one use a shift register instead of indexing? since both "solve" the "problem".
In this case, is a shift register simply redundant, and no indexing should be used?
It seems best to me to place the error wire on a shift register, but place the task wire in a tunnel with no indexing, since in this case the task is always the same...
Zip-
That's a GREAT question and one that should get a simple answer- But it won't. and every CLD will have their own opinion.
IMHO- use shift registers (marginally equivalent to pass by referance since the loop can change the value) for any variable that can change within the structure UNLESS you wish to ignore the change (e.g. ignore certain errors ONLY IF it is known what likely events cause errors and a retry is desired) I commonly use a tunnel for the error cluster inside equipment driver initialize functions if there was an bad or no response from the device and pop-up a prompt to change the VISA resource. Yet for other common tasks I will place the error cluster on a shift register because I want to stop wasting time when an error occurs in a structure. "expecting the same action to yield differing results is insanity"
Use tunnels without indexing to send the same value to every call to functions within the structure (marginally equivilant to a pass by value since all iterations get the same value). This works well for referances like filenames or VISA sessions if you insure that the loop callees can not change the values!
And use tunnels with indexing to synchronize or control values passed to the funtions inside the structure.
In summary - there are "rules-of-thumb" that coding experience will teach but, it really gets down to a matter of style.
Good Luck
02-20-2009 08:56 AM
First, you don't want to use indexing at all. Auto indexing is the type of tunnel that is [ ]. It autoindexes through arrays. So on a 1-D array coming in, each iteration returns one element of the array. On a scalar going out of the loop each iteration builds the scalars into a 1-D array. This isn't what you want and is why your wires were broken.
Another type of tunnel is normal. It is a solid color, it just passes the last value out of a loop, or uses the value coming in from outside a loop. When a loop ends, only the last value that the wire contained is used.
The third type is a shift register. It allows the value that goes out at the end of a loop iteration to be returned to the front and used at the beginning of the next loop iteration. When the loop ends, the wire coming out of the right hand shift register will contain the last value.
One difference between a normal tunnel and a shift register is how memory is allocated for storing the value. For your situation, this isn't really an issue. As Jeff had said, if you use a shift register on an error wire, that error will pass into the next iteration. Since many functions don't run if there is an error coming into it, you could wind up with no later iterations of a loop ever running the way you intended. So let's say you have a loop where you continually read the serial port and graph the data. If you have a VISA read failure, let's say the cable got disconnected accidentally, you'll generate and error. That error will go into the next iteration of the loop, Even if you correct your cable problem, you will never read the Serial port again because the error will always persist unless you have some other mechanism for clearing it. With a normal tunnel, the loop will begin with the orignal no error condition and you can continue to try reading from the serial port.
Now the advantages of a shift register are this. A value can be returned to the next iteration of a loop. Let's say you have a purple VISA wire and you intially have it set for COM1 going into the loop. If you want to change from Com1 to Com2 while you are inside the loop, you can do that. Com2 will get passed to the next iteration of the loop by way of the shift register. If you had a normal tunnel, the next iteration would go back to using Com1.
One other subtle difference between a normal tunnel and shift register. This won't matter for a while loop, because they always run at least one time. But a For loop has a chance of running zero times. With a shift register, the value going in to the left will come out the right even though the loop never iterated. But with a normal tunnel, the value out will be the default value for the data type because the loop never ran to pass the value going in to the left tunnel by wire to the tunnel going out the right side.