P4025¶
- Code
P4025
- Message
Bit access index is out of range for the variable type
This error occurs when a bit access index on a variable exceeds the number
of bits available in the variable’s declared type, or when a partial access
(.%Bn, .%Wn, .%Dn, .%Ln) selects a slice the variable does
not have.
Each integer and bit string type has a fixed number of bits:
SINT,USINT,BYTE: 8 bits (valid indices 0..7)INT,UINT,WORD: 16 bits (valid indices 0..15)DINT,UDINT,DWORD: 32 bits (valid indices 0..31)LINT,ULINT,LWORD: 64 bits (valid indices 0..63)
A variable of any other type has no bits to select at all, and raises P4069 instead.
Example¶
The following code will generate error P4025:
FUNCTION_BLOCK FB1
VAR
myByte : BYTE;
myBool : BOOL;
END_VAR
myBool := myByte.8; (* Error: BYTE has only 8 bits, valid range is 0..7 *)
END_FUNCTION_BLOCK
The variable myByte is declared as BYTE which has 8 bits (indices 0 through 7). Accessing bit 8 is out of range.
To fix this error, use an index within the valid range or use a wider type:
FUNCTION_BLOCK FB1
VAR
myWord : WORD;
myBool : BOOL;
END_VAR
myBool := myWord.8; (* Correct: WORD has 16 bits, valid range is 0..15 *)
END_FUNCTION_BLOCK
Partial Access¶
The same check applies to the Edition 3 byte, word, double word, and long word forms. The slice must fit within the variable, and the index must not run past its last slice:
FUNCTION_BLOCK FB1
VAR
myWord : WORD;
myByte : BYTE;
END_VAR
myByte := myWord.%B2; (* Error: WORD has bytes 0..1 *)
myWord := myByte.%W0; (* Error: a WORD does not fit in a BYTE *)
END_FUNCTION_BLOCK
See Bit Access for the valid index range of each form.
Think IronPLC is wrong about this?
If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.