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

user1063287 picture user1063287 · Jan 9, 2015 · Viewed 42.6k times · Source

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.

Answer

user1063287 picture user1063287 · Jan 11, 2015

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