我试图使我的代码不会一遍又一遍地循环整个事情,我试图使其停止并仅循环执行第一个执行(如果那里什么都没有发生)。执行命令1的功能是它从卡中读取数据并将其存储到db中。 这是我尝试与代码一起使用的当前if语句:
编辑 当前,首先执行comman2,然后执行c1,即使没有输入,程序也会继续循环 我试图使第一个execute(command2)在没有输入的情况下停留在comman2上,而只有在有输入时才移至c1(第二个执行)
import serial
from datetime import datetime, timedelta
ser = serial.Serial('COM4',9600)
print("Serial input ready..")
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="rfid",
passwd="rfidpasswd",
database="rfid"
)
mycursor = mydb.cursor()
try:
while True:
line = ser.readline().strip()
l= line
data = line.decode('ascii').split(':')
rid = data[0]
tag = data[1]
t = datetime.now() -timedelta(minutes = 190 )
command2 = "INSERT INTO tag_logs (reader_id, tag_no) VALUES ('" +rid+ "','" +tag+ "')"
mycursor.execute(command2)
mydb.commit()
c1 = "Select * From tag_logs where reader_id = 'Reader_001' and timestamp > '" + str(t) +"' and tag_no = '"+tag+"'"
print(c1)
n=mycursor.execute(c1)
result1 = mycursor.fetchall()
print((n))
print (len(result1))
x= (len(result1))
mycursor = mydb.cursor(buffered=True)
if x > 0:
ser.write(b'H')
print('done')
else:
ser.write(b'L')
print('deny')
if l == 1:
n=mycursor.execute(c1)
else:
mycursor.execute(command2)
except KeyboardInterrupt:
pass