用烧瓶运行多个python函数 [英] Run multiple python functions using flask
问题描述
第二次更新
几乎那里!但是得到一个ValueError:试图使用一个未打开的端口
lockquote
$ $ p $
文件c :\Python27\lib\site-packages\flask\app.py,行1701,在__call__
返回self.wsgi_app(environ,start_response)
文件 c:\Python27\lib\site-packages\flask\app.py,行1689,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件c:\Python27\lib\site-packages\flask\app.py,行1687,在wsgi_app
response = self.full_dispatch_request()
文件c:\Python27\lib\site-packages\flask\app.py,第1360行,在full_dispatch_request
中rv = self.handle_user_exception(e)
文件c:\Python27\lib\site-packages\flask\app.py,第1358行,在full_dispatch_request
rv = self.dispatch_request()
文件c:\Python27\lib\site-packages\flask\app.py,li ne 1344,在dispatch_request
返回self.view_functions [rule.endpoint](** req.view_args)
文件G:\OverAir\arduino\server.py,第19行,在light_off
board.output([pin])
文件G:\OverAir\arduino\arduino.py,行16,在输出
self .__ sendData(len(pinArray))
文件G:\OverAir\arduino\arduino.py,第56行,在__sendData
while(self .__ getData ):
在__getData
文件G:\OverAir\arduino\arduino.py,第61行,返回self.serial。 readline()。rstrip('\\\
')
文件c:\Python27\lib\site-packages\serial\serialwin32.py,第221行,
如果不是self.hComPort:raise portNotOpenError
ValueError:尝试使用未打开的端口
更新
下面的@Blender答案代码可能是正确的。现在的问题
我得到的是我越来越SerialException:无法打开端口
COM5:[错误5]访问被拒绝。
但是,如果我单独运行我的原始,on.py或off.py脚本,它
工作正常...我看不到如何新代码不起作用。 ..
试图打开两次COM5
$ b $ END $ UPDATE
$ / $ $ $ $
我试图使用Flask运行多个python函数,或单独的.py脚本;以生成单独的网址。例如,我有on.py和off.py,并希望构建sever.py。只需要打开Arduino ON / OFF在本地运行它。
所以,当我打开127.0.0.1:5000/on它运行on.py,类似地127.0.0.1:5000/off运行off.py,
几个小时后,我只是决定将两个.py函数添加到server.py中,但当然这是行不通的。我想我只是不明白参数的用法。
真的很感谢你!
##打开与Arduino的串行连接。
从进口时间睡眠
从arduino进口Arduino
$ b $从烧瓶进口Flask
b = Arduino(COM5 ,9600)
pin = 13
num = 5
ON = Flask(Light_ON)
OFF = Flask(Light_OFF)
$ b $ def LightON():
#declare作为列表/元组的输出引脚
b.output([pin])
b.setHigh(pin)
b.close()
return'Light is ON'
$ b $ def LightOFF():
#declare输出引脚作为列表/元组
b.output [pin])
b.setLow(pin)
return'灯不亮'
@ ON.route('/ ON')
@ OFF.ROUTE ('/ OFF')
ON.run()
OFF.run()//我知道这个不能运行,只是不知道如何包含它。
b
$ b
from arduino import Arduino
from flask import Flask
$ b app = Flask('light_control')
board = Arduino('COM5',9600)
pin = 13
@ app.route('/ on')
def light_on():
board .output([pin])
board.setHigh(pin)
board.close()
返回'灯亮'
@app ())
def light_off():
board.output([pin])
board.setLow(pin)
#board.close() ??
返回'Light is OFF'
if __name__ =='__main__':
app.debug = True
app.run()
2nd UPDATE
Almost there!! But getting a "ValueError: Attempting to use a port that is not open"
File "c:\Python27\lib\site-packages\flask\app.py", line 1701, in __call__ return self.wsgi_app(environ, start_response) File "c:\Python27\lib\site-packages\flask\app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "c:\Python27\lib\site-packages\flask\app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "c:\Python27\lib\site-packages\flask\app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "c:\Python27\lib\site-packages\flask\app.py", line 1358, in full_dispatch_request rv = self.dispatch_request() File "c:\Python27\lib\site-packages\flask\app.py", line 1344, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "G:\OverAir\arduino\server.py", line 19, in light_off board.output([pin]) File "G:\OverAir\arduino\arduino.py", line 16, in output self.__sendData(len(pinArray)) File "G:\OverAir\arduino\arduino.py", line 56, in __sendData while(self.__getData()[0] != "w"): File "G:\OverAir\arduino\arduino.py", line 61, in __getData return self.serial.readline().rstrip('\n') File "c:\Python27\lib\site-packages\serial\serialwin32.py", line 221, in read if not self.hComPort: raise portNotOpenError ValueError: Attempting to use a port that is not open
UPDATE
The code from @Blender answer below is probably correct. The issue now I am having is I'm getting "SerialException: could not open port COM5: [Error 5] Access is denied."
However, if I run my original, on.py or off.py script individually, it works fine...I don't see how the new code is not working...unless does it try to open COM5 twice??
END UPDATE
I trying to run multiple python functions, or separate .py scripts, using Flask; in order to generate separate URLs. Just trying to turn the Arduino ON/OFF running it locally.
For example, I have on.py and off.py and want to build sever.py. So, when I open 127.0.0.1:5000/on it runs on.py, similarly 127.0.0.1:5000/off runs off.py,
After a few hours, I just decided to add both .py functions into the server.py, but of course it doesn't work...I think I just don't understand the parameters usage.
Really thank you in advance!
## Open a serial connection with Arduino.
from time import sleep
from arduino import Arduino
from flask import Flask
b = Arduino("COM5",9600)
pin = 13
num = 5
ON = Flask("Light_ON")
OFF = Flask("Light_OFF")
def LightON():
#declare output pins as a list/tuple
b.output([pin])
b.setHigh(pin)
b.close()
return 'Light is ON'
def LightOFF():
#declare output pins as a list/tuple
b.output([pin])
b.setLow(pin)
return 'Light is OFF'
@ON.route('/ON')
@OFF.route('/OFF')
ON.run()
OFF.run() //I know this doesn't run, just not sure how to include it.
Something like this should work:
from arduino import Arduino
from flask import Flask
app = Flask('light_control')
board = Arduino('COM5', 9600)
pin = 13
@app.route('/on')
def light_on():
board.output([pin])
board.setHigh(pin)
board.close()
return 'Light is ON'
@app.route('/off')
def light_off():
board.output([pin])
board.setLow(pin)
# board.close() ???
return 'Light is OFF'
if __name__ == '__main__':
app.debug = True
app.run()
这篇关于用烧瓶运行多个python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!