5

I'm trying to connect my Arduino to my RPi through python and PySerial, and I'm using the following code:

import serial
ser = serial.Serial('/dev/serial/ttyACM0', 9600)

and I was getting an issue along the lines of the program not being able to open the COM port due to a permission issue

Traceback(Most recent call last):
    File "<pyshell#2>", line 1, in <module>
        ser = serial.Serial('/dev/ttyACM0', 9600)
    File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in___init__
        self.open()
    File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 276, in open
        raise SerialException("could not open port %s: %s* % (self._port, msg))
SerialException: could not open port /dev/ttyACM0: [Errno 13] Permission denied:'/dev/ttyACM0'

So according to this question I substituted the /dev/ttyACM0 to what I found in /dev/serial/by-id. That turned out to be the id

usb-Arduino__www.arduino.cc__0043_7434353035171C0B2-if00

to make

/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_7434353035171C0B2-if00

and now I am receiving the same error in the python program, which seemed to work for the asker in the previous question.

Any ideas?

2

You can start python as root, for a quick and dirty workaround:

sudo python

and then

import serial
....

Of course, this is not the best way to go, it would be better to try setting different permissions on the device itself, like

sudo chmod 777 /dev/ttyACM0

or (for exclusive access to the user pi)

sudo chown pi:pi /dev/ttyACM0

or similar. You can get some more information here.

9

@LiWi's answer will work temporarily, but the permission change will be lost once the device is removed or the server rebooted.

A permanent solution is to add your user to the dialout group, which will allow it to access serial devices.

sudo usermod -a -G dialout youruser
0

i had the same probleme ; so I figure out another way to read RX flue ; here is the code :

   import os
   #....

    rx='0'  #erase old data
   ch = os.popen("sudo cat /dev/ttyAMA0", "r")  # send terminal commande with
            root permission and save the resulte in "ch"
    while(j<20):  # for me 20 lines are suffisent 
        rx+= ch.readline()   # read line and concat lines
        j+=1       # next loop/line
   ch.close()    # close ch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.