IMDHMIControls LibraryShop main store

How-to

How to read Factory I/O REAL and INT tags in CODESYS Modbus TCP

PLC programming Published 6 min read

Short answer

Factory I/O multiplies each Float sensor value by 100 and sends it as a 16-bit Word, and its Int tags cross as 32-bit values. In CODESYS, receive the Word, convert it to REAL, divide by 100.0, and declare each Int tag as a DInt; BOOL tags pass straight through the Modbus TCP slave mapping you already have.

On this page
  1. Why do my BOOL tags work while REAL and INT tags do not?
  2. What does Factory I/O actually put in the registers?
  3. How do I turn the received Word into a REAL?
  4. How much resolution and range does the ×100 scaling leave?
  5. How do I declare and map the Int tags?
  6. How do I add the conversion without breaking the BOOL setup?
  7. How do I verify the converted values?
  8. Questions asked

Receive each Factory I/O Float sensor value as a 16-bit signed WORD, convert it to REAL, and divide by 100.0. Declare each Int tag as a DInt. Leave the BOOL mappings exactly as they are, because they already work. Factory I/O's Modbus TCP/IP client never transmits a native REAL: it multiplies every Float sensor value by 100, converts the result to an integer, and sends it in one Word, so the scaling arithmetic belongs in your CODESYS program. The Int type also crosses as a 32-bit value, which is why a plain CODESYS INT shows the wrong magnitude. What follows keeps your working slave-device configuration untouched and adds one conversion per numeric tag.

Why do my BOOL tags work while REAL and INT tags do not? #

Because BOOLs cross the link as single bits and need no arithmetic, while Factory I/O never transmits a native REAL: its Modbus TCP/IP client multiplies each Float sensor value by 100, sends the result as a signed 16-bit Word, and sends Int tags as 32-bit values. Your BOOL mappings were bit-for-bit correct from the start, so they worked the moment the link came up. Numeric tags carry magnitude, and magnitude over Modbus lives in 16-bit registers, so the driver's encoding rules decide what arrives and your program has to decode it.

This article assumes the arrangement you already have: CODESYS runs the Modbus TCP slave device and Factory I/O runs the Modbus TCP/IP client driver. If your project is the other way round, with CODESYS polling outward, the configuration differs from the first step, and setting up a CODESYS Modbus TCP client to talk through a gateway covers that role.

What does Factory I/O actually put in the registers? #

One 16-bit signed Word per Float sensor, scaled ×100; one 32-bit integer across two consecutive registers per Int tag; and one bit per BOOL, with the register values decoded in your program. On the CODESYS side that means a WORD, a DInt and a BOOL declaration, plus a single divide for the floats.

Factory I/O type What Modbus carries CODESYS declaration Conversion you add
BOOL one bit BOOL none
Float sensor value one signed 16-bit register, value ×100 WORD, then REAL Word to REAL, divide by 100.0
Int 32-bit integer across two registers DInt none

Take each numeric address from the tag list the Factory I/O client driver shows for the scene. Do not count forward from the BOOL addresses: in Modbus the bit area (coils and discrete inputs) and the register area (holding and input registers) are separate address spaces, so a bit number tells you nothing about register numbering.

How do I turn the received Word into a REAL? #

Convert the raw WORD to REAL, then divide by the REAL literal 100.0; in structured text the whole decode is one assignment, and the divisor must be written 100.0 so the division is REAL arithmetic end to end.

VAR
    wRaw      : WORD  := 0;    // raw register written by Factory I/O
    rDistance : REAL  := 0.0;  // engineering value, sensor's own unit
END_VAR

rDistance := WORD_TO_REAL(wRaw) / 100.0;

The variable names are illustrative; WORD_TO_REAL is the standard IEC 61131-3 conversion operator name for Word-to-Real in CODESYS structured text. Worked example: the scene's distance sensor reads 34.56, so Factory I/O computes 34.56 × 100 = 3456 and transmits the Word 3456. Your program computes WORD_TO_REAL(3456) = 3456.0, then 3456.0 / 100.0 = 34.56, and the REAL matches the scene to the hundredth. That match is the test that proves the decode.

How much resolution and range does the ×100 scaling leave? #

Hundredth resolution, a ceiling of 327.67 and a floor of −327.68, because the scaled value lives in one signed 16-bit register that spans −32768 to 32767. The boundary is arithmetic, not a rule of thumb: 327.67 × 100 = 32767, the largest value a signed Word holds, so 327.67 is the largest engineering value the link can carry. Keep scene magnitudes inside that span by design; a value beyond it does not fit in the register and the reading you get is not trustworthy. Digits finer than the hundredth never cross the link.

How do I declare and map the Int tags? #

Declare each Factory I/O Int tag as a DInt — 32 bits spanning −2,147,483,648 to 2,147,483,647 — and map it across the two consecutive registers at the address the Factory I/O client configuration lists. The mapping mechanics are the same tab you used for the BOOLs, and mapping physical I/O channels in CODESYS V3.5 walks that I/O mapping in detail.

Word order is the one trap in a 32-bit value. Modbus transmits the most significant byte first inside each 16-bit register, but the protocol does not fix which of the two words of a 32-bit pair goes first. In the reported pairing, Factory I/O 2.5.1 with CODESYS 3.5.17.60, a plain DInt at the mapped address read correctly, so map directly and change nothing unless a value comes back wrong by a factor of 65536 — that signature means the pair is reversed, and moving the mapping one register fixes it.

How do I add the conversion without breaking the BOOL setup? #

Leave the Modbus TCP slave device and every BOOL mapping untouched, declare one WORD and one REAL per Float sensor and one DInt per Int tag, point each numeric variable at the register the Factory I/O configuration lists for it, and add one conversion line per sensor in the program. In full:

  1. Open the Modbus TCP slave device in the device tree and note the register address the Factory I/O client configuration lists for each numeric tag.
  2. Declare the variables: one WORD plus one REAL per Float sensor, one DInt per Int tag, and no changes to any BOOL declaration.
  3. Map each WORD and DInt to its register in the slave device's I/O mapping tab, next to the BOOL bits that already work.
  4. Add the decode — one line per sensor in structured text, rTag := WORD_TO_REAL(wTag) / 100.0; — or one rung per sensor in ladder with the same arithmetic.
  5. Download, put the scene in run mode, and confirm a BOOL sensor and actuator still toggle before you look at a single number.
  6. Only then judge the numerics with the checks below.

How do I verify the converted values? #

Watch the raw WORD and the converted REAL side by side: when the raw register shows 3456 the REAL must show exactly 34.56, and every other pair must obey the same divide-by-100 relationship. Three checks settle it. BOOLs still toggle, which proves the link and leaves the mapping as the only suspect. The raw and converted pair holds across a sweep: drive one sensor over its range and confirm both ends, 0 on the raw giving 0.00 on the REAL and 100.00 on the scene arriving as 10000 raw. Every DInt matches its scene integer exactly.

The behavior is documented driver data handling rather than a version quirk, confirmed in the pairing named above. One caution that outlives the simulator: prove the 100.0 divisor against a known scene value before reusing this pattern on real plant I/O, because a factor-of-100 error on a real setpoint is an unsafe command, not a wrong display. If you would rather avoid scaling altogether, OPC UA carries typed values natively, and connecting Factory I/O to a local CODESYS soft PLC over OPC UA shows that pairing.

Questions asked

Does the ×100 scaling carry negative sensor values?

Yes, through sign. The register is a signed 16-bit Word, so −25.4 is transmitted as −2540, the Word-to-REAL conversion preserves the sign, and your divide by 100.0 returns −25.40. Only the magnitude limit applies on the negative side: the most negative value the link can carry is −327.68.

Why does the converted value move in steps of 0.01?

Because the scale stores hundredths: 12.34 travels as 1234 and 12.35 as 1235, and nothing between them exists on the wire. A sensor showing 12.345 therefore cannot arrive exactly, and the converted REAL settles on 12.34 or 12.35 depending on how the driver rounds the third digit. For simulation scenes that granularity is finer than any actuator in the scene can use.

What does a DInt reading of 19,660,800 tell me?

That the two 16-bit halves of a 32-bit value arrived in the wrong order. Modbus sends the most significant byte first within a register but does not fix which word of a pair comes first: 300 transmitted as word 0 then word 300 and read reversed becomes 300 × 65536 = 19,660,800. Move the DInt mapping or reorder the pair and the value lands on 300.

Can I build the conversion in ladder instead of structured text?

Yes. Build one rung per Float sensor: a Word-to-REAL conversion feeding a division by the REAL constant 100.0, writing to your REAL tag, with the raw WORD left visible for troubleshooting. The arithmetic is identical to the structured-text line, so the same raw-to-REAL pair check proves either version.

Does this still apply to current Factory I/O and CODESYS versions?

The confirmed pairing was Factory I/O 2.5.1 with CODESYS 3.5.17.60, but the ×100-into-a-Word behavior is the Modbus TCP/IP client driver's documented data handling, not a quirk of those releases. Treat it as current until the driver's own documentation says otherwise; only a documented change there would move your divide by 100.0.

Keep reading

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming

  • Troubleshooting

    Why a Micro850 will not read tags over a DF1 I/O server

    A Micro850 serves variables through a Modbus map you build in Connected Components Workbench, not through DF1 or ABLOGIX I/O servers, because its memory has no data-table files for those drivers to read. Map each variable, poll the mapped range with an independent Modbus client on unit identifier 1, and set the tag address to the offset that actually answers.

    Industrial networks

  • Troubleshooting

    Why MELSECNET II data reads zeros after adding a Modbus card

    The zeros are not a network fault. The QJ71MB91 occupies 32 I/O points, and inserting it ahead of the MELSECNET II module moved that module's start I/O number, so the access code reads the wrong position. Diff the I/O assignment against the map from before the change, re-address the assignment so the MELSECNET II module sits at 0070, and update every instruction that targets it.

    PLC programming