# Modbus Sensor Is Not Reading a Negative Number Correctly

**URL:** <https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184>\
**Category:** Programming\
**Created:** [March 29, 2023, 2:55am UTC](https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184 "2023-03-29T02:55:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon11653851](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@anon11653851](https://forum.arduino.cc/u/anon11653851)\
**Post date:** [March 29, 2023, 2:55am UTC](https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184/1 "2023-03-29T02:55:58Z")

</div>

Hello, I have my Modbus sensor reading on a Arduino Opta RS485. It is working fine when reading a positive number but as soon as it goes to read a negative number it reads 65XXX. I cannot figure out how to get it to read a negative number. Thanks n advanced for any help!  
My code

```cpp
/**
  Getting Started with Modbus RTU on Opta™
  Name: Opta_Client
  Purpose: Writes Coil and Holding Register values; Reads Coil, Discrete Input, Holding Registers, and Input Register values.

  @author Arduino
*/

#include <ArduinoModbus.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library

constexpr auto baudrate { 19200 };

// Calculate preDelay and postDelay in microseconds as per Modbus RTU Specification
// MODBUS over serial line specification and implementation guide V1.02
// Paragraph 2.5.1.1 MODBUS Message RTU Framing
// https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
constexpr auto bitduration { 1.f / baudrate };
constexpr auto preDelayBR { bitduration * 9.6f * 3.5f * 1e6 };
constexpr auto postDelayBR { bitduration * 9.6f * 3.5f * 1e6 };
// constexpr auto preDelayBR { bitduration * 10.0f * 3.5f * 1e6 };

int counter = 0;

void setup() {
    Serial.begin(9600);
    while (!Serial);

    Serial.println("Modbus RTU Client");

    RS485.setDelays(preDelayBR, postDelayBR);

    // Start the Modbus RTU client
    if (!ModbusRTUClient.begin(baudrate, SERIAL_8N1)) {
        Serial.println("Failed to start Modbus RTU Client!");
        while (1);
    }
}

void loop() {
  readInputRegisterValues();

    counter++;

    delay(1500);
    Serial.println();
}
/**
  Reads Input Register values from the server under specified address.
*/
void readInputRegisterValues() {
    Serial.print("Reading input register values ... ");

    // Read 10 discrete input values from (server) id 42,
    if (!ModbusRTUClient.requestFrom(2, INPUT_REGISTERS, 0x00, 1)) {
        Serial.print("failed! ");
        Serial.println(ModbusRTUClient.lastError());
    } else {
        Serial.println("success");

        while (ModbusRTUClient.available()) {
            Serial.print(ModbusRTUClient.read()*0.001, 3);

            Serial.print(' ');
        }
        Serial.println();
    }

    // Alternatively, to read a single Input Register value use:
    // ModbusRTUClient.inputRegisterRead(...)
}

```

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [April 11, 2023, 7:19am UTC](https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184/2 "2023-04-11T07:19:44Z")

</div>

> [@vapor-man](#):
>
> I cannot figure out how to get it to read a negative number.

Simply because the Modbus protocol doesn't define negative numbers. Every register returns a 16 bit unsigned integer by the protocol. It's up to you to convert the returned value to the correct measurement according to the manual of the device.

In your case it seems to be relatively easy, just a cast away:

```arduino
uint16_t mvalue = ModbusRTUClient.read();
int16_t rvalue = *((in16_t *)&mvalue);
float units = 0.001 * rvalue;

```

---

<div class="post-metadata">

**Author:** ![anon11653851](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@anon11653851](https://forum.arduino.cc/u/anon11653851)\
**Post date:** [April 22, 2023, 11:36pm UTC](https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184/3 "2023-04-22T23:36:20Z")

</div>

pylon, Works great now. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 19, 2023, 11:36pm UTC](https://forum.arduino.cc/t/modbus-sensor-is-not-reading-a-negative-number-correctly/1108184/4 "2023-10-19T23:36:51Z")

</div>

Closed on October 19, 2023, 11:36pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
