[Python] Is there any way to grant a Flask app access to COM ports in windows?

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I wanted to make a Flask server that receives data from an android app, and then sends data over serial to an arduino connected via USB. However, I can't seem to be able to access the COM port where the arduino is connected.

    from flask import Flask
    from flask_restful import Api, Resource, reqparse
    import serial

    # Initialize serial streams
    serCam = serial.Serial('COM3', baudrate=9600, timeout=1)
    if not serCam.isOpen():
    serCam.open()

    app = Flask(__name__)
    api = Api(app)

    ## Here there are flask requests...

    if __name__ == "__main__":
    app.run(debug=True)


    And the error is this one: "serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, 'Access is denied.', None, 5)"

    I have checked that I am trying to connect to the correct port using the arduino terminal and using the same serial related code in another script. I did not forget to close any other serial connections. I also ran the python script with administrator rights, as I read that on windows the COM port privileges are sometimes locked to normal users on windows, but it still doesn't work as I believe Flask starts a subprocess that no longer has admin privileges.

    So is there any way to grant COM port access to a flask application without granting all the root privileges?

    Continue reading...

Compartilhe esta Página