Forum Home › Forums › Understanding the Software › Using Formulas › InitScripts() use
Tagged: InitScripts()
- This topic has 14 replies, 3 voices, and was last updated 7 months, 2 weeks ago by
詹森.
-
AuthorPosts
-
January 2, 2025 at 1:42 pm #15892
詹森
ParticipantHello, Why did I add code under InitScripts() under Server methods, such as writing 1 to channel 19, and SetData(19, 1, 1) didn’t work
public override void InitScripts()
{
SetData(19, 1, 1);
}Thank you!
January 2, 2025 at 1:43 pm #15893詹森
ParticipantAs if there is no call, or how to use it
January 2, 2025 at 3:32 pm #15894manjey73
Participant// Set the default setpoint value. // For channels of the Calculated/output type public CnlData GetDefaultData(double val) { return Stat() > 0 ? Data() : NewData(val, 1); } // For a channel with the Integer data type public CnlData GetDefaultDataInt(long val) { return Stat() > 0 ? Data() : NewData(BitConverter.Int64BitsToDouble(val), 1); }
If I understood correctly, you want to initialize some variables.
Here are the formulas that Mikhail once proposed.Formulas are used in the Input.
GetDefaultData(1)
SetVal(CnlNum, Cmd)
is used for the output formulas-
This reply was modified 7 months, 3 weeks ago by
manjey73.
January 3, 2025 at 1:48 am #15896詹森
ParticipantOkay, thank you. That solved my problem, and I want to know more
public override void InitScripts ()
{}
How to use this method? Thank you!
January 3, 2025 at 8:41 am #15898Mikhail
ModeratorIf you use some variables in your scripts, you can initialize them in the InitScripts method, for example. It is called once when Server starts.
January 4, 2025 at 8:05 am #15903詹森
ParticipantHow to achieve that the initial value of the channel is 0 every time the program service restarts (even if the value of the last run is not 0, it is reset to 0), and why does the default initial value of channel 11 not write?
public override void InitScripts()
{
SetData(11, 0, 1);
}January 4, 2025 at 2:03 pm #15904manjey73
ParticipantI may be wrong, but it seems enough to specify
Val()
in the input formula andSetVal(CnlNum, Cmd)
in the output, and when the Server starts, the channel will be 0.0, and after entering your valueFor Calculated/Output channels
-
This reply was modified 7 months, 3 weeks ago by
manjey73.
January 5, 2025 at 3:13 am #15911詹森
Participantpublic override void InitScripts()
{
// Put your initialization code here
// Поместите сюда код инициализацииSetData(1153, 0, 1);
SetData(1154, 0, 1);
SetData(1155, 0, 1);
SetVal(1031, 0);
SetVal(1032, 1);
}hello, I now call SetVal(), but it doesn’t have any effect.
GetDefaultData(Val)
Judging that the state is 1, I will not initialize it again, and I need the channel to default to 0 every time I turn on, what else can I do? Thank you for your help.
January 5, 2025 at 12:05 pm #15912manjey73
ParticipantExplain for which types of channels do you want to set the values to 0 when the server starts?
Show the settings of these channels
January 5, 2025 at 12:08 pm #15913manjey73
ParticipantPerhaps InitScripts() is intended for other things. And at this point, formulas like setData may not be valid yet.
January 5, 2025 at 12:20 pm #15914manjey73
ParticipantIf you look at CalcEngine.cs, then InitScripts() goes first and only then SetData.
When the server is started, this code is compiled every time.
Using SetData inside Initscripts, do you encounter any errors?January 5, 2025 at 12:27 pm #15915manjey73
ParticipantI checked, there are no errors during compilation, but it seems to me that you are trying to use an internal formula even before the server starts processing formula data. Most likely InitScripts is intended for other things.
January 5, 2025 at 12:47 pm #15916manjey73
ParticipantJust create your own formula in the script table, in which you can initialize any variables.
I was doing something similar to version 5 to simulate non-volatile variables through files.
In a simple version, it looks like thisbool initVal = false; public double InitValues() { if (!initVal) { SetData(1153, 0, 1); SetData(1154, 0, 1); SetData(1155, 0, 1); SetVal(1031, 0); SetVal(1032, 1); initVal = true; } return Convert.ToDouble(initVal); }
And most importantly, place the channel with the call of this formula at the very first in the channel database.
Moreover, the formulas in the channels themselves can be any.January 6, 2025 at 11:32 am #15918Mikhail
ModeratorProbably channel data is not ready when InitScripts is called.
What kind of channels do you need to initialize?January 6, 2025 at 11:40 am #15922詹森
ParticipantOkay, thank you. I thought this method would call itself, so I think I got it. Thank you
-
This reply was modified 7 months, 3 weeks ago by
-
AuthorPosts
- You must be logged in to reply to this topic.