Converting REAL Floating-Point Values to WORD or DWORD in SIMATIC PCS 7 CFC

Converting REAL Floating-Point Values to WORD or DWORD in SIMATIC PCS 7 CFC

To convert a 32-bit floating-point REAL value (such as the output of mathematical block DIV_R) to a WORD or DWORD in SIMATIC PCS 7 CFC, engineers must select the appropriate conversion block depending on whether they require bit-pattern preservation or numerical rounding. For bit-level preservation (e.g. packing IEEE 754 float registers for Modbus RTU/TCP telemetry), use the R_DW block from the standard library (CFC Library -> ELEMENTA -> CONVERT -> R_DW) and split into words via DW_W. For numerical integer conversion (e.g. converting 123.45 to integer 123), use ROUND or TRUNC to convert REAL to DINT, followed by DI_I to obtain a 16-bit integer/word.

Table of Contents

Data Representation: IEEE 754 Floating-Point vs Integer Bitstrings

In SIMATIC PCS 7 controllers (S7-400 and CPU 410-5H), data types have distinct binary memory structures:

  • REAL (32-bit IEEE 754 Floating-Point): Composed of 1 sign bit, an 8-bit exponent, and a 23-bit mantissa. The mathematical output of blocks like DIV_R (floating-point division) is always formatted as a REAL number.
  • DWORD (32-bit Bitstring): An untyped sequence of 32 bits, often treated as hexadecimal or raw register data.
  • WORD (16-bit Bitstring): An untyped sequence of 16 bits.
  • INT / DINT (16-bit / 32-bit Signed Integers): Two's complement integer numbers.

Engineers frequently need to convert REAL values into WORD or DWORD structures when transmitting process variables across fieldbus protocols (such as Modbus RTU, Modbus TCP, PROFIBUS DP, or OPC) where destination registers are restricted to 16-bit word boundaries.

Bit-Pattern Preservation vs Numerical Rounding in PCS 7

A critical engineering distinction must be made between two conversion objectives:

  1. Case A: Bit-Pattern Transmission (Transmitting IEEE 754 over Modbus):
    - The engineer wants to transmit the exact floating-point value (e.g. 25.75) across two 16-bit Modbus registers.
    - The 32 bits of the IEEE 754 representation must not be rounded or modified; they must be cast directly into a 32-bit DWORD via block R_DW, and then divided into a High Word and Low Word using block DW_W.
    - The receiver reconstructs the exact REAL float by combining the two words.

  2. Case B: Numerical Value Conversion (Integer Truncation/Rounding):
    - The engineer wants the numerical quantity itself converted to an integer (e.g. 25.75 becomes 26 or 25).
    - Passing a REAL through R_DW in this case results in a meaningless hexadecimal bitmask. Instead, the mathematical blocks ROUND (mathematical round to nearest integer) or TRUNC (truncate decimal places) must be used to produce a DINT, which is then converted to an INT/WORD.

CFC Elementary Conversion Blocks Reference Matrix

The following table summarizes the standard CFC library conversion blocks available under ELEMENTA -> CONVERT:

Block NameInput TypeOutput TypeConversion MethodCommon ApplicationR_DWREAL (32-bit)DWORD (32-bit)Bit-pattern preservation (no mathematical change)IEEE 754 float packaging for Modbus / TelecontrolDW_WDWORD (32-bit)2x WORD (16-bit)Splits DWORD into High Word (W1) and Low Word (W0)Modbus register splitting (Word swapping)W_DW2x WORD (16-bit)DWORD (32-bit)Combines two 16-bit words into a single 32-bit DWORDReconstructing 32-bit values from ModbusDW_RDWORD (32-bit)REAL (32-bit)Casts raw 32-bit pattern back into IEEE 754 floatReconstructing received floats from ModbusROUNDREAL (32-bit)DINT (32-bit)Mathematical rounding to nearest whole integerNumerical conversion for counters / presetsTRUNCREAL (32-bit)DINT (32-bit)Drops fractional part (truncation towards zero)Decimal stripping for integer mathDI_IDINT (32-bit)INT (16-bit)Value transfer with overflow clampingConverting 32-bit integer to 16-bit word/int

Step-by-Step Procedure to Implement REAL to WORD Conversion in CFC

Follow this procedure to configure conversion in the CFC Editor:

  1. Identify the Conversion Objective:
    - Determine whether you need to send an IEEE 754 float across two Modbus registers (Procedure A) or convert a float to an integer word (Procedure B).

  2. Implementation for Procedure A (Modbus Float Transmission):
    - In the CFC catalog, expand Libraries -> CFC Library -> ELEMENTA -> CONVERT.
    - Drag block R_DW into the chart.
    - Connect the output Out of your math block (e.g. DIV_R) to the input In of R_DW.
    - Drag block DW_W from the same library folder into the chart.
    - Connect the output Out of R_DW to the input In of DW_W.
    - Connect outputs W1 (High Word) and W0 (Low Word) to your Modbus send data block. Swap W0 and W1 if the slave device uses Little-Endian word ordering.

  3. Implementation for Procedure B (Numerical Integer Word Conversion):
    - Drag block ROUND from ELEMENTA -> CONVERT into the chart.
    - Connect the REAL output of DIV_R to In of ROUND.
    - Drag block DI_I into the chart.
    - Connect output Out of ROUND to In of DI_I. The output of DI_I provides a 16-bit integer/word reflecting the rounded numerical value.

  4. Compile and Download:
    - Select Chart -> Compile -> Changes only.
    - Download changes to the S7-400 controller in RUN mode.

Verification

Verify conversion accuracy in the CFC Editor online view:
1. Open the CFC chart containing the conversion blocks and switch to Online test mode (Test -> Watch on).
2. Apply a test input to DIV_R producing an output of 100.0.
3. For Procedure A (R_DW + DW_W), verify that R_DW outputs 16#42C80000 (hex representation of IEEE 754 100.0). Verify W1 = 16#42C8 and W0 = 16#0000.
4. For Procedure B (ROUND + DI_I), verify that DI_I outputs integer 100 (16#0064).
5. Verify that destination telemetry devices receive and interpret the registers correctly without byte-order corruption.

Limitations and Operational Precautions

  1. Integer Overflow During DI_I Conversion: If the REAL value converted via ROUND or TRUNC exceeds the 16-bit integer limits (-32,768 to +32,767), DI_I will saturate or trigger an arithmetic overflow exception. Always clamp values using limits prior to conversion.
  2. Byte Swapping and Endianness in Modbus: Modbus devices differ in their expectation of register order (Big-Endian vs Little-Endian byte and word swap). If the receiving system reads 100.0 as NaN or astronomical values, invert the connection order of W0 and W1.
  3. Precision Loss in Rounding: Converting a continuous REAL process value (e.g. pressure of 14.87 bar) to an integer word loses fractional resolution unless scaled prior to rounding (e.g. multiply by 100 before rounding to retain 2 decimal places).

Engineer FAQ

Why does R_DW output a weird number like 16#42C80000 instead of 100?

R_DW does not perform mathematical integer conversion; it preserves the raw 32-bit IEEE 754 binary floating-point pattern. 16#42C80000 is the exact hexadecimal representation of single-precision float 100.0.

How do I split a REAL into two words for Modbus communication?

Connect the REAL to R_DW to obtain a DWORD. Connect the DWORD to DW_W, which outputs W1 (most significant word) and W0 (least significant word). Feed W1 and W0 into the Modbus send buffer.

What block should I use if I want 23.6 to become 24?

Use the ROUND block from CFC Library -> ELEMENTA -> CONVERT. It performs standard mathematical rounding of a REAL value to the nearest DINT integer.

Technical References

Related Articles

Related Tags

  • CFC Conversion
  • R_DW
  • DW_W
  • IEEE 754 Float
  • Modbus Registers
  • Data Type Conversion
6 respuestas