0
votes

I try to open serial port, but cant with permission. But works with sudo.

How i can get permission for serial port?

sas@sas-linuxmint ~ $groups sas
sas : sas adm tty dialout cdrom sudo dip plugdev lpadmin sambashare

This my code:

def get_serial_port():
    ser_devs = [dev for dev in os.listdir('/dev') if dev.startswith('tty')]
    for i in ser_devs:
        port = "/dev/" + i
        try :
            ser = serial.Serial(port, 19200)
            if ser.is_open:
                print("OPEN!!!!!!!!!!!!!!!!!!!!!! {}".format(port))
            ser.close()
        except serial.SerialException as e:
            print(e, port)
    return None

output:

[Errno 13] could not open port /dev/ttyprintk: [Errno 13] Permission denied: '/dev/ttyprintk' /dev/ttyprintk

for all ports.

I tried change mod for port, but it doesn't work too

sudo chmod 766 /dev/ttyS10
sudo chmod -R a+rw /dev/ttyS10
sudo chmod 777 /dev/ttyS10
sudo chmod 666 /dev/ttyS10
1
It look like it is not python issue. Did you try to use root or sudo to execute your code?Andy Wong
What are you trying to accomplish with this?? Not every tty is supposed to be handled by normal users, especially /dev/ttyprintk ;-)Alfe
"Did you try to use root or sudo to execute your code?" Yes, with sudo it works.mamol
" Not every tty is supposed to be handled by normal users, especially /dev/ttyprintk ;-)" I try for all ports /dev/tty*, /dev/ttyprintk just first from list :)mamol

1 Answers

-1
votes

may be 2 years late but, you simply add your user to the tty group eg:

sudo usermod -a -G tty $USER

link