05-02-2020 10:42 AM
I have several VIs that I am trying to use as perpetual shift registers. my intent is to call them from a VI that is being called numerous times from an external call, but I want to track the state of several variables each time through. The stop.vi seems to work, but the RFD_WriteDataCapture seems to reset the shift register to the default value every time through. I had to initialize the shift register or I would get an error, but it's resetting to the default every time I try to Write. I just want it to concatenate what I send in (during the write state). Any thoughts?
Solved! Go to Solution.
05-02-2020 10:52 AM - edited 05-02-2020 10:54 AM
Hi mrbean,
@mrbean wrote:
I have several VIs that I am trying to use as perpetual shift registers. my intent is to call them from a VI that is being called numerous times from an external call, but I want to track the state of several variables each time through. The stop.vi seems to work, but the RFD_WriteDataCapture seems to reset the shift register to the default value every time through. I had to initialize the shift register or I would get an error, but it's resetting to the default every time I try to Write. I just want it to concatenate what I send in (during the write state). Any thoughts?
You are trying to create VIs commonly known as FGV (functional global variable) or AE (action engine)…
Your WriteData-FGV will NOT keep previous values because you initialize the shift register with each call!
(Possible solution: create one more state in this FGV named "Initialize" and place that array constant inside this case to initialize the shift register data.)
05-02-2020 11:04 AM
Hi mrbean,
You are trying to make a Functional Global Variable, so you need to make the shift register of RFD_WriteDataCapture uninitialized because if not, every time you call the VI the shift register willl take the value of the constant outside the loop.
Also, if you need to initialize the array of strings you can create a 'Init' case in that VI, and call it only one time at the setup of your Main VI.
I attach the VI modified to give you an example
05-02-2020 12:29 PM - edited 05-02-2020 12:38 PM
You did not include the typdef, but here are some potential simplifications:
(0) It has been pointed out already that the SR needs to be uninitialized. (Additional information )
(1) You can use a feedback node, eliminating the loop that's only used to anchor the shift register. It seems silly to wire the error across all cases if nothing operates on it. Here's how the entire code of the subVI could look like (No while loop, no case structure, etc. modify as needed, e.g. decide if you want to globally initialize):
Note that the code is now significantly easier to read, verify and debug, because nothing is hidden in other cases. Yes, this is the entire code! 😄
(2) The correct function to append an element to an existing array is "built array": ("Insert into array" is a much more complicated function with many more inputs and can give unexpected results in other cases, for example if you wire an index that is outside the current array size, nothing will happen).
05-03-2020 09:58 AM