使用循环检查操作之间的时间

时间:2017-08-17 17:04:24

标签: python function loops timeout python-multithreading

我有两个功能。我想在这里做的是:当ser.write('1')时(即当Python告诉门保持打开时),我想记下时间并保持门打开8秒钟。现在,如果在内 那个时候,send_data(data)返回true,我想写ser.write('0')或关闭大门。

有谁能建议我如何实现这个?我尝试通过while循环执行此操作,但我没有成功。

def send_data(data):
    if (data ==0):
        print "manga"
        return True
    else:
        print "muringa"
        return False

def delay_for_goodant(data):
    print "thenga"
    global ser
    try:
        if (ser == None):
            ser = serial.Serial("COM1",9600,timeout = 0)
        if send_data(data) is True:
            ser.write('0')
            print "python is telling arduino to keep the gate closed"
        else:
            start_time_opening = time.time()
            ser.write('1')
            end_time_opening = time.time()
            elapsed_time =  end_time_opening - start_time_opening
            print "time taken for opening is"+ str(end_time_opening - start_time_opening)
            if elapsed_time < 8 and send_data(data) is True:
                ser.write('0')
            elif elapsed_time < 8 and send_data(data) is False:
                ser.write('1')
            incoming_data2 = ser.readline()
            print "python is telling the arduino to keep the gate open"
            print "incoming_data for opening is-",(incoming_data2)

0 个答案:

没有答案