I want to import some function in form of a script, let's call it controller.py, to a flask app as a web service. Let's call the flask app api.py.
The problem is, in controller.py, there is a pyserial declaration.
controller.py:
import serial
ser = serial.Serial('COM31', 9600,timeout=2)
def serial_function(foo):
ser.write(foo)
reply = ser.read()
return reply
api.py:
from flask import Flask
import controller as cont
app = Flask(__name__)
@app.route('/function/<foo>',methods=['GET'])
def do_function(foo):
data=cont.serial_function(foo)
return data
if __name__ == '__main__':
app.run('0.0.0.0', 80,True)
But i got this error:
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port COM31: [Error 5] Access is denied.
It seems that Flask is trying to import controller.py over and over again, and the serial port re initialized.
Is there some way I can achieve what I'm trying to do as described above?