Faster thread

Tagged: 

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #16618
    詹森
    Participant

    In the script calculation, the smallest execution interval currently observed is 1 second (using EverySec(getDataFunc), a method can be called once per second). I would like to ask if there are faster threads and methods? For instance, if I want my script to perform a calculation every 100 milliseconds or even 10 milliseconds, how can I achieve this? Want to achieve faster threads,Thank you for your help

    | Function | Data Type | Description |
    | ——————— | ——— | ——————————————– |
    | EverySec(getDataFunc) | CnlData | Executes the specified function every second |

    #16619
    manjey73
    Participant

    All formulas in the channels are executed in a cycle from the smaller channel to the larger one. If you don’t specify every second, then it’s about 10 times per second. And the more channels there are, the longer the pause time will be.
    Or don’t use the every second wrapper, or use your own timer for example.

    #16620
    詹森
    Participant

    How can one implement their own timer? How to create a custom thread? Could you please explain the implementation steps or examples? Thank you very much for your help!!

    #16621
    manjey73
    Participant

    What’s wrong with processing a script in a loop, which also requires a timer of less than a second?
    To be honest, I haven’t implemented timers for such a task. I usually use them for other purposes, for example, to delay triggering the device status.
    What exactly is your goal? Can you draw a flowchart for understanding?

    An example of a TON timer from CodeSys or similar.

    // IEC timers and others
    public static long Ticks()
    {
    DateTime now = DateTime.Now;
    long time = now.Ticks/10000;
    return time;
    }
    
    protected class UtilTimer
    {
    public long et;
    public bool flag;
    public bool q;
    }
    
    protected Dictionary<string, UtilTimer> TonTimer = new Dictionary<string, UtilTimer>();
    protected Dictionary<int, UtilTimer> TofTimer = new Dictionary<int, UtilTimer>();
    protected Dictionary<int, UtilTimer> TpTimer = new Dictionary<int, UtilTimer>();
    
    // Timer with a delay of turning on TON
    public double TON(double IN, double PT, double Q, string str = "", int idx = 0)
    {
    var ut = new UtilTimer() {et = 0L, flag = false, q = false};
    string keys = $"{CnlNum}_{idx}"; // The key is the channel number plus the timer index, zero by default. Allows you to use multiple timers inside a formula.
    
    long ET  = 0L;
    long _pt = Convert.ToInt64(PT);
    bool q   = Q > 0;
    bool _in = IN > 0;
    string s = str.ToLower();
    
    if (s == "s" || s == "sec") _pt = Convert.ToInt64(PT) * 1000;
    if (s == "m" || s == "min") _pt = Convert.ToInt64(PT) * 60000;
    if (s == "h" || s == "hour") _pt = Convert.ToInt64(PT) * 3600000;
    
    if (!TonTimer.ContainsKey(keys))
    {
        TonTimer.Add(keys, ut);
    }
    
    if (!_in)
    {
        TonTimer[keys].q = false;
        TonTimer[keys].flag = false;
        TonTimer[keys].et = 0L;
    }
    else
    {
        if (!TonTimer[keys].flag)
    {
        TonTimer[keys].flag = true;
        TonTimer[keys].et = Ticks();
    }
     else
     {
         if (!q) ET = Ticks() - TonTimer[keys].et;
     }
     if (ET >= _pt) q = true;
     TonTimer[keys].q = q;
    }
    q = TonTimer[keys].q;
    return Convert.ToDouble(q);
    }
    // --------------------------------------------
    // Timer with delayed shutdown of TOF
    public double TOF(double IN, double PT)
    {
    var ut = new UtilTimer() {et = 0L, flag = false};
    long ET = 0L;
    long _pt = Convert.ToInt64(PT);
    bool q = Val(CnlNum) > 0;
    bool _in = IN > 0;
    
    if (!TofTimer.ContainsKey(CnlNum))
    {
    TofTimer.Add(CnlNum, ut);
    }
    
    if (_in)
    {
    q = true;
    TofTimer[CnlNum].flag = true;
    TofTimer[CnlNum].et = 0L;
    ET = 0L;
    }
    else
    {
    if (TofTimer[CnlNum].flag)
    {
    TofTimer[CnlNum].flag = false;
    TofTimer[CnlNum].et = Ticks();
    ET = 0L;
    }
     else
     {
       if (q) ET = Ticks() - TofTimer[CnlNum].et;
     }
     if (ET >= _pt) q = false;
    }
    return Convert.ToDouble(q);
    }
    
    // --------------------------------------------
    // Pulse Timer TP
    public double TP(double IN, double PT)
    {
    var ut = new UtilTimer() {et = 0L, flag = false};
    long ET = 0L;
    long _pt = Convert.ToInt64(PT);
    bool q = Val(CnlNum) > 0;
    bool _in = IN > 0;
    
    if (!TpTimer.ContainsKey(CnlNum))
    {
    TpTimer.Add(CnlNum, ut);
    }
    
    if (!TpTimer[CnlNum].flag)
    {
     if (_in)
     {
     TpTimer[CnlNum].flag = true;
     TpTimer[CnlNum].et = Ticks();
     if (ET < _pt) q = true;
     }
    }
    else
    {
      if (q)
      {
      ET = Ticks() - TpTimer[CnlNum].et;
      if (ET >= _pt) q = false;
      }
      else
      {
       if(!_in)
       {
       TpTimer[CnlNum].flag = false;
       ET = 0L;
       }
     }
    }
    return Convert.ToDouble(q);
    }
    
    #16622
    manjey73
    Participant

    You can use it inside your scripts. Someone also has to set the timer so that it starts working.

    #16623
    詹森
    Participant

    1. We have encountered a situation where there are many computations in the channel, resulting in the script being called in the channel only calculating once every few seconds. Therefore, this method is currently unavailable.
    2. Using “EverySec (getDataFunc)” can only calculate once per second at the fastest

    I currently have a requirement to perform a script calculation every 100 milliseconds to obtain the latest calculation results in real time. The main need is to meet some calculations that require rapid response. Could you please inform me if there are any other methods to meet such a requirement? Thank you very much for your reply. Thank you

    #16624
    manjey73
    Participant

    1. How does your script run for a few seconds without using EverySec?
    How did you determine that?

    #16626
    詹森
    Participant

    Due to the large number of data channels, I found that the script directly called in the channel has slowed down to about 10 seconds before calling the calculation once. If I want to call the calculation script once every 100 milliseconds, how can I achieve this? Could you please let me know? Thank you

    #16627
    manjey73
    Participant

    I don’t understand how your script works fast when using the limit of once per second (everysecond) and at the same time runs for 10 seconds with a direct call?

    Show your script that behaves this way.

    #16628
    詹森
    Participant

    Method 1: Call the script in the channel. It takes only 10 seconds to poll for one cycle (why is it so long because there are many computing channels).

    Method 2: Use EverySec (getDataFunc), but with one call cycle per second

    I’d like to ask if there are other ways to call a script (method, function) once every 100 milliseconds?

    Thank you for your help

    #16629
    manjey73
    Participant

    Please show me your script.
    When calling the script directly, it should run faster than when called through EverySec.

    #16632
    Mikhail
    Moderator

    Hello,
    The main loop of the Server service iterates approximately 10 times a second. If you need faster calculation rate, I suggest to develop a specific module in C#.

    #16633
    manjey73
    Participant

    so 10 times per second is about a 100ms cycle 🙂

    #16634
    詹森
    Participant

    Hello, regarding the #16629 issue of manjey73, since I use a large number of channels, there are approximately 5,000 computing channels. This has led to a polling cycle of calculation taking up to 10 seconds. This is related to CPU performance. Is there any solution? Thank you very much for your help.

    Hello Mikhail, you mentioned that specific modules can be developed. Do you have any relevant materials or examples for this? Please help me when it’s convenient. Thank you very much for your assistance.

    #16635
    manjey73
    Participant

    You’ve voiced the problem, but you haven’t provided a single script that you think could lead to this.

Viewing 15 posts - 1 through 15 (of 19 total)
  • You must be logged in to reply to this topic.