OSError: [Errno 13] Permission denied: '/dev/ttyACM0' - using pyserial from Python to Arduino

前端 未结 2 1974
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 03:53

Environment

  • Linux Mint 17.1
  • Python 2.7
  • pyserial 2.7
  • Arduino UNO rv3

Desired Behaviour

相关标签:
2条回答
  • 2021-02-06 04:38

    The permissions on the file make no difference to the user that the program runs as

    When you are logged in interactively you do have permission to use the /dev/ttyACM0

    When your script is running (presumably as the apache user) it does not have permission

    You need to alter the permissions on the /dev/ttyACM0

    See the 2nd answer here How can I programmatically set permissions on my char device for an example of altering udev permissions so the file has the correct permissions

    0 讨论(0)
  • 2021-02-06 04:53

    The following fleshes out some of the ideas in the first answer (I tried to add this content to that answer and accept it, but the edits were rejected). I'm not an expert in the area, so please just use this information to support your own research.

    You can do one of the following:

    01. Alter the permissions on /dev/ttyACM0 so that world has read and write priviliges (something you may not want to do) - although you may find they reset each time the device is plugged in eg:

    sudo chmod 666 /dev/ttyACM0  
    

    02. Create a rule in /etc/udev/rules.d that will set the permissions of the device (a restart will be required):

    # navigate to rules.d directory
    cd /etc/udev/rules.d
    #create a new rule file
    sudo touch my-newrule.rules
    # open the file
    sudo vim my-newrule.rules
    # add the following
    KERNEL=="ttyACM0", MODE="0666"
    

    This also sets permissions for world to read and write, which you may not want to do.

    For more information about this approach, see these answers:

    https://unix.stackexchange.com/a/48596/92486

    https://stackoverflow.com/a/11848003/1063287

    03. The third option, which is the option I implemented, adds the Apache user to the dialout group so that if the script is being run by Apache, then it can access the device.

    a) Find the location of your Apache config file, then search for the User setting within that file:

    # open file in editor
    sudo vim /etc/apache2/apache2.conf
    # search for User setting
    /User
    

    You may find something like:

    # These need to be set in /etc/apache2/envvars
    User ${APACHE_RUN_USER}
    Group ${APACHE_RUN_GROUP}
    

    b) Quit vim and search for APACHE_RUN_USER in /etc/apache2/envvars (if the above scenario applies):

    # open file in editor
    sudo vim /etc/apache2/envvars
    # search for APACHE_RUN_USER
    /APACHE_RUN_USER
    

    You may find something like:

    export APACHE_RUN_USER=www-data
    

    c) Add the User www-data to the dialout group:

    sudo usermod -a -G dialout www-data
    

    d) Restart.

    As the Apache user has been added to the dialout group, the script should now be able to access the device.

    Further Reading

    How to find the location of the Apache config file:

    https://stackoverflow.com/a/12202042/1063287

    0 讨论(0)
提交回复
热议问题