Thank you, that works fine indeed, will adapt for temps below 0 and share here.
At least it allows me to push the temp data of the TG-BT5-OUT to MQTT without any client hassle.
Thanks for sharing, but is “another” problem.
On OP case, is a HEX Float value to be converted on decimal (string)
On your case, you have a integer, than rapresent 10 times the value???
and you want convert it on float (string)???
Is not clear how to use that fragment, because is out of the contest where is used.
You can get to the modbus float to string with a script I suspect. But you do run into the problem you can NOT compare or do math on them, unless you convert to integer - since there are no “real” floats.
So what your hoping to do with these floats becomes important
I'm new to modbus stuff so forgive me if i can't explant very good. I also writed wrong 80500 on the first result.
I have multiple energy meters where every value is stored according the the manufactures on 2 registers if i use IEEE values table and starting from 0x1000 or 4 registers starting from 0x0000.
So, to read System Voltage i interogate 0x1000 (or 44097) with a length of 2 (or 4 for the alternative table). I used length of 2 because i thought it will be simplier.
With Mikrotik i get this result: 18630;49152 . These values, in hex, are actually 0x48C6;0xC000
If you convert 0x48C6C000 on a IEEE 754 converter to decimal you get 402,944 which are mW according to the manual or 402.9 V.
So FFS, how do i transform 18630;49152 to 402944 directly on Mikrotik?
search tag # rextended ieee754toint IEEE754 DWORD FLOAT to number
Functions to convert DWORD FLOAT as IEEE754 to number
This feature is not complete.
RouterOS does not support numbers larger than 9223372036854775807 or smaller than -9223372036854775808, and it also does not support decimal division.
So we can only have an approximation, because the float type go from
(+/-)0,(put 45 zeros here)140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125
to
(+/-)340282346638528859811704183484516925440
and is impossible to represent correctly on RouterOS.
So this function, in extreme cases, could produce an incorrect value.
:global ieee754toint do={
:local input [:tonum "$1"]
:local hack 0x3B9ACA00 ; # Hack, RouterOS do not support decimal numbers...
:local isneg ($input >> 31)
:local exponent (($input >> 23) & 0xFF)
:local powerof2 1
:for x from=1 to=($exponent - 0x7F) step=1 do={:set powerof2 ($powerof2 * 2)}
:set exponent $powerof2
:local mantissa ($input & 0x7FFFFF)
:set powerof2 $hack
:local temp $hack
:for x from=22 to=5 step=-1 do={ ; # is 5 and not 0 because missing support for decimals on RouterOS
:set powerof2 ($powerof2 / 2)
:if ((($mantissa >> $x) & 1) = 1) do={
:set temp ($temp + $powerof2)
}
}
:set mantissa $temp
:local total (($exponent * $mantissa) / $hack)
:local decimal (($exponent * $mantissa) % $hack)
:if ($decimal > 444444444) do={:set total ($total +1)}
:if ($isneg = 1) do={:set total ($total * -1)}
:return $total
}