# Trouble with floats

**URL:** <https://forum.inductiveautomation.com/t/trouble-with-floats/14941>\
**Category:** Ignition\
**Created:** [July 31, 2017, 6:48pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941 "2017-07-31T18:48:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [July 31, 2017, 6:48pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/1 "2017-07-31T18:48:41Z")

</div>

So in the tag viewer I’m unable to get floats to show up correctly, they are all 0, if I use modpoll the value comes through correctly. The diagnostics shows that the value is good with a scientific notation value. It’s my first time with floats so I’m sure I’m just not using the tags properly.

Thanks,

Jake

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 31, 2017, 7:10pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/2 "2017-07-31T19:10:53Z")

</div>

Check that the tag offset is correct (try moving up or down one register) and also play with two of the settings on the device connection:

1. Reverse Word Order
2. One-Based Addressing

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 31, 2017, 7:14pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/3 "2017-07-31T19:14:13Z")

</div>

Also worth looking at: is this “scientific notation value” really close to 0 and modpoll is showing you a rounded number?

---

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [July 31, 2017, 7:20pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/4 "2017-07-31T19:20:49Z")

</div>

the scientific notation value is 2.4243e-41, modpoll shows 300, moving up and down a register gives an error, in that block there’s only 1 float register, if I get each register at a time the first is 0 and the second is like 17,302. The opc item path is using the HRF prefix.

---

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [July 31, 2017, 8:31pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/5 "2017-07-31T20:31:36Z")

</div>

how do I reverse the byte order?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 31, 2017, 9:04pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/6 "2017-07-31T21:04:06Z")

</div>

Reverse byte order is counter to the Modbus spec, so we don’t directly support it, but you could try to do so from a script:

```auto
def int32BE_to_float32LE(i): 
	from java.lang import Float, Integer
	return Float.intBitsToFloat(Integer.reverseBytes(i))

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [July 31, 2017, 10:11pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/7 "2017-07-31T22:11:15Z")

</div>

The word order reversal is an advanced option on the driver settings.

Reversing _byte order_ requires @paul-griffith’s scripting workaround.

---

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [August 1, 2017, 12:09am UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/8 "2017-08-01T00:09:43Z")

</div>

I figured it out, think it’s because the modbus protocol is big endian, I had my software store the data as such. Thanks everybody.

---

<div class="post-metadata">

**Author:** ![netcloud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/netcloud/32/3799_2.png) [@netcloud](https://forum.inductiveautomation.com/u/netcloud)\
**Post date:** [August 1, 2017, 9:06am UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/9 "2017-08-01T09:06:01Z")

</div>

Had the same problem fo you @codersmurf. Just solve it playing with play with Reverse Word Order and One-Based Addressing, also being aware of the Modbus spec.

Many thanks to all!

---

<div class="post-metadata">

**Author:** ![buck2825](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@buck2825](https://forum.inductiveautomation.com/u/buck2825)\
**Post date:** [January 30, 2018, 9:47pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/10 "2018-01-30T21:47:48Z")

</div>

Where would you install this script.

On the Value Changed Event?

how should the tag be setup? as an int or float?

-Phil

---

<div class="post-metadata">

**Author:** ![codersmurf](https://avatars.discourse-cdn.com/v4/letter/c/df788c/32.png) [@codersmurf](https://forum.inductiveautomation.com/u/codersmurf)\
**Post date:** [January 30, 2018, 10:48pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/11 "2018-01-30T22:48:10Z")

</div>

Try reading this before messing with that:

[https://docs.inductiveautomation.com/display/DOC/About+Modbus+Addressing](https://docs.inductiveautomation.com/display/DOC/About+Modbus+Addressing)

There are some special register types like HRBCD and such that will grab the data with conversions.

---

<div class="post-metadata">

**Author:** ![shaun2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/shaun2/32/28478_2.png) [@shaun2](https://forum.inductiveautomation.com/u/shaun2)\
**Post date:** [July 23, 2024, 8:29pm UTC](https://forum.inductiveautomation.com/t/trouble-with-floats/14941/12 "2024-07-23T20:29:55Z")

</div>

I had to deal with this issue in order to get the weight off of truck scales. I was was able to get it to work by pulling the two input registers in question in 16bit unsigned format. Whenever they change (via a tag change gateway script) the following gateway script runs.

import struct

def combine\_modbus\_words\_to\_float(tag\_path1, tag\_path2, output\_tag\_path):  
def swap\_bytes(word):  
# Swap the bytes within the word  
return ((word & 0x00FF) \<\< 8) | ((word & 0xFF00) \>\> 8)

```
def combine_words_to_hex(word1, word2):
    # Combine the high-order and low-order bits
    combined_int = (word1 << 16) | word2
    # Convert to hexadecimal string
    combined_hex = format(combined_int, '08x')
    return combined_hex

def hex_to_float(hex_str):
    # Convert hex string to integer
    int_value = int(hex_str, 16)
    # Pack the integer into bytes and unpack as float
    combined_float = struct.unpack('!f', struct.pack('!I', int_value))[0]
    return combined_float

# Read Modbus values
a = system.tag.readBlocking([tag_path1])[0].value
b = system.tag.readBlocking([tag_path2])[0].value

logger = system.util.getLogger("ModbusScript")

# Log raw values
logger.info("Raw values: {}, {}".format(a, b))

# Swap the bytes within each word
a_swapped = swap_bytes(a)
b_swapped = swap_bytes(b)

# Convert individual words to hexadecimal after swapping
a_hex = format(a_swapped, '04x')
b_hex = format(b_swapped, '04x')

# Log hexadecimal representations
logger.info("Hexadecimal representation of the first word (swapped): {}".format(a_hex))
logger.info("Hexadecimal representation of the second word (swapped): {}".format(b_hex))

# Combine swapped words into a single hexadecimal representation
combined_hex = combine_words_to_hex(a_swapped, b_swapped)
logger.info("Combined hexadecimal representation: {}".format(combined_hex))

# Convert combined hexadecimal to floating-point value 
combined_float_value = hex_to_float(combined_hex)
logger.info("Combined floating-point value: {}".format(combined_float_value))

# Write the result to another tag
system.tag.writeBlocking([output_tag_path], [combined_float_value])

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/0/a04c74d835a0d0a9aeb03d7a0a20016535782323.png)
