Page 1 of 1

Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Thu Nov 16, 2017 8:13 am
by poojasuresh
Good Afternoon,
I am trying to send data through UART to the microcontroller. But I am getting an error as below:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/Radio Altimeter/Radaltuart_3l.py", line 17, in <module>
    bytesize=serial.EIGHTBITS
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
SerialException: could not open port /dev/ttyS0: [Errno 13] Permission denied: '/dev/ttyS0'
I have used command sudo chmod 666 /dev/ttyS0 and it works fine but once i reboot it or shut it down and run my code I get the same error. I want to permanently change the permissions. I am also the member of dialout group:

Code: Select all

pi@raspberrypi:~ $ ls -l /dev/ttyS0
crw--w---- 1 root tty 4, 64 Nov 16 07:38 /dev/ttyS0
pi@raspberrypi:~ $ sudo adduser pi dialout
The user `pi' is already a member of `dialout'.
So can anyone please let me know a permanent solution for the problem?

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Thu Nov 16, 2017 2:17 pm
by topguy
You can see that ttyS0 has group "tty" so maybe add "pi" to that group instead.

Have you checked if you have a "/dev/serial0" device also ?

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Wed Mar 21, 2018 9:00 pm
by lara1
Good day guys.I have experienced the same problem,when I change the permission to serial port,after 10 minutes the setting change alone and I lose the permission at groups.
I already add the user pi to the groups dialout and tty.
I used sudo chmod 766 ttyAMA0 or sudo chmod -R a+rw ttyAMA0 commands.Can you suggest me other actions?Thanks

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Wed Mar 21, 2018 10:12 pm
by DougieLawson
Your userid needs to be in the dialout and netdev groups.

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Thu Mar 22, 2018 7:30 pm
by lara1
Hi Dougie,I checked many times the groups.Below the list of pi user.
I already tried to use the comands as root.

pi@chris:~ $ groups pi
pi : pi root adm tty dialout cdrom sudo audio www-data video plugdev games users input netdev spi i2c

gpiopi@chris:~ $ ls -l /dev/ttyAMA0
crw--w---- 1 root tty 204, 64 Mar 22 20:12 /dev/ttyAMA0

pi@chris:~ $ sudo chmod 777 /dev/ttyAMA0

pi@chris:~ $ ls -l /dev/ttyAMA0
crwxrwxrwx 1 root tty 204, 64 Mar 22 20:13 /dev/ttyAMA0

pi@chris:~ $ ls -l /dev/ttyAMA0
crw--w---- 1 root tty 204, 64 Mar 22 20:15 /dev/ttyAMA0

After 2 minutes the permissions are changed alone!!!
Have you got any idea?Thanks

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Mon Mar 26, 2018 5:36 pm
by Richard-TX
I believe you are bumping heads with udev.
Try creating a file in /etc/udev/rules.d with the suffix .rules, e.g. local.rules, and add a line like this to it:

ACTION=="add", KERNEL=="ttyS0", MODE="0666"

As a FYI, I have had timing issues with the serial port hardware on the Rpi so your mileage may vary.

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Mon Mar 26, 2018 6:28 pm
by jojopi
lara1 wrote:
Thu Mar 22, 2018 7:30 pm
pi@chris:~ $ sudo chmod 777 /dev/ttyAMA0
Richard-TX wrote:
Mon Mar 26, 2018 5:36 pm
ACTION=="add", KERNEL=="ttyS0", MODE="0666"
Let us not go crazy. 0666 or 0777 is never the right mode for anything.

The problem here is that something else is using the device. That is why it is in group "tty" instead of "dialout".

Disable serial shell in the interfacing options in "sudo raspi-config".

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Wed Jan 02, 2019 11:38 am
by nabeel313
open the terminal
try to execute the program with sudo

Re: Accessing permission to open port /dev/ttyS0 for Serial communication

Posted: Sun Jun 09, 2019 3:56 pm
by jon_858
You are likely in a race condition with the system. I ran into a similar issue when I installed a package that updated my serial settings. As mentioned above you should verify your Raspi-config settings for your serial port.

Sudo Raspi-config —> Interfacing options —> serial —> no —> yes
Reboot