IMDHMIControls LibraryShop main store

Troubleshooting

Converting an unsigned 16-bit Modbus word to a signed integer

Industrial networks Published 6 min read

Short answer

The module is sending offset-binary counts for a bipolar input, and the controller reads them as two's complement, so every count from 32768 up arrives negative. Subtract 65536 from any count at or above 32768, or set the channel to a unipolar range so the wrap disappears entirely.

On this page
  1. Symptoms that identify this fault
  2. Offset binary versus two's complement
  3. The conversion rule
  4. Converting the raw count at 0.001 V
  5. Fix 1: set the channel to a unipolar range
  6. Fix 2: subtract the offset in the controller
  7. Deciding between the two fixes
  8. Questions asked

Your register reads 32770 in a Modbus master but -32766 in the controller, and the controller value moves toward 0 as the real signal rises. The cause is a data-format mismatch: the analog module is sending offset-binary counts for a bipolar input range, and the controller is interpreting each 16-bit word as two's complement. Fix it either by changing the channel to a unipolar input range so no offset is transmitted, or by subtracting 65536 from every raw count of 32768 or more before you scale.

Symptoms that identify this fault #

The pattern is consistent across integrations of bipolar analog modules over Modbus:

  • A master tool shows a count that rises smoothly with the input signal, crossing mid-scale.
  • The controller value flips from positive to negative as the real signal passes the zero point of the sensor range.
  • Above mid-scale, the controller value climbs toward 0 while the true signal keeps increasing.
  • Very large counts saturate: the controller's analog input holds -32768 or +32767 instead of tracking.

If the master count is monotonic and the controller value is not, the wiring, address and register map are fine; the problem is only how the word is interpreted.

Offset binary versus two's complement #

A 16-bit word carries the same 16 bits either way; only the interpretation differs:

Bit pattern Unsigned / offset binary Two's complement signed
0x7FFF 32767 32767
0x8000 32768 -32768
0x8002 32770 -32766
0xFFFF 65535 -1

On a bipolar range such as 10 V, the module maps the negative full scale to 0 and the positive full scale to 65535, so a zero input sits at mid-scale, about 32768. A controller that treats the word as signed reads that mid-scale count as roughly -32768, which is exactly why a sensor at rest shows a large negative number. The Modbus protocol itself defines the register as 16 bits without a signedness; the data format is a property of the slave device's encoding, not of Modbus.

The conversion rule #

For a two's complement interpretation of an unsigned word:

  • If the raw count is 32767 or less, the signed value equals the raw count.
  • If the raw count is 32768 or more, the signed value equals the raw count minus 65536.

That single subtraction is the whole conversion. This page on the 16-bit signed integer subtraction covers the arithmetic in more depth. The catch on some small controllers is that the analog network input block clamps or wraps before your logic sees the word, so apply the comparison on the raw unsigned value wherever the module lets you read it as a word.

Converting the raw count at 0.001 V #

This worked example uses the values from the reported case, a bipolar 10 V channel on an 8-channel analog input module:

Inputs. Input range: 10 V bipolar (module configuration). Applied input: 0.001 V from the load cell amplifier at zero load. Raw register count: 32770, read with a master tool. Signed range of the controller input: -32768 to +32767.

Rule. A count of 32768 or more is negative in two's complement: subtract 65536.

Arithmetic. 32770 - 65536 = -32766.

Result. The controller receives -32766 for a sensor sitting essentially at 0 V, and it will keep climbing toward -1 as the voltage rises toward positive full scale, where the count reaches 65535 and the signed value becomes -1.

Decision. -32766 is not a fault and not a wiring error; it is 32770 minus 65536, about 2 counts above mid-scale, or roughly 0.001 V on a 10 V range. Any count you see between 32768 and 65535 converts the same way; counts of 32767 and below pass through unchanged.

Fix 1: set the channel to a unipolar range #

If your sensor never goes negative, the cleanest fix is to remove the offset at the source. The module in this case is a 16-bit, 8-channel differential input unit with independently programmable ranges per channel; its accepted voltage types include 10 V bipolar and 0 V to 10 V unipolar, and it can also transmit engineering units directly. Set the channel from the bipolar range to 0 V to 10 V and the count becomes 0 at zero input, so no conversion is needed downstream. Better still, enable the module's own scaling so it returns 0 to 10000 for 0 V to 10 V and the controller gets finished engineering units with no arithmetic at all.

Range configuration lives in the module's configuration utility or its built-in web pages, not in runtime registers. One integration ran into a utility build, version 2.6.00 (B04), where the unipolar choice was missing from the channel drop-down; connecting to the module's internal website in a browser is the alternative route to the same per-channel settings. After any range change, power-cycle the module and confirm with a master tool that zero input now reads near 0 rather than mid-scale.

Fix 2: subtract the offset in the controller #

If the sensor genuinely swings negative around a tare, keep the bipolar range and convert in logic. In a function-block environment, build one small block per channel:

  1. Read the register as an unsigned word (0 to 65535).
  2. Compare the raw count against 32768.
  3. On the negative half, subtract 65536; on the positive half, pass through.
  4. Scale the signed result, -32768 to +32767, onto your engineering range.

For a load cell that must show 0 to 10000 around a mid-scale zero, scale the signed value after conversion rather than before. Where you need coarse indication only, a pre-scale by 10 before the comparison keeps every intermediate value inside the signed 16-bit envelope, trading precision you do not need. Analog scaling details for input modules generally are covered in the article on how PLC analog input and output modules convert signals, and the same clamp behavior appears in the article on an analog input that saturates on wire break.

Deciding between the two fixes #

  • Sensor signal never negative: set the channel to the unipolar range, and enable module-side scaling if it is offered. No controller logic changes, full resolution, no wrap risk.
  • Sensor signal genuinely bipolar: keep the bipolar range, subtract 65536 from counts of 32768 and above, then scale once. This preserves the full signed span.
  • Controller input clamps at 32767 or -32768 before your logic runs: you must fix the format at the module, because the clamped value no longer carries the information the subtraction needs.

One safety note: before changing any analog channel that feeds an interlock, alarm or shutdown comparison, force the associated logic to a safe state and revalidate the comparison limits after the range or conversion change, because every threshold in that loop moves with the data format.

Questions asked

Why does my analog value run toward zero as the real signal increases?

With a bipolar range the module's count rises from mid-scale toward 65535 as the signal grows. The controller treats the top half of that count range as negative, so the signed value climbs from -32768 toward -1, which reads as moving toward zero. A master tool that shows unsigned words still displays the count rising normally, which is the fastest way to confirm this data-format mismatch rather than a wiring fault.

Is a value of 32767 or 32768 on a bipolar channel a fault?

No. On a bipolar range zero input sits at mid-scale, so a count of about 32768 means 0 V or 0 mA at the terminal, not an overrange. The same modules also return special codes for broken wire or overrange on some ranges, so check the module manual for the exact code before treating a stuck maximum as a real signal.

Can I do the conversion in the controller instead of the module?

Yes. Read the register as an unsigned word, subtract 65536 from any count of 32768 or more, and keep counts below that as they are. If the controller's analog network input clamps instead of wrapping, do the comparison on the raw word before any signed math. This costs one comparison and one subtraction per channel and preserves the full 16-bit precision.

What if the engineering software has no unipolar range in the drop-down?

Older utility builds have shown the bipolar and unipolar lists incomplete for some module versions. Update the utility to a current build, or configure the range through the module's own web pages in a browser, which expose the same per-channel input type settings. After any range change, reboot the module and re-read the register with a master tool to confirm zero input now sits near 0 rather than mid-scale.

Keep reading

  • How-to

    How a PLC runs two Modbus networks at the same time

    Yes. Each serial port is its own independent link: the HMI polls the PLC on one port while the PLC acts as master, or slave, on a separate RS-485 Modbus RTU network. The two links share no settings — only their endpoints must match — and PLC copy logic moves any data between them.

    Industrial networks

  • How-to

    How to run analog I/O over a Modbus TCP bus coupler

    Yes: remote analog I/O behind a Modbus TCP bus coupler can close a control loop from the PLC. Prove the electrical path with the network out of the loop, bring up the connection with a real timeout, map and scale the channels against known test points, then close the loop on measured timing — one documented integration refreshes coupler data every 10 ms, but the valve and hydraulics set the real response, and a failed exchange is a fault state, never fresh data.

    Controllers and I/O

  • Troubleshooting

    Reading a CLICK PLC float over Modbus TCP in an HMI

    F428673 is the right address: the F prefix reads the two Modbus registers as the IEEE 754 float the CLICK program wrote to DF1 (24.9645 here), while L returns the raw 32-bit pattern (1103607636) and U returns one 16-bit half (46932). The screen showed 25 because an integer tag or whole-number format rounded it. Move the project to 3.99w and bind F with a decimal format and a floating-point variable.

    SCADA and data logging