import serial
import MySQLdb
dbConn = MySQLdb.connect("localhost","root","test","ISEF_DB") or die ("Could not connect to database")
cursor = dbConn.cursor()
device = '/dev/ttyACM0'
try:
print "Trying...",device
arduino = serial.Serial(device, 250000)
except:
print "Failed to connect on",device
try:
data = arduino.readline() #read data
# pieces = data.split("\t")
print "The data is:",data
if data == 461518B1:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '461518B1'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 46154D41:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '46154D41'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 4615A161:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '4615A161'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 4616A511:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '4616A511'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 46193031:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '46193031'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 46196771:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '46196771'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 461A79D1:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '461A79D1'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 46211881:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '46211881'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 465598F1:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '465598F1'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
elif data == 9A2DCDE4:
try:
cursor.execute("UPDATE `ISEF_DB`.`attendance` SET `present`='1' WHERE `id` = '9A2DCDE4'")
dbConn.commit()
cursor.close()
print "data inserted"
except MySQLdb.IntegrityError:
print "Failed to insert data"
finally:
cursor.close()
print "done"
else:
print "Card not recognized"
except:
print "Failed to get data"
此代码给出了以下错误
File "test.py", line 19
if 461518B1 == data:
^
SyntaxError: invalid syntax
我要做的是将变量与字符串进行比较,但它不起作用。我看不出我做错了什么。数据值只能是包含数字和字母的10个if
语句之一。我没有看到任何语法问题。有人可以帮忙解决这个错误吗?
谢谢
答案 0 :(得分:-2)
虽然这并没有直接回答你的问题(在评论中已经充分解决了这个问题, viz ,
"461518B1"
不是字符串,'461518B1'
或id_values = ['461518B1', '46154D41', '4615A161', '4616A511', '46193031', '46196771', '461A79D1', '46211881', '465598F1', '9A2DCDE4'] data = arduino.readline() # read data data = data.strip() # get rid of whitespace like '\r\n' print "The data is:",data if data in id_values: try: cursor.execute("UPDATE 'ISEF_DB'.'attendance' SET 'present'='1' WHERE 'id' = '" + data + "'") dbConn.commit() cursor.close() print "data inserted" except MySQLdb.IntegrityError: print "Failed to insert data" finally: cursor.close() print "done" else: print "Card not recognized {}".format(data)
(使用引号指定字符串文字)
我想指出您可以修改代码以避免冗余,因为您似乎在每个if / elif`块中使用相同的代码块:
a = sin((1−f)⋅δ) / sin δ
b = sin(f⋅δ) / sin δ
x = a ⋅ cos φ1 ⋅ cos λ1 + b ⋅ cos φ2 ⋅ cos λ2
y = a ⋅ cos φ1 ⋅ sin λ1 + b ⋅ cos φ2 ⋅ sin λ2
z = a ⋅ sin φ1 + b ⋅ sin φ2
φi = atan2(z, √x² + y²)
λi = atan2(y, x)