Page 1 of 1
Precision of modbus values
Posted:
Wed Oct 11, 2023 5:42 pm
by kucharzb
Hello,
could somebody help me with a minor problem which I am facing?
I have my VFD configured thru Modbus, but some values are not displaying correctly (decimals or just amount of digits). Attached is the macro which I am using and screenshot where it shows how does it display. Is anybody able to modify the code?
Thanks, Kris
Re: Precision of modbus values
Posted:
Thu Oct 12, 2023 2:58 pm
by cncdrive
Hi,
modbus has 16 bits integer numbers only.
I mean this is the Modbus standard, so if you want precise numbers or floating point ones then you have to handle that yourself in softwre/macro/plugin etc. on both the receiver and transmitter sides.
Re: Precision of modbus values
Posted:
Fri Oct 13, 2023 8:48 pm
by kucharzb
I have figured it out, but which command could I use to get the effect I want?
Re: Precision of modbus values
Posted:
Sat Oct 14, 2023 6:08 am
by Greolt
If I am following what you are asking then some values need to have a formula applied to give the result you are looking for.
For example my VFD supplies current in a range of 0 to 1000.
So I divide that result by 10 to give me a percentage which makes more sense to me.
This is the relevant line in my macroloop.
It gets that value, divides it by ten and then sends that result to my onscreen field numbered 2099.
ushort Current;
exec.GetModbusregister(2, out Current);
AS3.Setfield((Current/10), 2099);
Re: Precision of modbus values
Posted:
Tue Oct 17, 2023 8:21 pm
by kucharzb
the simple math (like divide by 10) is clear to me, but I do not know how to change the precision of numbers, so how to change 43.5478954789431 to 43.5 for example. Or rounded 4, to more precise 4.1.
Re: Precision of modbus values
Posted:
Tue Oct 17, 2023 8:53 pm
by dezsoe
Use Setfieldtext instead:
- Code: Select all
AS3.Setfieldtext(((double)Current/10.0).ToString("F2"), 2099);
To round a number:
- Code: Select all
double rounded = Math.Round(original, 2, MidpointRounding.AwayFromZero); // round to 2 decimals
Re: Precision of modbus values
Posted:
Thu Oct 19, 2023 7:03 am
by Greolt
Dezoe
The macro line that I posted works as I want.
Am I doing something wrong? Is there a more correct way?
I am NO programmer.
Re: Precision of modbus values
Posted:
Thu Oct 19, 2023 7:45 am
by dezsoe
There are 2 differences. You let the compiler do the automatic type conversions while in my code I force it to convert to the type that is needed. Automatic conversion works in most cases, but it's a better practice to tell the compiler what you want exactly. The second difference is in the result on the screen. Your calculation displays 1 decimal if the result is not integer and no decimals if it is. If you use the format string "Fn" (where F is for floating point and n is the number of decimals) then it will display all the wanted decimals even if they are zero. I don't like numbers jumping back and forth depending on the number of decimals, but it's me. Of course, my sample is bad, because dividing by 10 may result only one decimal and I display 2, so the correct format string is "F1".
Re: Precision of modbus values
Posted:
Fri Oct 20, 2023 8:45 am
by Greolt
I appreciate your reply Dezsoe.
However it goes over this old carpenters head.
I will leave what is working alone.