beckhoff TE1200 — 4.2.1.1.20 SA0020: Possibly assignment of truncated value to REAL variable

Configuration | Rules 4.2.1.1.19 SA0019: Implicit pointer conversions | SA0019: Implicit pointer conversions Samples: PROGRAM MAIN VAR nInt : INT; nByte : BYTE; pInt : POINTER TO INT; pByte : POINTER TO BYTE; END_VAR pInt := ADR(nInt); pByte := ADR(nByte); pInt := ADR(nByte); // => SA0019 pByte := ADR(nInt); // => SA0019 pInt := pByte; // => SA0019 pByte := pInt; // => SA0019 4.2.1.1.20 SA0020: Possibly assignment of truncated value to REAL variable | SA0020: Possibly assignment of truncated value to REAL vari ... Sample: PROGRAM MAIN VAR nVar1 : DWORD; nVar2 : DWORD; fVar :

REAL; END_VAR nVar1 := nVar1 + DWORD#1; nVar2 := nVar2 + DWORD#2; fVar := nVar1 * nVar2; // => SA0020 TE1200 29 Version: 2.11.1 Function Determines implicitly generated pointer data type conversions. Reason In TwinCAT, pointers are not strictly typed and can be assigned to each other arbitrarily. This is a commonly used option and therefore not reported by the compiler. However, this may result in unwanted and unexpected access. For example, assigning the type POINTER TO DWORD to a pointer with the type POINTER TO BYTE can result in unwanted memory overwriting.

Therefore, always check this rule and suppress the message only in cases where you deliberately want to access a value with a different type. Implicit data type conversions are reported with a different message. Exceptions 1) BOOL ↔ BIT 2) Conversions from and to the POINTER TO BYTE data type are not checked by rule SA0019. They are always allowed. Importance High PLCopen rule CP25 Function Determines operations on integer variables, during which a truncated value may be assigned to a variable of data type REAL. Reason The static code analysis returns an error when the result of an integer calculation is assigned to a REAL or LREAL variable.

The programmer should be made aware of a possibly incorrect interpretation of such an assignment: fLEAL := nDINT1 * nDINT2. Since the value range of LREAL is greater than that of DINT, it could be assumed that the result of the calculation is always displayed in LREAL. But this is not the case. The processor calculates the result of the multiplication as an integer and then casts the result to LREAL. An overflow in the integer calculation would be lost. To avoid this problem, the calculation should be performed as a REAL operation: fLREAL := TO LREAL(nDINT1) * TO LREAL(nDINT2) _ _ Importance High