Appendix A: D320 PLC Communication Protocol
203
Cyclic Redundancy Checking (CRC)
•
The CRC is a 2-byte checksum that is calculated from the data of every message and then
attached to the end of the message by the sender. It is used as an error-checking device to
prevent loss or corruption of data during transmission of the message.
•
The sender of the message calculates and attaches the CRC when it generates and sends the
message. The receiver should also calculate the CRC from the data of the message and
compare the calculated value to the CRC that was sent. If the calculated CRC does not match
the CRC received, an error has occurred in the message during transmission.
CRC Calculation Range
The following subroutines illustrate the program code required to calculate the CRC for a message.
The initial value of the CRC (CRC_Sum) is set to 65535 ($FFFF). Then one of these subroutines
would be called once for each byte (data) of the CRC calculation range shown above.
CRC-16 Calculation Subroutine (BASIC)
CRC_Sum: CRC-16 reserve code after the calculation (CRC content to be sent at end of message)
Data: CRC-16 Data input to be calculated (Byte Data from message)
1000
CRC_Sum = CRC_Sum XOR Data
1010
FOR I=1 to 8
1020
CARRY=CRC_Sum AND 1
1030
CRC_Sum=CRC_Sum SHR 1
1040
IF CARRY=1 THEN CRC_Sum XOR 0A001H
1050
NEXT I
1060
RETURN
CRC-16 Calculation Subroutine (PASCAL)
Procedure CRC16(Data : Byte)
Var i : Byte;
Begin
CRC_Sum := CRC_Sum x or Data;
for i : 1 to 8 do
begin
if((CRC_Sum and 1)=1) then CRC_Sum := (CRC_Sum shr 1) xor $A001;
else CRC_Sum := CRC_Sum shr 1;
end;
End;
CRC-16 Calculation Subroutine (C)
void Crc16(unsigned int Data) {
unsigned int i;
Crc=Crc^(Data & 0x00FF);
for(i=0;i<=7;I++) {
if((Crc & 0x0001) == 0x0001) Crc=(Crc>>1)^0xA001;
else Crc=Crc>>1;
}
}
DA
SA Function
Length
Information
CRC L
CRC H
CRC Calculation Range
2 Bytes
Summary of Contents for D320 PLC
Page 1: ...D320 PLC User s Manual...
Page 18: ...6 D320 PLC User s Manual...
Page 28: ...16 D320 PLC User s Manual...
Page 34: ...22 D320 PLC User s Manual...
Page 78: ...66 D320 PLC User s Manual...
Page 176: ...164 D320 PLC User s Manual...
Page 210: ...198 D320 PLC User s Manual...
Page 258: ...246 D320 PLC User s Manual...