1

I'm using some XBee to build a mesh network and everything is working, BUT only if I run the code using sudo. Now that the code is "Finished" i want to remove this need, because i know that it isn't good.

Basic serial reading code:

import time
import serial
import RPi.GPIO as GPIO
import os

os.system("sudo systemctl stop serial-getty@ttyS0.service")
os.system("sudo systemctl stop serial-getty@serial0.service")

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23,GPIO.OUT)
ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate = 115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1             
 )
while 1:
    x=ser.readline()    
    print(x)

Command sent to terminal:
working: sudo python3 Desktop/Old/mustwork.py
doesn't work: python3 Desktop/Old/mustwork.py error:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/serial/serialposix.py", line 265, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
PermissionError: [Errno 13] Permission denied: '/dev/serial0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Desktop/Old/mustwork.py", line 14, in <module>
    timeout=1             
  File "/home/pi/.local/lib/python3.5/site-packages/serial/serialutil.py", line 240, in __init__
    self.open()
  File "/home/pi/.local/lib/python3.5/site-packages/serial/serialposix.py", line 268, in open
    raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/serial0: [Errno 13] Permission denied: '/dev/serial0'

Some raspberry configs:

$ uname -a
Linux raspberrypi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux

$ cat /boot/config.txt
enable_uart=1

$ grep -v ^\# /boot/config.txt
enable_uart=1

$ ls -lh /dev/serial*
lrwxrwxrwx 1 root root 5 mai 18 20:59 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 mai 18 20:59 /dev/serial1 -> ttyAMA0

$ groups ${USER}
pi : pi adm tty dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

Already went to raspi-config -5 -P6 -Enable

  • What are the rights on /dev/ttyS0? – Janka May 19 at 0:54
1

Why are you calling

os.system("sudo systemctl stop serial-getty@ttyS0.service")
os.system("sudo systemctl stop serial-getty@serial0.service")

These are the same interface, and if you aren't using serial console it is better to disable (using raspi-config) then there is no need for sudo.

There is no need to use sudo with /dev/serial0 provided the user is a member of group dialout (which is default for pi)

Incidentally why do you have serial in /home/pi/.local/lib/python3.5/site-packages this is included in the normal python library?

  • I'm using this os.system("sudo systemctl stop serial-getty@serial0.service") because sometimes i get an error message of a kind of conflict, like if the system was trying to access it too. I know, it souldn't need sudo permission... =/ – SWoto May 19 at 12:47
0

Found the answer following this tutorial.

It worked after removing the line: 'console=serial0,115200', in the cmdline.txt like bellow:

$ sudo nano /boot/cmdline.txt

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes root wait
  • Rather than fiddling with the cmdline it is better to disable serial console in raspi-config, as suggested – Milliways May 19 at 22:49

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.