Forum Home › Forums › Understanding the Software › Using Formulas › Event on change while using formula of stat
 This topic has 10 replies, 3 voices, and was last updated 1 year, 3 months ago by Mikhail.

AuthorPosts

June 21, 2021 at 6:52 am #9112JWParticipant
Hi,
I added some custom “Events Types” with color indication.
for a channel, I enable event on change and use formulas.
CalVal();CalStat()
While the value changes, 2 event log appears, screen capture in link.
– the 1st event only show the new value (“Normal”) with old stat (Leak/Cyan);
– while the 2nd event show the new value (“Normal”) with new stat (Normal/Green).
https://drive.google.com/file/d/1jCBetZss3Pp4XCYk24hN8rjNJMAyVp1/view?usp=sharingIs there any way to show 1 event only, with new Val and Stat at the same time?
June 21, 2021 at 8:53 am #9117MikhailModeratorHi,
Likely, this is caused by formulas. Please provide screenshots of the input channel properties and code of the used formulas.
June 21, 2021 at 5:21 pm #9122JWParticipantChannel settings:
the value can be 0,1,2,3
the stat can be 0,121,122,123
https://drive.google.com/file/d/1IXNixhZteaGPdD4zrm4qe3Q6wMCXM3lJ/view?usp=sharing———————
Formulas:int LeakValList(int [] lst) { int na = 0; int active = 0; int inactive = 0; int normal = 0; for (int i = 0; i < lst.Length; i++) { if (Val(lst[i]) == 1) {active ++;} else if (Val(lst[i]) == 2) {inactive ++;} else if (Val(lst[i]) == 3) {normal ++;} else {na++;} } if (active > 0) {return 1;} else if (inactive > 0) {return 2;} else if (normal > 0) {return 3;} else {return 0;} } int LeakStat() { int s = (int) Val(); if (s > 0) {s = s + 120;} return s; }
I was using Cnl, now replaced with Val() for stat calculation, but seems the same.
 This reply was modified 1 year, 3 months ago by JW.
June 22, 2021 at 6:53 am #9126JWParticipantQ1:
for real/discrete channel, Cnl and Val() are value before and after formula calculation;
but for calculated real/discrete, what’s the Cnl? last value or same as Val()?Q2:
Is it possible to return Val and Stat at the same time using 1 formula?June 22, 2021 at 3:27 pm #9130MikhailModeratorI suppose, events created twice, because LeakStat returns different statuses.
The idea is the formulas like that:int LeakStat(int leak) { // your calculations }
In the channel: LeakValList(210, 220); LeakStat(LeakValList(210, 220))
Note: now you create a new array (formula argument) on every loop. It’s not efficient, because memory is allocated and cleaned too active.June 22, 2021 at 3:29 pm #9131MikhailModeratorQ1:
Cnl is come from the PLC.
Val() is stored in the channel.
For calculated channels Cnl is meaningless.Q2:
It will be included in v6.June 22, 2021 at 6:02 pm #9135JWParticipantThanks Mikhail.
what’s the proper way to pass an array to a formula as input, while the size of array is not constant.
the input is usually multiple channel numbers. e.g. it can be {11,12} or {11,12,13}
June 23, 2021 at 6:58 am #9143JWParticipantI came up with the following solution.
One downside is that I can’t see which channels are called in the channel formula, but not a big deal.in dictionaries/formulas, declare the arrays I need to use,
public static int[] lst_1 = new int[]{1}; public static int[] lst_2 = new int[]{1,2}; public static int[] lst_3 = new int[]{1,2,3}; public static int[] lst_4 = new int[]{1,2,3,4};
then in channels,
LeakValList(lst_1) LeakValList(lst_2)
June 23, 2021 at 7:15 am #9144RomirosParticipantpublic double LeakValList(params int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
…
}}
In formula LeakValList(101,102,103)
June 23, 2021 at 10:19 am #9145JWParticipantthanks Romiros, works perfectly!
June 23, 2021 at 11:59 am #9147 
AuthorPosts
 You must be logged in to reply to this topic.