stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.3k stars 1.71k forks source link

float endianess issue for me introduced with 49af73d #652

Closed sven28472 closed 1 year ago

sven28472 commented 1 year ago

libmodbus version

db1cbc5

OS and/or distribution

Ubuntu 20.04.5 LTS

Environment

Linux 5.4.0-125-generic #141-Ubuntu SMP Wed Aug 10 13:42:03 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Running on an up board cpuinfo: cpu family : 6 model : 76 model name : Intel(R) Atom(TM) x5-Z8350 CPU @ 1.44GHz

Description

I have trouble using the mbrtu tool, which uses libmodbus, when reading values from my power meter. here you can see attempts to read wattage and the received values are jumping wild indicating some data type issues: ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 0.00 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -0.00 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 14462.94 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -0.00 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 2.70 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 85357129061519474032640.00 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -0.00 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 0.00

By divide and conquer testing strategy i tracked down that the issue was introduced for me with this commit: commit 49af73debd756be68497a61bd53a07c02673da96 Author: SZ Lin (林上智) szlin@debian.org Date: Thu Dec 20 13:35:31 2018 +0800

Fix float endianness issue on big endian architecture.

It converts float values depending on what order they come in.

This patch was modified from rm5248 [1]

[1] https://github.com/synexxus/libmodbus/commit/a511768e7fe7ec52d7bae1d9ae04e33f87a59627

Actual behavior if applicable

-

Expected behavior or suggestion

When i build current HEAD (aea0b02d6de0574c3db81c64e55aa5b05646291b) and revert the commit 49af73d, then the issue dissapears and i can read normal Wattage values from the power meter:

./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -23.58 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -34.10 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -32.30 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -45.69 ./mbrtu -Q -P -d/dev/ttyUSB0 -b9600 -pn -s1 -a1 -fi -tf32_dcba -n2 -r12 1 12 -49.98

Steps to reproduce the behavior (commands or source code)

<...>

libmodbus output with debug mode enabled

<...>

stephane commented 1 year ago

I think it means you shouldn't swap the bytes (at first) and uses something like -tf32_abcd?