REAL From Two Registers
medium
Reads a REAL that a Modbus device sends in two registers, high word first.
What it teaches
A REAL is 32 bits, which device manuals call a float, and Modbus carries it in two 16-bit registers. COP copies bits without converting them, so copying the two registers into a DINT and then into a REAL rebuilds the value, but it puts register 0 in the low half. When the device sends the high word first, SWPB with WORD swaps the halves back before the bits become a REAL. A wrong order is easy to spot: 24.0 reads as a tiny number near 2.36e-41. Builds on Combine Two Registers and Swap Register Bytes.
Parameters
| Tag | Direction | Type | Default | Description |
|---|---|---|---|---|
| Out_Value | Output | REAL | 0 | The device's REAL value |
| Ref_Registers | InOut | INT[2] | — | Two registers as received; register 0 holds the high word |
Required behavior
- The device sends one REAL in two 16-bit registers, high word first: Ref_Registers[0] holds bits 16-31 of the REAL and Ref_Registers[1] holds bits 0-15.
- Every scan, Out_Value is the REAL those 32 bits spell. Copying the two registers into a 32-bit tag as they are puts register 0 in the low half, so the halves have to swap places before the bits are read as a REAL.
- Registers 16832 (16#41C0) and 0 hold 24.0. Read without the swap, the same bits give a tiny number close to 2.36e-41.
- Never change Ref_Registers.