Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

does local variable value get reset to initial value when starts running?

I'm trying to send a command via serial port and read it back (see attached VI). It looks like the local variables latch the values everytime after the VI gets run.

 

Here is the scenario where the problem was found:

 

run the VI with the default values (PAYLOAD = EF);

change the value of PAYLOAD variable (to PAYLOAD = AB) and re-run the VI. (result - Debug Array index 7 is EF but should be AB)

re-run the VI without any change (result - Debug Array index 7 is now AB)

 

If I move the encode/decode part inside the loop and remove the variables "Connected?" and "data from serial port", everything works as expected.

 

Is there any suggestion that I could keep the local variables and have the expected result?

 

Thanks.

0 Kudos
Message 1 of 5
(7,199 Views)

That's a perfect example of local variable abuse. You have a race condition and a misunderstanding of dataflow. The locals inside the loop can be read before your code outside can write, Your code outside won't even run more than once so I don't see the point of putting it out there at all.

Message 2 of 5
(7,192 Views)

I understand the locals inside the loop can be read before the code outside the loop. But I thought the local variables got initialized to 0 for numeric or empty for string after the program finishes running.

 

The code outside the loop may be used many times in my real project but I think I will avoid using local variables.

0 Kudos
Message 3 of 5
(7,173 Views)

Hi MileP,

 

It is always recommended to avoid local variables when possible.  For more information on what Dennis was referring to please refer to Tutorial: Local Variable, Global Variable, and Race Conditions 

Regards,

Hassan Atassi
Senior Group Manager, Digital Support
0 Kudos
Message 4 of 5
(7,133 Views)

Locals(read) don't get "reset" per se.  They output the value property of the object that they are linked to AT THE time they are read.  If it is linked to a control and read while the VI is in a state defined by default values (just got into memory) the local will produce the default value for the control- if no default value was expressly set the value will be the default default for the data type.  eg F for bool, 0 for ints, etc...


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 5
(7,117 Views)