crcCalculate (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

1.08 / 1.00.00


This function calculates, by standard, the CRC-CCITT 16 (16 bit polynomial) checksum of a buffer.

By using the CRC-CCITT 16 polynomial, an overall error detection rate of 99.955% is achieved.

 

By adjusting the parameters, a wide range of CRC polynomials/algorithms can be realised.

Please consult relevant literature for further information.

 

 

Input:

buffer : PTR

Pointer to the buffer that is used to calculate the CRC.

 

length : DINT

Number of bytes in the buffer.

 

polynom : DINT (Default: 16#00001021)

The polynom used to calculate the CRC.

Default is X^16 + X^12 + X^5 + 1.

 

preset : DINT (Default: 16#0000FFFF)

The preset CRC value.

 

finalxor : DINT (Default: 16#00000000)

This value is XOR'ed with the CRC calculated from the buffer just before it is returned.

 

order : SINT (1..32, Default: 16)

The number of bits used in the CRC.

 

direct : BOOL (Default: TRUE)

Calculates the CRC with (FALSE) or without (TRUE) augmented zero bits.

 

reversedata : BOOL (Default: FALSE)

Reverses data bytes (TRUE) before CRC is calculated.

 

reversecrc : BOOL (Default: FALSE)

Reverse calculated CRC (TRUE) before finalxor is applied.

 

Returns: DINT

The calculated CRC of the buffer.

 

Declaration:

FUNCTION crcCalculate : DINT;
VAR_INPUT
  // Data buffer
  buffer     : PTR;
  length     : DINT;
  // CRC algorithm parameters
  polynom     : DINT := 16#00001021;
  preset     : DINT := 16#0000FFFF;
  finalxor   : DINT := 16#00000000;
  order       : SINT := 16;
  direct     : BOOL := TRUE;
  reversedata : BOOL := FALSE;
  reversecrc : BOOL := FALSE;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
PROGRAM test;
VAR
  buffer : ARRAY[1..9] OF SINT;
  crc16 : INT;
  crc32 : DINT;
END_VAR;
 
  // Test data
  strToMemory(dst:=ADDR(buffer), str:="123456789", len:=9);
 
  // Calculate CRC-CCITT16
  DebugMsg(message:="****************************************");
  DebugMsg(message:="CALC_CRC: (CRC-CCITT16)");
  crc16 := INT( crcCalculate(buffer:=ADDR(buffer), length:=9) );
  DebugFmt(message:="-CRC = \1", v1:=crc16);
 
  DebugMsg(message:="****************************************");
  DebugMsg(message:="CALC_CRC: (CRC16)");
  DebugMsg(message:="-Order = 16");
  DebugMsg(message:="-Polynom = 16#00008005");
  DebugMsg(message:="-Preset = 16#00000000");
  DebugMsg(message:="-Direct = TRUE");
  DebugMsg(message:="-Finalxor = 16#00000000");
  DebugMsg(message:="-ReverseData = TRUE");
  DebugMsg(message:="-ReverseCRC = TRUE");
  crc16 := INT(
              crcCalculate(
                            buffer:=ADDR(buffer),
                            length:=9,
                            Order:=16,
                            Polynom:=16#00008005,
                            Preset:=0,
                            Direct:=TRUE,
                            Finalxor:=0,
                            ReverseData:=TRUE,
                            ReverseCRC:=TRUE
                          )
              );
  DebugFmt(message:="-CRC = \1", v1:=crc16);
 
  DebugMsg(message:="****************************************");
  DebugMsg(message:="CALC_CRC: (CRC32)");
  DebugMsg(message:="-Order = 32");
  DebugMsg(message:="-Polynom = 16#04C11DB7");
  DebugMsg(message:="-Preset = 16#FFFFFFFF");
  DebugMsg(message:="-Direct = TRUE");
  DebugMsg(message:="-Finalxor = 16#FFFFFFFF");
  DebugMsg(message:="-ReverseData = TRUE");
  DebugMsg(message:="-ReverseCRC = TRUE");
  crc32 := crcCalculate(
                        buffer:=ADDR(buffer),
                        length:=9,
                        Order:=32,
                        Polynom:=16#04C11DB7,
                        Preset:=16#FFFFFFFF,
                        Direct:=TRUE,
                        Finalxor:=16#FFFFFFFF,
                        ReverseData:=TRUE,
                        ReverseCRC:=TRUE
                      );
  DebugFmt(message:="-CRC = \4", v4:=crc32);
 
BEGIN
END;
 
END_PROGRAM;