What difference does it make if you declare variables (integers, reals, bools, frames, positions, etc.) in the .src file compared to declaring them in the respective .dat file?
Is there any difference in monitoring, using, the variables?
Declaring variables in .src <-> .dat
-
Plc_User -
March 8, 2016 at 2:21 PM -
Thread is marked as Resolved.
-
-
there is a huge difference....
for example:
variables declared in SRC can ONLY be used inside THAT routine (not in other routines in same SRC file). also cannot be used to pass parameters to interrupt subs. cannot be monitored all the time, variable returns memory to system when routine ends. cannot declare constants (only variables).
variables declared in DAT, can be used to retain value even after program finished. if initialized in DAT file, they also retain value after reboot. can be used to pass parameters to interrupts subs. can be used in entire corresponding SRC file. can be used to declare constants, potentially can be used to declare global variables. can be monitored as long as program is selected etc. local variables declared in DAT of a subroutine can be monitored even when instruction pointer is not in that sub. variables declared in DAT, take memory as soon as program is selected and keep it until reboot. keep in mind when running bunch of different programs....
-
The underlying concept is "Scope." https://en.wikipedia.org/wiki/Scope_(computer_science)
In the SRC file, a variable declared inside a subroutine (or function) only exists for that subroutine. As soon as that subroutine is no longer in the call stack, that variable is lost.
In the .DAT file, any variable declared has a "module-wide" scope -- any subroutine in that .DAT file's associate SRC file can access them. But the variables are only kept in memory as long as one of the subroutines in that SRC file are in the caller stack.
If a .DAT variable is declared with an initial value (eg, DECL INT Counter=0), then the values of that variable are remembered forever, even if the program is reset or the robot is rebooted. An Archive of the robot will show a "snapshot" of all these values as they were the moment the Archive was made.
Any variable declared with GLOBAL, in any DAT file, has global scope -- any subroutine, in any module, can access it. The file $CONFIG.DAT is a special case -- anything declared in $CONFIG.DAT is automatically global, without need a GLOBAL in the DECL line. This is because before GLOBAL DECL was added to KRL, $CONFIG.DAT was the only way to create global variables.
-
Also just to point out, if I am not wrong, any IN/ OUT parameters have to be declared in *.src file, or ERROR 2318 appears.
(I guess the workaround here is to assign the IN params to local .dat params at the beginning of main routine and assign local .dat params to the OUT params at its end, to get the same functionality as the oen you are pursuing)