重复程序(python)

时间:2015-12-21 15:06:24

标签: loops repeat

Heyho

我想在运行任务后重复一个程序。在程序开始时,我问了一些问题,而不是代码跳进任务。如果任务完成,问题再次提出问题.. 每个问题都在串口读取一些信息。如果我得到信息十次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()

1 个答案:

答案 0 :(得分:0)

首先,您应该使用 If / Elif / 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)