Reading float32 Modbus values

I have a Modbus energy meter connected to my energy storage system and I’d like to read the values in OpenHAB. I was polling it with the Modbus binding but having two clients trying to poll the same meter was causing issues with the inverter.

I have worked out a way to eavesdrop the exchanges between the inverter and the meter and I’m successfully getting byte streams into an OpenHAB item but I can’t work out how to convert the data, which is encoded as a float32 into a readable form.

Can anyone help with a rule syntax to convert a four byte string into a float32 number please?

1 Like

You should be able to read directly the foat32 value.

Maybe you can give some details of the device you are reading and your openhab configuration?

Should I? How is that? Hopefully not by using the binding that I explicitly said I couldn’t use.

Maybe with this

I looked at that but I’m receiving a byte string, so four bytes not 8 hex characters. If there isn’t a simple/direct way, I could potentially convert to hex first and then use this method.

Hi @barneyd,

What do you exactly mean by “byte string”?

You can anyway convert a string to a byte array and display the bytes as hex values. Hex is just the representation of the byte values, like also decimal is.

4 Bytes:
Decimal: 34 76 234 142
Hex: 22 4C EA 8E
ASCII: “ L ê Ž

Maybe you have an example from your sniffer readings?

Cheers Jonathan

The string is exactly like your example. A sequence of 4 x 8 bit values.

The background is that the meter is connected to the inverter by two devices, each converting Modbus serial into a Modbus TCP packet that is transmitted over Ethernet or vice versa.

The conversion devices also have the capability to take the serial messages and turn them into an HTTP message. I use this function to transmit each serial message to OpenHAB, which receives them as a string. I then substring out the four bytes that represent the meter register value in 32 bit float. I need to convert them into a suitable Number type to use in OpenHAB.

Hi @barneyd,

What about the code piece @stamate_viorel referenced in the post above.
With a little modification (omitting the 16 in the parseInt function) you can use it also:

var String parse = '124365167'
var int n = Integer.parseInt(parse) 
var Float convert = Float.intBitsToFloat(n)
logInfo("test", convert.toString)


2023-03-10 23:51:56.680 [INFO ] [org.openhab.core.model.script.test  ] - 1.7578764E-34

To prove this, I also used the conversion to hex first:

And the check with the other tool for Big Endian float value representation:

Cheers Jonathan

Doesn’t parseInt take text characters representing digits and turn them into a Number as you might need to when parsing a JSON response?

I need to take an IEEE 754 32 bit float value, arriving as 4 x 8 bit bytes and turn that into a decimal number. I would provide an example but the string usually contains unprintables because they are just bytes.

In your example above, my string would be:
“BEL i HT o” assumig A9 is the same as 09 when we are writing it out.

By way of another example, if my string was “1234”, that would be Hex 31 32 33 34 and convert to floating pioint number: 0.00000000259315147133065693196840584278106689453125


String “CDEF” converts to 43 44 45 46 and into floating point 196.270599365234375

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