how to sent a foat value in modbus protocol [SOLVED]

Hi guys

i want to store afloat value into holding register , and i don't know how to do it please help me

holdingRegs[REG_HUM] = (int16_t)(10 * Humidity);

i don't think that works

can someone help me with a code

thankyou

Is your holdingRegs 16 bits wide and does the floating point number only need 1 decimal place of precision?

If you multiply by 10 to move the decimal point one place to the right then the code that reads the stored value from holdingRegs must divide by 10 to to restore the floating point position.

Do you really need Humidity (normally 0-100%) to resolve to tenths of a percent?

hello thank you for your reply i'm newbie and i donot now wath i should do

I need humidity between 0 and 100% and the temperature -40 to 105 i'm using sht85 (i2c protocol) and i want to send this in modbus
what I did is not wrong?
and for the temperature i did the same thing

holdingRegs[REG_TEMP]=(int16_t)(10*Temperature);

acham:
I need humidity between 0 and 100% and the temperature -40 to 105 i'm using sht85 (i2c protocol) and i want to send this in modbus

Are you writing both ends of the Modbus program?
How many decimal places do you want to preserve of the humidity and temperature readings?
As an example, to preserve 2 decimal places of humidity you would multiply the floating point value by 100 so something like 54.321% humidity would become 5432 as an int16_t that you would store in the holdingRegs.

holdingRegs[REG_HUM] = (int16_t)(Humidity * 100);

When you read the value you then divide by 100.0 to restore the 2 decimal places. (5432 / 100 = 54.32)

float Humidity = holdingRegs[REG_HUM] / 100.0;

Use the same principle for Temperature

first of all thank you for your reply

Riva:
Are you writing both ends of the Modbus program?

yes , if i may ask if i have a negative value how it work ? for example -1.25

Riva:
How many decimal places do you want to preserve of the humidity and temperature readings?

i need two

can you cheek my code cause i have trouble too display this in modbus software

acham:
yes , if i may ask if i have a negative value how it work ? for example -1.25i need two

can you cheek my code cause i have trouble too display this in modbus software

With an int16_t you can have both positive and negative values (max 32,767 to min -32,768). The multiply/divide works the same for negative and positive numbers.
-1.25 * 100 = -125
-125 / 100 = -1.25

You have not posted your code so impossible to check it.

it works i test it and , it works now, thank you riva :slight_smile: