Cyclic redundancy check
This function calculates the CRC16 (Cyclic Redundancy Check) for all PLC types by using n bytes (8 bits) specified with the parameter NumberOfBytes and the starting address StartAddress.
Input
Starting address for the calculation of the check sum. For PLCs which do not support the instruction F70_BCC with CRC16 calculation (FP0, FP5, FP10), the starting address must be in the DT or FL area.
The number of bytes (8 bits), beginning with StartAddress, on which the CRC16 calculation is performed.
Output
The calculated check sum, which is only valid if the flag IsValid is set to TRUE.
Flag indicating whether the calculated check sum is valid or not.
For PLCs which do not support the instruction F70_BCC with CRC16 calculation (FP0, FP5, FP10) the CRC is not valid:
during the first eight execution scans when an internal table is built
if the address area of the variable StartAddress is not in the DT or FL area.
For PLCs that support the instruction F70_BCC with CRC16 calculation, the CRC is always valid.
Instead of using this F instruction, we recommend using the corresponding FP7 instruction: FP_CRC
Depending on the PLC type, one of the following two implementations of the function will be used:
PLCs which support the instruction F70_BCC with the parameter s1=10 to calculate CRC16 (FP-e, FP-Sigma, FP2, FP2SH, FP10SH) use F70_BCC directly.
For the other PLCs (FP0, FP0R, FP3, FP5, FP10), a sub-program making an explicit CRC16 calculation is called. The following restrictions apply to this sub-program:
During the first eight execution scans an internal table is built. During this time, no check sum is calculated, and the output IsValid remains FALSE. After that, the check sum is calculated, and the output IsValid is set to TRUE.
StartAddress requires an address in the DT or FL area.
The number of steps can increase up to approx. 200 when CRC16 is used as a sub-program.
When programming, please be aware that a certain amount of time is needed to build the internal table and to calculate the check sum, especially for large data volumes.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
Array1: ARRAY [0..10] OF INT:=[0,1,2,3,4,5,6,7,8,9,10];
ARRAY1_BYTES: INT:=22;
Array1Crc: WORD:=0;
CrcIsvalid: BOOL:=FALSE;
END_VAR
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 5 ;
NETWORK_BODY
B(B_F,CRC16!,Instance,10,0,21,4,,?DStartAddress?DNumberOfBytes?CCRC?CIsValid);
B(B_VARIN,,Array1,8,1,10,3,);
B(B_VARIN,,ARRAY1_BYTES,8,2,10,4,);
B(B_VAROUT,,Array1Crc,21,1,23,3,);
B(B_VAROUT,,CrcIsvalid,21,2,23,4,);
L(1,0,1,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY
CRC16(StartAddress := Array1,
NumberOfBytes := ARRAY1_BYTES,
CRC => Array1Crc,
IsValid => CrcIsvalid);