IMDHMIControls LibraryShop main store

Troubleshooting

Scaling Modbus floating point values to 4-20 mA outputs

Industrial networks Published 8 min read By IMDHMI controls team

Short answer

Read each gas concentration as a 32-bit float with Modbus function code 3, scale it linearly between its engineering limits onto the analog output module's documented count endpoints, clamp, convert to a 16-bit INT and write it to the channel. Put the controller and analyzer on one subnet, verify the float's word order against the register map, and drive all three outputs to an agreed fault level when the link fails.

On this page
  1. What does converting Modbus values to 4-20 mA actually require?
  2. How do I put the 750-881 and the gas analyzer on one Ethernet network?
  3. Which function code and registers hold the floating point measurements?
  4. Why does a decoded float come back as garbage or near zero?
  5. How do I scale a gas concentration to raw analog output counts?
  6. How do I write the scaled value to the 4-20 mA channel?
  7. What should the outputs do when the Modbus link fails?
  8. How do I prove all three channels end to end?
  9. Questions asked

Turning Modbus values into 4-20 mA is not one operation but a chain of four: read each gas concentration as a 32-bit float with Modbus function code 3 over Ethernet, scale it linearly between its engineering limits onto the analog output module's count endpoints, clamp the result and convert it to a 16-bit INT, then write that INT to the output channel. The register map, the concentration spans and the module's endpoints decide every number in the program, so they get pinned down first. This article walks that chain on a WAGO 750-881 in CODESYS, driving three 4-20 mA outputs from an Ethernet gas analyzer, with the arithmetic worked through and the fault behavior defined.

What does converting Modbus values to 4-20 mA actually require? #

You need four operations in one chain: a Modbus TCP read of each 32-bit float with function code 3, a linear scale from the concentration span to the analog module's raw count endpoints, a clamp with a REAL-to-INT conversion, and a defined fault level for a dead link.

There is no direct protocol conversion between a register and a current loop; the controller is the bridge, and every number it computes comes from interface data someone supplies. Collect these five inputs before writing logic:

  • The function code, start address, register count, data type and word order of each measurement, from the analyzer's register map.
  • The concentration at the low endpoint and the concentration at the high endpoint, for each of the three gases — the spans usually differ.
  • The raw count endpoints of the installed analog output module in its 4-20 mA operating mode, from that module's own documentation.
  • The output level the receiving controller interprets as a fault, agreed with its owner.
  • Fixed addresses for the controller and the analyzer on one subnet.

A missing item stops the program, not the wiring: a guessed endpoint drives a wrong current at every concentration the analyzer reports.

How do I put the 750-881 and the gas analyzer on one Ethernet network? #

Put the controller and the analyzer on the same subnet with fixed addresses — for example the analyzer at 192.168.1.10 and the 750-881 at 192.168.1.20 with mask 255.255.255.0 — and prove the path with a ping before you debug any logic.

The analyzer is the Modbus TCP server and the 750-881 is the client polling it, so both need fixed addresses in one subnet; the example pair above puts them on the same network under one mask. An address of 0.0.0.0 means none is set, and nothing answers through it — exactly what an unconfigured controller shows. In CODESYS, add the analyzer under the controller's Ethernet interface as a generic Modbus TCP device, enter the analyzer's address as the target, and leave the interface settings as configured on the controller. Prove reachability with a ping in both directions first, because a dead route and a bad scale look identical at the output.

One lifecycle note: WAGO documents the 750-881 as discontinued, so keep it for repairing an installed panel and choose a current controller from the same I/O family for a new machine — the chain in this article transfers unchanged.

Which function code and registers hold the floating point measurements? #

Function code 3 is the right choice only when the analyzer's register map lists the measurements in holding registers, and that same map is your single source for the start address, the register count and the word order of every 32-bit float.

Function code 3 reads a contiguous block of holding registers in one request; the classic 4xxxx notation marks that register area, so a map entry such as 40011 is the holding register at offset 10. One IEEE 754 single-precision float spans two consecutive 16-bit registers, so three measurements occupy six registers — read the whole block in one function code 3 request so all three values leave the analyzer together. Word order is the one thing the map must state: some servers send the high word first, some the low word first, and nothing in the frame labels which is which. For one controller family's approach to the same two-register overlay, the article on how the M221 reads Modbus 32-bit floats by overlaying %MW onto %MF shows the idea directly.

Why does a decoded float come back as garbage or near zero? #

If a known value such as 42.0 %LEL decodes as roughly 0 or as an absurd number, the two 16-bit registers of the float are combined in the wrong order: 42.0 is 0x42280000 in IEEE 754, and swapping its words gives a denormal near 2e-41.

Assemble the words in the order the register map documents and the value reads correctly. The signature tells you the fault: a measurement whose low register is small decodes as a denormal close to zero, while one with significant low bits decodes as an enormous nonsense number — both mean the same wrong word order, fixed by swapping the two words where you combine them. If a single-register value in the same map also reads wrong, the sign interpretation is off; Converting an unsigned 16-bit Modbus word to a signed integer covers that case. Fix the decode before touching any scaling, because a wrong value scaled perfectly still drives a wrong current.

How do I scale a gas concentration to raw analog output counts? #

Scale linearly between the channel's two endpoint counts: with the example pair 0 and 10000 counts under a 0-100 %LEL span, a reading of 42.0 %LEL produces 4200 counts, which the current loop carries as 10.72 mA.

Linear scaling is one line of arithmetic per channel:

raw counts = rawLow + (engineering - engLow) × (rawHigh - rawLow) ÷ (engHigh - engLow)

Worked example, with every input labeled as an example: the methane channel represents 0 to 100 %LEL across the loop; the endpoint pair is 0 counts at the low end and 10000 counts at the high end, a round illustrative pair rather than the module's documented value, which comes from the module's data sheet; the analyzer reports 42.0 %LEL.

raw counts = 0 + (42.0 - 0) × (10000 - 0) ÷ (100.0 - 0) = 4200.0

Current check: that count is 4200 ÷ 10000 = 0.42 of the span, so the loop carries 4 mA + 0.42 × 16 mA = 10.72 mA.

Clamp the engineering value to its span before the multiply, so an over-range reading maps to the endpoint instead of past it, and guard the divide — equal engineering limits mean a misconfigured span and would be a divide-by-zero. Write the scaling as one reusable block and call it three times, because the three gases rarely share a span; it is the same linear map covered in Scale a PID real output to analog output counts, with a different source value.

How do I write the scaled value to the 4-20 mA channel? #

Clamp the scaled REAL to the channel's count endpoints, convert it to a 16-bit INT, and write that INT to the channel's output word — the example's 4200.0 becomes 4200, well inside the 16-bit register limit of 32767 — and the module's output stage turns counts into current.

Keep the chain in REAL until this last step, because converting early loses resolution. Clamp first, convert second: a clamped value cannot exceed the channel's endpoints, so the conversion stays inside the register's 32767 limit and cannot wrap. Write the INT to that channel's output word and let the module's output stage do the count-to-current conversion; its operation is covered in How PLC analog input and output modules convert signals. One output word per measurement, one scaling-block call per channel, executed every scan so the current always tracks the latest good reading.

Decide the fault behavior before startup and write it into the program: on three consecutive failed reads or 5 seconds without a good frame — example settings, pick one — drive every channel to the level the receiving controller interprets as a fault and raise a diagnostic bit.

An output word that is only written after good reads keeps driving its last value through a dead link, which the receiving controller reads as a live measurement. Count consecutive failed requests in the program and act when the count or a timer trips: the example policy trips on three consecutive failures or 5 seconds without a good frame, writes the agreed fault level to all three channels and sets a diagnostic bit the operator can see. When a good frame returns, clear the watchdog and resume normal writes automatically. If these currents feed a gas alarm or shutdown elsewhere, the fallback level belongs to that function's design — agree it with the receiving system before startup rather than treating this repeater chain as the safety function itself.

How do I prove all three channels end to end? #

Prove each channel at three points — the low endpoint, a mid value and the high endpoint — by measuring the loop current and comparing the reading the receiving controller shows, then pull the Ethernet cable and watch every output go to the agreed fault level.

Work through this sequence on each channel before handover:

  1. Hold the analyzer at a known concentration and compare each decoded float against the analyzer's own display.
  2. Force the engineering low limit and measure the loop current; the example channel reads 4 mA there.
  3. Force half the span; the example gives 5000 counts and 12.0 mA.
  4. Force the engineering high limit and measure 20.0 mA at the receiving device.
  5. Force 110 percent of span and confirm the clamp holds the current at the high endpoint instead of wrapping past it.
  6. Disconnect the Ethernet path and confirm all three outputs drive the agreed fault level, then reconnect and confirm automatic recovery.

The decision for this machine: one grouped function code 3 read of the six holding registers the map lists, the decode verified against the analyzer display, one reusable scaling block called three times with each gas's span onto the module's documented endpoints, a clamp with a REAL-to-INT conversion before every write, and an agreed fault level on a watched link — proven channel by channel before handover. On an installed 750-881 that chain runs as designed; on a new panel, run it on a current controller from the same I/O family.

Questions asked

How many registers does one 32-bit float occupy on Modbus?

Two consecutive 16-bit registers hold one IEEE 754 single-precision value, so three gas measurements occupy six registers. Read all six in one function code 3 request covering the block, so the three values leave the analyzer from the same update; a receiving controller that compares them against each other then works from one consistent snapshot, and the link carries one request per scan instead of three.

Is the 750-881 still a sensible controller for this job?

WAGO documents the 750-881 as discontinued. For repairing or extending an existing panel it runs this chain unchanged, and replacement planning follows the manufacturer's discontinuation bulletin. For a new panel, choose a current Ethernet controller from the same I/O family; the register handling, the scaling arithmetic and the fault logic transfer without redesign.

Where do the output channel's endpoint numbers come from?

From the documentation of the installed analog output module, for its 4-20 mA operating mode; every module family defines its own count range. If the module sets its range by parameters, select the current-loop mode there first and read the endpoints for that mode. The worked example's round endpoint pair is illustrative only and must not be typed in as if the module documented it.

Why does an output hold its last value when the link drops?

Because the program only writes the channel word after a good read, so the module keeps driving whatever count it received last, and the receiving controller sees that as a live measurement. A watchdog that counts failed requests and then writes the agreed fault level to every channel removes the silent freeze, and it clears itself when good frames return.

Can all three gases share one scaling routine?

Yes. Write the scaling once as a block with four real inputs — engineering low, engineering high and the two raw count endpoints — and keep the clamp and the integer conversion inside it, then call it three times with each measurement's span. No channel can then skip the clamp or convert at the wrong point in the chain.

Keep reading

  • How-to

    Output 8 mA and 4 mA from an S7-200 EM232 output with MOV_W

    On the EM232 current output, 0 to 20 mA maps onto a 0 to 32000 data word, so the 8 mA run command is MOV_W 12800 to AQW0 and the 4 mA stop command is MOV_W 6400. Hold one sequence state that I0.0 sets and I0.1 clears, drive the motor from that state, and let it pick which word is written so two sensors never fight over the output.

    Controllers and I/O

  • How-to

    Set up a CLICK PLC 4-20 mA pressure trip with a relay output

    Wire the two-wire loop into channel 1 of a C0-04AD-1, set that channel to 4-20 mA with 4 mA scaled to 0 PSI and 20 mA to 300 PSI in DF1, and drive relay output Y1 from Set and Rst coils that trip above 250 PSI and release below 230 PSI, with a loop-fault test below -10 PSI.

    PLC programming

  • Explainer

    Can a Modbus write change PLC outputs?

    Yes, but not by outrunning the scan. On a Modicon CPU the communication interface reaches data memory only between scans, so a write is applied whole and the next scan either consumes it or overwrites it. The real exposure is that Modbus TCP authenticates nothing: any host that can reach the CPU can write an unprotected holding register your logic unpacks into outputs, so pack status into read-only words, keep commands in a separate writable range, and enforce write protection with an IP access control list.

    Industrial networks