Loops 重复程序(python)
嗨 我想在运行任务后重复一个程序。在程序开始时,我问了一些问题,然后代码跳转到任务中。如果任务完成了,就不要再问问题了。。 每个问题都会在串行端口读取一些信息。如果我收到十次信息,我会重新启动程序。。但是窗口关闭了,我必须启动文件 我能做什么Loops 重复程序(python),loops,repeat,Loops,Repeat,嗨 我想在运行任务后重复一个程序。在程序开始时,我问了一些问题,然后代码跳转到任务中。如果任务完成了,就不要再问问题了。。 每个问题都会在串行端口读取一些信息。如果我收到十次信息,我会重新启动程序。。但是窗口关闭了,我必须启动文件 我能做什么 import serial import struct import datetime print("\n") print("This tool reads the internal Bus ") print("---------------------
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()
导入序列号
导入结构
导入日期时间
打印(“\n”)
打印(“此工具读取内部总线”)
打印(“--------------------------------------------------------------------------------------”)
COM=input(“检查您的COM端口,并用一个分号(如:“COM13”):”填写)
ser=串行。串行(
端口=COM,
波特率=19200,
奇偶校验=串行。奇偶校验\u无,
停止位=串行。停止位\u一,
bytesize=串行8比特
)
打印(“\n”)
打印(“请选择一个选项:”)
打印(“轮询测量值和工作状态(1)”)
打印(“读取参数存储器(2)”)
打印(“读取故障存储器(3)”)
打印(“退出(4)”)
打印(“\n”)
i=输入(“读数:”)
而(i==1):
计数=0
while(count首先,应该使用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)
#设置一个简单的run_任务方法(已打印循环次数)
def run_任务(计数):
打印“运行某个‘任务’”,计数,“次数”
#count是一个变量,用于计算通过循环的次数
计数=0
#循环10次
而