2015-12-21 42 views
-1

Heyho重複程序(python)

我想在運行任務後重復一個程序。在程序開始時,我會問一些問題,而不是代碼跳到任務中。如果任務完成,sholud再次詢問問題。 每個問題都會在串口讀取一些信息。如果我得到十次信息ich將重新啓動程序..但窗口關閉,我必須啓動該文件。

我該怎麼辦?

import serial 
import struct 
import datetime 

print("\n") 
print("This tool reads the internal Bus ") 
print("-----------------------------------------------------------------------") 
COM=input("Check your COM Port an fill in with single semicolon (like: 'COM13'): ") 

ser = serial.Serial(
    port=COM, 
    baudrate=19200, 
    parity=serial.PARITY_NONE, 
    stopbits=serial.STOPBITS_ONE, 
    bytesize=serial.EIGHTBITS 
) 

     print("\n") 
     print("Please choose an option: ") 
     print("Polling of measured values and the operating status (1)") 
     print("Reading Parameter memory (2)") 
     print("Reading Fault memory (3)") 
     print("EXIT (4)") 
     print("\n") 
     i=input("Reading: ") 
     while(i==1): 
      count=0 
      while (count<10): 
        print(file.name) 
         print(ser.isOpen()) 
         print ("connected to: "+ ser.portstr) 
         data = "\xE1\x14\x75\x81" 
         ser.write(data) 
         a=(map(hex,map(ord,ser.read(46)))) 

         with open("RS485_Reflex.txt",mode='a+') as file: 
         file.write(str(datetime.datetime.now())) 
         file.write(", Values: ,") 
          file.write(str(a)) 
           file.write("\n") 
         print(a) 
         count=count+1 

       else: 
        i=0 
     loop=1 
#----------------------------------------------------------------------- 
     while(i==2): 
       count=0 
       while (count<10): 
         print(file.name) 
         print(ser.isOpen()) 
         print ("connected to: "+ ser.portstr) 
         data = "\xE1\x13\x00\x00\x74\x81" 
         ser.write(data) 
         a=(map(hex,map(ord,ser.read(11)))) 

         with open("RS485_Reflex.txt",mode='a+') as file: 
           file.write(str(datetime.datetime.now())) 
           file.write(", Parameters: , ") 
           file.write(str(a)) 
           file.write("\n") 
         print(a) 
         count=count+1 
       else: 
         i=0 

#--------------------------------------------------------------------- 
     while(i==3): 
       count=0 
       while (count<10): 
         print(file.name) 
         print(ser.isOpen()) 
         print ("connected to: "+ ser.portstr) 
         data = "\xE1\x12\x00\x00\x73\x81" 
         ser.write(data) 
         a=(map(hex,map(ord,ser.read(11)))) 

         with open("RS485_Reflex.txt",mode='a+') as file: 
           file.write(str(datetime.datetime.now())) 
           file.write(", Fault: , ") 
           file.write(str(a)) 
           file.write("\n") 
         print(a) 
         count=count+1 
       else: 
         i=0 

#---------------------------------------------------------------------- 
     while(i==4): 
       file.close() 
       ser.close() 
       sys.exit(0) 

     file.close() 
     ser.close() 

回答

0

首先,你應該使用如果/艾利芙/ ELSE語句,而不是while循環。

while(i==1): 

應該

if(i==1)0: 

這是一個簡單的程序我寫的,你可以遵循:

# setup a simple run_Task method (prints number of times through the loop already) 
def run_task(count): 
    print "Ran a certain 'task'", count,"times" 

# count is a variable that will count the number of times we pass through a loop 
count = 0 
# loop through 10 times 
while(count<10): 
    # ask questions 
    q1 = raw_input("What is your name?") 
    q2 = raw_input("What is your favorite color?") 

    # run a 'task' 
    run_task(count)