我正在尝试在RPI零上创建一个Clock / IN-Clock / OUT设备,该设备将具有2个用于进出时钟的物理按钮(红色和绿色),2X16 LCD显示屏和RFID读取器,它将将所有数据注册到mySQL DB,我创建的功能将不断:从RFID读取,检查按钮,以及将日期/时间显示到LCD,这就是为什么我使用多重处理并行化所有功能的原因 问题是我无法将变量从一个函数传递给另一个函数
我尝试了全局变量-不走运
我试图将'return'放入readdb()函数中,但这仅读取一次 另外,如果我将readdb()作为参数传递给p3 = Process(target = greenbutton,args =(readdb(),))程序将冻结
from multiprocessing import Process
import sys
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
import MySQLdb
import I2C_LCD_driver
import time
mylcd = I2C_LCD_driver.lcd()
reader = SimpleMFRC522()
GPIO.setmode(GPIO.BCM)
def timedisplay():
while True:
mylcd.lcd_display_string(" ", 1)
mylcd.lcd_display_string("Time: %s" %time.strftime("%H:%M"), 1)
time.sleep(3)
mylcd.lcd_display_string(" ", 1)
mylcd.lcd_display_string("Date: %s" %time.strftime("%m/%d/%Y"), 1)
time.sleep(3)
def readdb(): #will extract data from DB that coresponds to rfid key scaned
while True:
try:
idc, text = reader.read()
finally:
GPIO.cleanup()
# Open database connection
db = MySQLdb.connect("localhost","pi","raspberry","mydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM staffs WHERE key_id = '%d'" %idc
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
#global idc
idc = row[0]
key_id = row[1]
name = row[2]
# Now print fetched result
print "id=%s,key_id=%s,name=%s" % (idc, key_id, name,) #prints in console key data
mylcd.lcd_display_string(" ", 2) #clears second row of lcd
mylcd.lcd_display_string(name, 2) #prints nam from database
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
return idc
def greenbutton(v):
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(27)
if input_state == False:
mylcd.lcd_display_string(" ", 2)
print('GREEN Button Pressed')
time.sleep(0.2)
if input_state == False:
db = MySQLdb.connect("localhost","pi","raspberry","mydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
#v=readdb()
sql = "INSERT INTO clocking (`staff_id`, `clock_in_flag`) VALUES ('%s','%d')" %(v, 1)
try:
# Execute the SQL command
cursor.execute(sql)
mylcd.lcd_display_string("Clocking IN", 2)
time.sleep(3)
mylcd.lcd_display_string(" ", 2)
except:
print "Error: unable to fecth data"
# disconnect from server
db.commit()
db.close()
#return
#return
def redbutton(v):
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(22)
if input_state == False:
mylcd.lcd_display_string(" ", 2)
print('RED Button Pressed')
time.sleep(0.2)
if input_state == False:
db = MySQLdb.connect("localhost","pi","raspberry","mydb" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
#v=readdb()
sql = "INSERT INTO clocking (`staff_id`, `clock_in_flag`) VALUES ('%s','%d')" %(v, 0)
try:
# Execute the SQL command
cursor.execute(sql)
mylcd.lcd_display_string("Clocking OUT", 2)
time.sleep(3)
mylcd.lcd_display_string(" ", 2)
except:
print "Error: unable to fecth data"
# disconnect from server
db.commit()
db.close()
if __name__ == '__main__':
while True:
p1 = Process(target=timedisplay)
p1.start()
p2 = Process(target=readdb)
p2.start()
p3 = Process(target=greenbutton, args=(readdb(), )) # this way will not work
p3.start()
p4 = Process(target=redbutton, args=(1, )) # 1 is just for test and it works
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
请给我建议我该如何解决