Block check code calculation
Calculates the Block Check Character (BCC), which is used to detect errors in message transmission, of s3_Number bytes of ASCII data starting from the 16-bit area specified by s2_Start according to the calculation method specified by s1_Control. The Block Check Code (BCC) is stored in the lower byte of the 16-bit area specified by d. (BCC is one byte. The higher byte of d does not change.)
Input
Specifies BCC calculation method:
0: Addition (SYS_BCC_CALCULATION_METHOD_ADD)
1: Subtraction (SYS_BCC_CALCULATION_METHOD_SUB)
2: Exclusive OR (SYS_BCC_CALCULATION_METHOD_XOR)
16#A: CRC-16 (SYS_BCC_CALCULATION_METHOD_CRC16)
Starting 16-bit area to calculate BCC
Specifies number of bytes for BCC calculation
Output
16-bit area for storing BCC
Instead of using this F instruction, we recommend using the corresponding FP7 instruction: FP_CRC, FP_BCC
Specifying the control code s1_Control
0: Addition (SYS_BCC_CALCULATION_METHOD_ADD)
1: Subtraction (SYS_BCC_CALCULATION_METHOD_SUB)
2: Exclusive OR (SYS_BCC_CALCULATION_METHOD_XOR)
16#A: CRC-16 (SYS_BCC_CALCULATION_METHOD_CRC16)
0–F
If CRC-16 is specified as the calculation method, ASCII code cannot be specified for the conversion data.
if the number of specified bytes for the target data exceeds the limit of the specified data area.
if the number of specified bytes for the target data exceeds the limit of the specified data area.
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
Start: BOOL:=FALSE;
(*activates the function*)
BCC_Calc_Method: INT:=2;
ASCII_String: STRING[32]:='%01#RCSX0000';
(*specifies the operation:
0: addition
1: subtraction
2:XOR*)
BCC: WORD:=0;
(*result after a 0->1 leading
edge from start: 10#172*)
END_VAR
A block check character calculation is performed on ASCII_String when Start turns to TRUE. The calculation method is exclusive OR. (Use this method when large amounts of data are transmitted).
How the BCC is calculated using the exclusive OR operation:
bvar_1 |
bvar_2 |
bvar_3 |
|
0 |
0 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
1 |
|
1 |
1 |
0 |
The ASCII BIN code bits of the first two characters are compared with each other to yield an 8-character exclusive OR operation result:
Sign for comparison |
ASCII BIN code |
% |
00100101 |
0 |
00110000 |
Exclusive OR result |
00010101 |
This result is then compared to the ASCII BIN code of the next character, i.e. "1".
Sign for comparison |
ASCII BIN code |
Exclusive OR result |
00010101 |
1 |
00110001 |
Next exclusive OR |
00100100 |
And so on until the final character is reached.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 11 ;
NETWORK_BODY
B(B_F,F70_BCC!,Instance,26,1,34,7,,?DEN?Ds1_Control?Ds2_Start?Ds3_Number?AENO?Cd);
B(B_CONTACT,,Start,3,2,5,4,);
B(B_VARIN,,BCC_Calc_Method,24,3,26,5,);
B(B_F,Adr_Of_VarOffs_I!,Instance,16,5,25,9,,?D@'Var'?DOffs?CAdr);
B(B_VAROUT,,BCC,34,3,36,5,);
B(B_VARIN,,ASCII_String,11,4,13,6,);
B(B_VARIN,,2,14,7,16,9,);
B(B_F,LEN!,Instance,16,8,21,11,,?DIN?C);
L(16,5,16,7);
L(13,5,16,5);
L(13,5,13,10);
L(13,10,16,10);
L(25,5,25,7);
L(25,5,26,5);
L(26,6,26,10);
L(21,10,26,10);
L(5,3,26,3);
L(1,3,3,3);
L(1,0,1,11);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF start then
F70_BCC( s1_Control:= BCC_Calc_Methode,
s2_Start:= Adr_Of_VarOffs( Var:= ASCII_String,
Offs:= 2),
s3_Number:= LEN( ASCII_String),
d=> BCC);
END_IF;