我试图从函数调用中断一个while循环。 blink来自脚本 control.py 。 这是从脚本 main.py 运行的。 我的目的是调用闪烁功能并在调用函数关闭时停止闪烁。
我尝试使用调用x =" 0" 并在off函数上调用X
Control.py
from ConfigParser import SafeConfigParser
from SimpleXMLRPCServer import SimpleXMLRPCServer
import RPi.GPIO as GPIO
from time import sleep as sleep
import xmlrpclib
#setup settings
parser = SafeConfigParser()
parser.read('/home/pi/ns/led.conf') # get GPIO Pin & Run time settings
proxy = xmlrpclib.ServerProxy("http://localhost:8000/") #Set host for LCD Screen
GPIO_PIN = int(parser.get('settings', 'GPIO_PIN'))#Set GPIO pin
run = float(parser.get('settings', 'run_time'))#set run time
def gpio():
return GPIO_PIN
int(parser.get('settings', 'GPIO_PIN'))#Set
def setup_gpio():
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(GPIO_PIN, GPIO.OUT)
def on():
try:
proxy.normal()
GPIO.output(GPIO_PIN, GPIO.HIGH)
proxy.stat_ok()
except Exception as e:
print e
GPIO.cleanup()
notify(e)
def off():
try:
proxy.normal()
GPIO.output(GPIO_PIN, GPIO.LOW)
proxy.stat_ok()
except Exception as e:
print e
GPIO.cleanup()
notify(e)
def blink():
try:
proxy.normal()
while true:
GPIO.output(GPIO_PIN, GPIO.HIGH)
sleep(run)
GPIO.output(GPIO_PIN, GPIO.LOW)
except Exception as e:
print e
GPIO.cleanup()
notify(e)
try:
server = SimpleXMLRPCServer(("localhost", 8001), allow_none=True)
print "Control Listening on port 8001..."
proxy.control_on()
server.register_function(on)
server.register_function(off)
server.register_function(blink)
server.serve_forever()
except Exception as error:
print error
proxy.control_off()
main.py
import xmlrpclib
control = xmlrpclib.ServerProxy("http://localhost:8001/")
while True:
cont = raw_input("Please select (O)n, (B)link, or O(F)F :")
if cont == "O":
control.on() # LED ON
elif cont == "F":
control.off() # LED OFF
elif cont == "B":
control.blink() # LED BLINK
else:
print "please try again"