telit script run number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • timoteo
    New Member
    • Jan 2009
    • 6

    telit script run number

    hello,
    I need to make a compiled script that I know how to do, done more then once, and work, *.pyo.
    Then I send it to the gsm module and put it run.
    The rest of the code is ok, I have test it more than once but to organize a group of aquired temperatures does not work, next is an example of the code that I need to run:
    Code:
    import MOD #Use build in module
    import MDM #Use AT command interface
    #i=5
    # wake module
    #Set time
    #Set next alarm
    print i
    if i == 1:
            i=i+1
            print'Temperature=',temp,'ºC'
    elif i == 2:
            i=i+1
            print 'voltage=',volt,'V'
    elif i == 3:
            i=1
            print 'send sms',volt,temp
    else:
            i = 1
            print 'reset'
    #Turn off module
    If I dont declare the variable i I get the error:
    Traceback (innermost last):
    File "C:\Programas\P ython\Pythonwin \pywin\framewor k\scriptutils.p y", line 301, in RunScript
    exec codeObject in __main__.__dict __
    File "D:\Python\Scri pt1.py", line 18, in ?
    print i
    NameError: i
    but if I declare i=5 like in the exemple it allway give me an output:
    i=5
    reset
    So what i need is to give a value to i in the beginig and after that first cycle disable it...
    Did i make myself clear?
    Thanks in advance
    Tim
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Tim,

    Can you describe the purpose of the code snippet? What determines the value of i?

    Comment

    • timoteo
      New Member
      • Jan 2009
      • 6

      #3
      bvdet,

      The i is like in the example, in the first run it collect the voltage in the second run it colect temperature and in the 3º run it put all the information toghether and send it by sms.
      Thanks
      Tim

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You can save the run number (variable i) to a file. It could work something like this:
        Code:
        import os
        
        def read_data(fn, i):
            # read data file if existing
            if os.path.exists(fn):
                return int(open(fn).read().strip())
            else:
                return i
        
        def save_data(fn, n):
            f = open(fn, 'w')
            f.write(str(n))
            f.close()
        
        def main(temp=None, volt=None):
            data_fn = 'data_i.txt'
            i = 1
            
            i = read_data(data_fn, i)
        
            if i == 1:
                i += 1
                print'Temperature =',temp,'ºC'
            elif i == 2:
                i += 1
                print 'Voltage =',volt,'V'
            elif i == 3:
                i = 1
                print 'send sms',volt,temp
            else:
                i = 1
                print 'reset'
                    
            save_data(data_fn, i)
            return i
        
        if __name__ == '__main__':
            print main(temp=23)
            print main(volt=220)
            print main(23,220)

        Comment

        • timoteo
          New Member
          • Jan 2009
          • 6

          #5
          hello bvdet,

          Thanks for the fast answer!
          Do you know if in telit modules is possible to save a txt file?
          Because i am using a telit ge863 gps and don't know if it is as the os module.
          thanks once again

          Tim

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            I am not familiar with telit. You don't need the os module if you do it like this:
            Code:
            def read_data(fn, i):
                 # read data file if existing
                 try:
                     return int(open(fn).read().strip())
                 except:
                     return i

            Comment

            • timoteo
              New Member
              • Jan 2009
              • 6

              #7
              hello bvdet,

              It took me some time to understand how to make that file and manipulate it but now is resolved the problem.
              Thanks for your help.
              If you want you can close the thread.

              Regards,
              Tim

              Comment

              Working...