-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transmit and receive LONG and Float data by Modbus. #158
Comments
Awesome! |
would be awesome to have this integrated directly in API :) |
@stritti |
Hello, Here I leave a library to be able to work with different types of data through modbus. |
First of all, thank you for this amazing library @emelianov and thank you @Merdock1 for simplifying the swap and union bytes. I'm not sure if it is the right place to comment, if it is not, you may just delete this comment. Here is the thing, in issues I have seem lots of issues about ESP32 meaning theres lots of ESP32 running the Modbus library and many will come trough here because of floats/doubles. This functions works perfectly, but theres a catch to use them on ESP32, endianness mismatch. Took me a while to figure out why the values after the union were 0.00 and this was the problem. More specific: The ESP32 is little-endian, meaning the least significant byte is stored first in memory. And the function data source is big-endian, where the most significant byte is stored first, so for the ESP32 you have to swap the order of uint1 and uint2 before assigning them to f_number.i. What I did was to just change the order on the header of the function. I used this: Instead of: Also I made the same change on the long function and it worked just fine. I'm just commenting this so others dont have to figure this out and specially cause it is not something trivial to know (LSB and MSB) when you are starting on programming microcontrollers. Paying more attention, this can also be resolved on the slave side:
I swapped this call on my slave and it worked just the same way when the header is swapped. Mine is:
This way theres no need to change the header function on the master side. Once again, thank you for this amazing library @emelianov and thank you for making my life with floats WAY easier @Merdock1. Best regards from Brazil! |
Hello @prustt. |
This is shared with us as information taken from the industruino page.
These examples are based on the fact that most devices send data in this format:
float / ulong / long type data, Communication transmission in byte order2-1-4-3
To separate a float value into 2 registers.
It's used
To retrieve floating value from 2 registers.
It's used
float float_reconstructed = f_2uint_float(reg1, reg0);
To separate LONG data the code:
It's used:
To rebuild LONG from two registers.:
It's used:
long long_reconstructed = l_2uint_long(reg1, reg0); // reconstruct the long from 2 unsigned integers
The text was updated successfully, but these errors were encountered: