FUNCTION_BLOCK FB 2
TITLE =
VERSION : 0.1
VAR_INPUT
iInstanceNumber : INT ;
END_VAR
VAR
iCopyOfInstanceNumber : INT ;
END_VAR
BEGIN
NETWORK
TITLE =accessing FB variables
//When you access a variable from instance data, that is
// variables in the IN,OUT,IN_OUT and STAT area (not the TEMP area),
// the editor displays this nicely for you as below
//..
L #iInstanceNumber;
T #iCopyOfInstanceNumber;
//..
//however, behind the scenes, the instructions actually stored are
//as in the comments below
//..
L #iInstanceNumber; // same as L DIW[AR2,P#0.0]
T #iCopyOfInstanceNumber; // same as T DIW[AR2,P#2.0]
//..
//If you type L DIW[AR2,P#0.0] and then save the block, the editor
//will automatically convert the instruction to the nice display format
// if it is possible.
//(as this block has been saved it cannot be shown other than in a comment!)
//Try it using the block editor and see for yourself.
//..
// L DIW [AR2,P#0.0]
//..
//If you use an instruction that does not convert to the nice display format,
//it will not convert. For example referencing a byte of the integer.
//Instructions with AR1 will also not convert.
//..
L DIB [AR2,P#0.0];
TAR2 ;
LAR1 ;
L DIW [AR1,P#0.0];
//..
//The editor works out all the correct P# offsets for every variable.
//If you insert another variable, the editor re-adjusts all the P# values
//..
//When an FB is called, the calling block will set AR2 to point to the base
//address of the instance data.
//For an FB with it's own instance DB, AR2 will point to the start of the instance DB
//For a multiple instance FB, AR2 will point to the relevant point in the instance DB
//..
//If you modify AR2 inside an FB, all instance data variable access will be relative
//to the modifed AR2
//For example, if we increment AR2 by two bytes,
//..
+AR2 P#2.0;
L #iInstanceNumber; //is actually accessing iCopyOfInstanceNumber
//..
//
//
END_FUNCTION_BLOCK