Go Down

Topic: how to sent a foat value in modbus protocol [SOLVED] (Read 157 times) previous topic - next topic

acham

Hi guys

i want to store afloat value into holding register , and i don't know how to do it please help me
Code: [Select]
holdingRegs[REG_HUM] = (int16_t)(10 * Humidity);
 i don't think that works

can someone help me with a code


thankyou

Riva

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?
Don't PM me for help as I will ignore it.

acham

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
Code: [Select]
holdingRegs[REG_TEMP]=(int16_t)(10*Temperature);

Riva

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.
Code: [Select]
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)
Code: [Select]
float Humidity = holdingRegs[REG_HUM] / 100.0;

Use the same principle for Temperature
Don't PM me for help as I will ignore it.

acham

#4
Oct 23, 2019, 09:12 am Last Edit: Oct 23, 2019, 09:13 am by acham
first of all thank you for your reply

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
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

Riva

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.
Don't PM me for help as I will ignore it.

acham

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

Go Up