Precision of modbus values

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Precision of modbus values

Postby kucharzb » Wed Oct 11, 2023 5:42 pm

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
Attachments
M20002.txt
(5.94 KiB) Downloaded 298 times
values.jpg
kucharzb
 
Posts: 27
Joined: Fri Nov 19, 2021 10:28 pm

Re: Precision of modbus values

Postby cncdrive » Thu Oct 12, 2023 2:58 pm

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.
cncdrive
Site Admin
 
Posts: 4901
Joined: Tue Aug 12, 2014 11:17 pm

Re: Precision of modbus values

Postby kucharzb » Fri Oct 13, 2023 8:48 pm

I have figured it out, but which command could I use to get the effect I want?
kucharzb
 
Posts: 27
Joined: Fri Nov 19, 2021 10:28 pm

Re: Precision of modbus values

Postby Greolt » Sat Oct 14, 2023 6:08 am

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);
Greolt
 
Posts: 239
Joined: Sun Sep 04, 2016 6:22 am

Re: Precision of modbus values

Postby kucharzb » Tue Oct 17, 2023 8:21 pm

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.
kucharzb
 
Posts: 27
Joined: Fri Nov 19, 2021 10:28 pm

Re: Precision of modbus values

Postby dezsoe » Tue Oct 17, 2023 8:53 pm

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
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Precision of modbus values

Postby Greolt » Thu Oct 19, 2023 7:03 am

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.
Greolt
 
Posts: 239
Joined: Sun Sep 04, 2016 6:22 am

Re: Precision of modbus values

Postby dezsoe » Thu Oct 19, 2023 7:45 am

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". :)
dezsoe
 
Posts: 2093
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Precision of modbus values

Postby Greolt » Fri Oct 20, 2023 8:45 am

I appreciate your reply Dezsoe.
However it goes over this old carpenters head.
I will leave what is working alone. :)
Greolt
 
Posts: 239
Joined: Sun Sep 04, 2016 6:22 am


Return to Macros

Who is online

Users browsing this forum: No registered users and 11 guests