title: OSError: [Errno 13] Permission denied: '/dev/ttyACM0' - using pyserial from Python to Arduino
body: <p><strong>Environment</strong></p>
  
  <ul>
  <li>Linux Mint 17.1</li>
  <li>Python 2.7</li>
  <li>pyserial 2.7</li>
  <li>Arduino UNO rv3</li>
  </ul>
  
  <p><strong>Desired Behaviour</strong></p>
  
  <p>I'm trying to send three values from a Python application to Arduino.  </p>
  
  <p>It works when doing the following from terminal:</p>
  
  <pre><code>$ python
  $ import serial
  $ import struct
  $ ser = serial.Serial('/dev/ttyACM0', 9600)
  $ ser.write(struct.pack('&gt;3B', 255, 0, 0))
  </code></pre>
  
  <p><strong>Current Behaviour</strong></p>
  
  <p>It doesn't work when using the same code in a Python file ie:</p>
  
  <pre><code>import serial
  import struct
  ser = serial.Serial('/dev/ttyACM0', 9600)
  ser.write(struct.pack('&gt;3B', red_value, green_value, blue_value))
  </code></pre>
  
  <p><strong>Error Message</strong></p>
  
  <pre><code>$ sudo tail -100 /var/log/apache2/error.log
  OSError: [Errno 13] Permission denied: '/dev/ttyACM0'
  </code></pre>
  
  <p><strong>Troubleshooting</strong></p>
  
  <p><em>Permissions</em></p>
  
  <p>Application file:</p>
  
  <pre><code>$ ls -l
  -rwxr-xr-x 1 myname mygroupname 114146 Jan  9 19:16 my_application.py
  </code></pre>
  
  <p>ttyACM0:  </p>
  
  <pre><code>ls -l /dev/ttyACM0
  crw-rw---- 1 root dialout 166, 0 Jan  9 20:12 /dev/ttyACM0
  </code></pre>
  
  <p><em>Groups</em></p>
  
  <p>Groups the owner is a member of:  </p>
  
  <pre><code>$ groups
  mygroupname adm dialout cdrom sudo dip plugdev lpadmin sambashare
  </code></pre>
  
  <p>Due to various suggestions on the internet I also added the owner to the <code>tty</code> group via System Settings > Users and Groups.  This had no effect.  </p>
  
  <p><em>Serial Ports Available</em></p>
  
  <pre><code>$ dmesg | grep tty
  [    0.000000] console [tty0] enabled
  [ 3390.614686] cdc_acm 3-2:1.0: ttyACM0: USB ACM device
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I can <em>force</em> it to work under the following conditions:</p>
  
  <p><strong>01.</strong>  Permissions for world must be set to <code>rw</code> ie:</p>
  
  <pre><code>sudo chmod 666 /dev/ttyACM0
  </code></pre>
  
  <p><strong>02.</strong>  Arduino IDE serial monitor needs to be open.  </p>
  
  <p>However these conditions are not sustainable as:</p>
  
  <ul>
  <li>Permissions are reset each time the USB is connected.  </li>
  <li>The Arduino IDE serial monitor shouldn't need to be open.  </li>
  </ul>