0
我正试图使用pyserial库来沟通python到arduino。在下面的代码中,我只是显示了几个函数,我试图根据这些函数返回的值打开或关闭LED。我想要做的是,当python告诉打开LED,我想让LED灯开启7秒,但在那段时间里,如果set_forbidden_list
返回任何值,我希望LED立即关闭并保持关闭状态直到set_forbidden_list
变空。任何帮助将非常感激。使用动作之间的延迟
def openthedoor(set_accepted_list,set_list_ant_id,set_forbidden_list,set_accepted_list_frozen):
if(((len(set_accepted_list)) >0) & (set_forbidden_list == set()) & ((set_accepted_list_frozen == None) or ((set_accepted_list_frozen & set_accepted_list)== set_accepted_list))):
use_door(1)
delay_for_goodant(1)
print "open the gate"
else:
use_door(0)
delay_for_goodant(0)
print "close the gate"
set_for_comparison = set(set_accepted_list & set_list_ant_id)
list_for_comparison = list(set_for_comparison)
return set_for_comparison,list_for_comparison
def establishing_connection():
print ser.read();
ser.write('1')
last_action = -1
def use_door(activate):
global last_action
if(last_action != activate):
send_data(activate)
last_action = activate
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)
print "reconnect"
incoming_data = ser.readline()
else: ## for turning off the LED
ser.write('0')
time.sleep(0)
incoming_data2 = ser.readline()
except IOError:
ser = None
这也类似于time.sleep()。系统休眠7秒钟。所以在'set_forbidden_list'返回一个值的睡眠时间,它不会使LED变暗。 – mudaliyar