我正在使用一个系统,该系统应该使我的树莓派 GPIO 引脚之一处于高电平大约 2 秒。我把它分成两个不同的文件。 “website”文件(名为 app.py)和“GPIO”文件(名为 test.ty)。测试文件的请求方式如下:
from flask import Flask, render_template
from test import open_door
app = Flask(__name__)
@app.route('/opendoor')
def openDoor():
open_door()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
test.py 文件如下所示:
import RPi.GPIO as GPIO
import time
testPin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(testPin, GPIO.OUT)
counter =0
def open_door():
try:
print ("Everthing is fine")
while counter < 900000:
print ("Everything is good")
GPIO.output(testPin, GPIO.HIGH)
counter += 1
except:
print ("Everything is oke!")
finally:
GPIO.cleanup()
我收到消息“一切都很好”和“一切都很好!”但不是“一切都很好”的信息。在我看来, while 循环没有执行。 有人知道为什么它不起作用吗?
最佳答案
counter
不在调用 open_door()
的其他文件的范围内,这就是为什么您会看到“一切正常!”因为未知变量是一个异常(exception)
关于python - while 循环不会循环或执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841056/