epsilonrt / mbpoll

command line utility to communicate with ModBus slave (RTU or TCP)
https://epsilonrt.fr
GNU General Public License v3.0
296 stars 71 forks source link

Problem with float output in exponential: different from what was written #55

Open tadzil opened 2 years ago

tadzil commented 2 years ago

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- 1269349 Written 1 references.

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1...

$ /usr/local/bin/mbpoll -t4:hex -c2 -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1...

As you can see - if I convert read (output to shell) value "1.26935e+06" to normal number, it will be 1269350 instead of 1269349. Using online IEEE-754 converter hex is decoded normally to 1269349. Why is it displayed in exponential form?

tadzil commented 2 years ago

If I do conversation from hex to iee-754 float in bash, I get correct value:

$ /usr/local/bin/mbpoll -t4:float -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1...

$ /usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1...

$ /usr/local/bin/mbpoll -t4:hex -c2 -0 -B -1 -q -r 10 127.0.0.1 -p 1502 -- Polling slave 1... **10: 0x499A

$ printf '%x\n' $(/usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 | awk 'FNR == 2 {print $2}') 499af328 $ printf '%x\n' $(/usr/local/bin/mbpoll -t4:int -0 -B -1 -q -r 10 127.0.0.1 -p 1502 | awk 'FNR == 2 {print $2}') | python3 -c 'import struct; print(struct.unpack("!f", bytes.fromhex(input()))[0])' 1269349.0