大家好,我不知道為什么唯一運行的塊是我的第一個函數。我試圖將我的 coin_counter 最后一個值傳遞給第二個函數,但我的第一個函數在發布后沒有傳遞該值。而且它也不會打印到控制臺import RPi.GPIO as GPIOimport timeimport threadingGPIO.setmode(GPIO.BCM)GPIO.setup(27,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)lock = threading.Lock()counter = 1pulse = 0def coin_counter(): global counter lock.acquire() try: while True: time.sleep(.1) GPIO.wait_for_edge(27, GPIO.RISING) #print("Pulse comming ! (%s)") %counter counter += 1 return counter finally: lock.release()print(coin_counter())def get_pulse_count(): while True: print('Hello World!')try: coincounter = threading.Thread(target=coin_counter) getpulse = threading.Thread(target=get_pulse_count) coincounter.start() getpulse.start()except KeyboardInterrupt: coincounter.stop() getpulse.stop() GPIO.cleanup()
2 回答

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
我覺得問題符合print(coin_counter())
。它應該被刪除,因為我們在主線程(coin_counter()
調用)中有無限循環。在此行之后不會執行任何代碼。如果我們刪除這一行并添加sleep
進去,get_pulse_count()
那么它就起作用了。此外return counter
,如果你通過全局變量傳遞值不要求counter
。
添加回答
舉報
0/150
提交
取消