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

眉间皱痕 提交于 2019-12-09 06:05:46

问题


Environment

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

Desired Behaviour

I'm trying to send three values from a Python application to Arduino.

It works when doing the following from terminal:

$ python
$ import serial
$ import struct
$ ser = serial.Serial('/dev/ttyACM0', 9600)
$ ser.write(struct.pack('>3B', 255, 0, 0))

Current Behaviour

It doesn't work when using the same code in a Python file ie:

import serial
import struct
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write(struct.pack('>3B', red_value, green_value, blue_value))

Error Message

$ sudo tail -100 /var/log/apache2/error.log
OSError: [Errno 13] Permission denied: '/dev/ttyACM0'

Troubleshooting

Permissions

Application file:

$ ls -l
-rwxr-xr-x 1 myname mygroupname 114146 Jan  9 19:16 my_application.py

ttyACM0:

ls -l /dev/ttyACM0
crw-rw---- 1 root dialout 166, 0 Jan  9 20:12 /dev/ttyACM0

Groups

Groups the owner is a member of:

$ groups
mygroupname adm dialout cdrom sudo dip plugdev lpadmin sambashare

Due to various suggestions on the internet I also added the owner to the tty group via System Settings > Users and Groups. This had no effect.

Serial Ports Available

$ dmesg | grep tty
[    0.000000] console [tty0] enabled
[ 3390.614686] cdc_acm 3-2:1.0: ttyACM0: USB ACM device

Update

I can force it to work under the following conditions:

01. Permissions for world must be set to rw ie:

sudo chmod 666 /dev/ttyACM0

02. Arduino IDE serial monitor needs to be open.

However these conditions are not sustainable as:

  • Permissions are reset each time the USB is connected.
  • The Arduino IDE serial monitor shouldn't need to be open.

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/27858041/oserror-errno-13-permission-denied-dev-ttyacm0-using-pyserial-from-pyth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!