Timers in Python

13 thoughts on “Timers in Python

  1. Hello everyone,

     

    I am wondering if there is a ready made function within Telit’s Python, to construct timeouts or other timing functionalities into your code.

     

    On some Python sample code I think I have seen the import timer, or similar, expression at the beginning. But never saw real examples or descriptions thereof. Can you point me to some relevant docs?

     

    Thanks, Tom

    1. Hi Tom,

      There aren’t other timing facilities than the ones provided by MOD module; here is an example of a timing procedure:

       

      def Chat(command, expected, timeout):
          timer = MOD.secCounter() + timeout
          MDM.send(command, 5)
          b = command + MDM.read()
          while (MOD.secCounter() < timer) and (b.find(expected) == -1):
              b = b + MDM.read()
          return b

      1. Hi Cosmin

         

        Thanks for this idea.  But I guess the MOD.secCounter function is based on the GSM network clock? Must I then be registered to the network for this function to perform properly?

         

        Also, at times,  I see the AT+CCLK? command returning nonsense after powerup or registration, like this:

        +CCLK: "00/01/01,00:00:19+00"

         

        Later on, it might  "jump" to correct time. Such a jump could perhaps cause unexpected consequences? Best would be if the  MOD.secCounter function was based on a simple hw counter being reset at powerup, counting endlessly.

         

        What do you think?

         

        Tom

        1. The counter works as you say in the last paragraph, is a simple running seconds counter with no connection with +CCLK.

          1. The counter works as you say in the last paragraph, is a simple running seconds counter with no connection with +CCLK.

            Cosmin,

             

            I would like to revisit this issue. In "Easy Script in Python" manualRev 13, on page 52/117 it is said that  "The AT+CCLK command is used to read and set the current date and time". So I think there IS a connection with +CCLK??

            Also, I am reading the MOD.secCounter value in my Python test code and when printing it, it takes values  over 361 million (example 361 395 005), so this seems to be the amount of seconds since the beginning of year 2000, right?

            But sometimes I see small values after startup, such 1723 or  2253. So please, what should we think about the actual operation of the MOD.secCounter?

             

            To rely on it, I would like to understand it completely.

             

            BR, Tom

          2. Hi Tom, indeed you are right, if you set module clock with AT+CCLK MOD.secCounter will show the correct value since 2000; I think the function is perfectly usable without setting CCLK, for on the fly short timings.  Other long term usage scenarios will surely benefit if a way to set module clock is found, with NITZ if available or online clocks if possible, or even GPS, why not?

             

          3. Oh, I see. So after a "cold start" I will get small numbers from the MOD.secCounter but when I advance in my program to an area where the AT+CCLK command is issued, the MOD.secCounter will "jump" ahead to some hundred million counts, as a kind of side effect?

            One must then be careful to not rely on the MOD.secCounter while issuing the AT+CCLK command. I think I can live with this but the manual should be more clear about it.

             

            Perhaps it would be a good idea to have two separate counters, one linked with AT+CCLK and another totally indendent of it. Something like MOD.secCounter1 and  MOD.secCounter2  maybe?

             

             

    2. Hello everyone,

       

      I am wondering if there is a ready made function within Telit’s Python, to construct timeouts or other timing functionalities into your code.

       

      On some Python sample code I think I have seen the import timer, or similar, expression at the beginning. But never saw real examples or descriptions thereof. Can you point me to some relevant docs?

       

      Thanks, Tom

      Hello Tom,

      You may have a look in the "Examples" folder of the PythonWin application: there is a file named timers.py, maybe it can help.

      Philippe

       

  2. Hey Tom

     

    If i understand your question correctly, alternative to the MOD module, you may also use the standard python time module:

     

    E.g

     

    import time

     

    time.sleep (10); create a delay of 10secs

    With this, you can create larger delays as oppose to the Telit Python MOD module fxn.

     

    However , Telit python manual does reckon using delay longer than 2sec in the way Cosmi presented.

    1. I don’t think time module is available on device, read "Python core supported features" in "Easy Script in Python" doc. More, at page 51:

       

      You need to use the MOD built-in module if you want to handle time functions in Python scripts.

  3. Thanks Philippe. Can you point me to the link holding the "Examples" folder you mentioned? Or is it maybe in the password protected Telit download zone? 

     

    Tom

    1. No no, is a folder in your PC Python installation folder, like "C:Program FilesPythonExamples".