# LGF_CalcCRC16 (FC / 3.1.0)

Overview

Author: Siemens Industry Support

## Short description

The CRC calculation is used for error detection at data transmission. The result of a calculation returns a CRC value via the data sent. The receiver detects a faulty transmission due to the unequal CRC value. The function `LGF_CalcCRC16` uses 16 bits as the generator polynomial (mask).

### Block Interface

| LGF_CalcCRC16 (FC) |
--- | --- | ---
Word |  |  | initValue |  | Ret_Val |  |  | Word
Word |  |  | mask |  | error |  |  | Bool
UInt |  |  | noOfElements |  | status |  |  | Word
Array[*] of Byte |  |  | array |  |  | Array[*] of Byte

### Input parameter

Identifier | Data type | Description
--- | --- | ---
initValue | Word | Start value with which the calculation is executed. If there is no need for start value - assign 16#00
mask | Word | Generator polynomial with which the calculation is executed. (Mask / CRC polynomial)
noOfElements | UInt | Number of elements to be used in CRC calculation 0 = all elements / the whole array

### Output parameter

Identifier | Data type | Description
--- | --- | ---
Ret_Val | Word | Calculated CRC value (return value of the function).
error | Bool | TRUE: An error occurred during the execution of the FB
status | Word | 16#0000 - 16#7FFF: Status of the FB, 16#8000 - 16#FFFF: Error identification

### In/Out parameter

Identifier | Data type | Description
--- | --- | ---
array | Array[*] of Byte | Data stream for which the CRC value will be calculated.

### Status & Error codes

Code / Value | Identifier / Description
--- | ---
16#0000 | STATUS_NO_ERROR No error occurred in function call
16#8400 | ERR_NO_OF_ELEMENTS Error: `noOfElements` exceeds the maximum number of array elements

## Functional description

The function calculates the CRC value from a data stream of any size. The data stream is composed of the individual elements of the array at the input/output parameter `array`. The start value `initValue` and the generator polynomial `mask` can be freely selected.

The input `noOfELements` can be used to specify the desired number of elements for calculation, it applies:

NumberOfElements <= (ArrayUpperLimit - ArrayLowerUnderLimit + 1)

Note
  Various online tools are available for calculating the CRC values. The function of the block was tested with the following online tool, since it supports the input parameters `mask` (Polynomial) and `initValue` (Initial Value):
  http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

## Change log

Version & Date | Change description
--- | ---
| 1.0.0 | Siemens Industry Online Support
| 03.07.2018 | First released version
| 1.0.1 | Siemens Industry Online Support
| 17.08.2018 | Upgrade: TIA V15 Update 2
| 1.0.2 | Siemens Industry Online Support
| 23.11.2018 | Upgrade: TIA V15.1
| 1.0.3 | Simatic Systems Support
| 09.10.2019 | Code refactoring, comments added
| 3.0.0 | Simatic Systems Support
| 23.04.2020 | Set version to V3.0.0, harmonize the version of the whole library
| 3.0.1 | Simatic Systems Support
| 12.11.2020 | Insert documentation Assign default start values to optional inputs - `initValue`, `mask`
| 3.1.0 | Simatic Systems Support
| 14.04.2023 | Add input `noOfElements` to assign length to be converted different from array size Add outputs `error` and `status` display a wrong assignment to `noOfElements`