M
M
Mamol272018-10-05 14:00:50
linux
Mamol27, 2018-10-05 14:00:50

How to open serial port in python without sudo?

Hello!
I'm trying to set up a simple modbus server on my test machine, but I can't access the port without sudo.

[email protected] ~ $groups sas
sas : sas adm tty dialout cdrom sudo dip plugdev lpadmin sambashare

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

Mistake:
spoiler
[Errno 13] could not open port /dev/ttyprintk: [Errno 13] Permission denied: '/dev/ttyprintk' /dev/ttyprintk
[Errno 13] could not open port /dev/ttyS31: [Errno 13] Permission denied: '/dev/ttyS31' /dev/ttyS31
[Errno 13] could not open port /dev/ttyS30: [Errno 13] Permission denied: '/dev/ttyS30' /dev/ttyS30
[Errno 13] could not open port /dev/ttyS29: [Errno 13] Permission denied: '/dev/ttyS29' /dev/ttyS29
[Errno 13] could not open port /dev/ttyS28: [Errno 13] Permission denied: '/dev/ttyS28' /dev/ttyS28
[Errno 13] could not open port /dev/ttyS27: [Errno 13] Permission denied: '/dev/ttyS27' /dev/ttyS27
[Errno 13] could not open port /dev/ttyS26: [Errno 13] Permission denied: '/dev/ttyS26' /dev/ttyS26
[Errno 13] could not open port /dev/ttyS25: [Errno 13] Permission denied: '/dev/ttyS25' /dev/ttyS25
[Errno 13] could not open port /dev/ttyS24: [Errno 13] Permission denied: '/dev/ttyS24' /dev/ttyS24
[Errno 13] could not open port /dev/ttyS23: [Errno 13] Permission denied: '/dev/ttyS23' /dev/ttyS23
[Errno 13] could not open port /dev/ttyS22: [Errno 13] Permission denied: '/dev/ttyS22' /dev/ttyS22
[Errno 13] could not open port /dev/ttyS21: [Errno 13] Permission denied: '/dev/ttyS21' /dev/ttyS21
[Errno 13] could not open port /dev/ttyS20: [Errno 13] Permission denied: '/dev/ttyS20' /dev/ttyS20
[Errno 13] could not open port /dev/ttyS19: [Errno 13] Permission denied: '/dev/ttyS19' /dev/ttyS19
[Errno 13] could not open port /dev/ttyS18: [Errno 13] Permission denied: '/dev/ttyS18' /dev/ttyS18
[Errno 13] could not open port /dev/ttyS17: [Errno 13] Permission denied: '/dev/ttyS17' /dev/ttyS17
[Errno 13] could not open port /dev/ttyS16: [Errno 13] Permission denied: '/dev/ttyS16' /dev/ttyS16
[Errno 13] could not open port /dev/ttyS15: [Errno 13] Permission denied: '/dev/ttyS15' /dev/ttyS15
[Errno 13] could not open port /dev/ttyS14: [Errno 13] Permission denied: '/dev/ttyS14' /dev/ttyS14
[Errno 13] could not open port /dev/ttyS13: [Errno 13] Permission denied: '/dev/ttyS13' /dev/ttyS13
[Errno 13] could not open port /dev/ttyS12: [Errno 13] Permission denied: '/dev/ttyS12' /dev/ttyS12
[Errno 13] could not open port /dev/ttyS11: [Errno 13] Permission denied: '/dev/ttyS11' /dev/ttyS11
Could not configure port: (5, 'Input/output error') /dev/ttyS10
[Errno 13] could not open port /dev/ttyS9: [Errno 13] Permission denied: '/dev/ttyS9' /dev/ttyS9
[Errno 13] could not open port /dev/ttyS8: [Errno 13] Permission denied: '/dev/ttyS8' /dev/ttyS8
[Errno 13] could not open port /dev/ttyS7: [Errno 13] Permission denied: '/dev/ttyS7' /dev/ttyS7
[Errno 13] could not open port /dev/ttyS6: [Errno 13] Permission denied: '/dev/ttyS6' /dev/ttyS6
[Errno 13] could not open port /dev/ttyS5: [Errno 13] Permission denied: '/dev/ttyS5' /dev/ttyS5
[Errno 13] could not open port /dev/ttyS4: [Errno 13] Permission denied: '/dev/ttyS4' /dev/ttyS4
[Errno 13] could not open port /dev/ttyS3: [Errno 13] Permission denied: '/dev/ttyS3' /dev/ttyS3
[Errno 13] could not open port /dev/ttyS2: [Errno 13] Permission denied: '/dev/ttyS2' /dev/ttyS2
[Errno 13] could not open port /dev/ttyS1: [Errno 13] Permission denied: '/dev/ttyS1' /dev/ttyS1
[Errno 13] could not open port /dev/ttyS0: [Errno 13] Permission denied: '/dev/ttyS0' /dev/ttyS0
[Errno 13] could not open port /dev/tty63: [Errno 13] Permission denied: '/dev/tty63' /dev/tty63
[Errno 13] could not open port /dev/tty62: [Errno 13] Permission denied: '/dev/tty62' /dev/tty62
[Errno 13] could not open port /dev/tty61: [Errno 13] Permission denied: '/dev/tty61' /dev/tty61
[Errno 13] could not open port /dev/tty60: [Errno 13] Permission denied: '/dev/tty60' /dev/tty60
[Errno 13] could not open port /dev/tty59: [Errno 13] Permission denied: '/dev/tty59' /dev/tty59
[Errno 13] could not open port /dev/tty58: [Errno 13] Permission denied: '/dev/tty58' /dev/tty58
[Errno 13] could not open port /dev/tty57: [Errno 13] Permission denied: '/dev/tty57' /dev/tty57
[Errno 13] could not open port /dev/tty56: [Errno 13] Permission denied: '/dev/tty56' /dev/tty56
[Errno 13] could not open port /dev/tty55: [Errno 13] Permission denied: '/dev/tty55' /dev/tty55
[Errno 13] could not open port /dev/tty54: [Errno 13] Permission denied: '/dev/tty54' /dev/tty54
[Errno 13] could not open port /dev/tty53: [Errno 13] Permission denied: '/dev/tty53' /dev/tty53
[Errno 13] could not open port /dev/tty52: [Errno 13] Permission denied: '/dev/tty52' /dev/tty52
[Errno 13] could not open port /dev/tty51: [Errno 13] Permission denied: '/dev/tty51' /dev/tty51
[Errno 13] could not open port /dev/tty50: [Errno 13] Permission denied: '/dev/tty50' /dev/tty50
[Errno 13] could not open port /dev/tty49: [Errno 13] Permission denied: '/dev/tty49' /dev/tty49
[Errno 13] could not open port /dev/tty48: [Errno 13] Permission denied: '/dev/tty48' /dev/tty48
[Errno 13] could not open port /dev/tty47: [Errno 13] Permission denied: '/dev/tty47' /dev/tty47

on /dev/ttyS10 tried to change access modes
sudo chmod 766 /dev/ttyS10
sudo chmod -R a+rw /dev/ttyS10
sudo chmod 777 /dev/ttyS10
sudo chmod 666 /dev/ttyS10
[email protected] ~ $ ls -l /dev/ttyS10
crw-rw-rw- 1 root dialout 4, 74 окт  5 10:17 /dev/ttyS10

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-10-05
@GavriKos

Well, anyway. If the program is executed from a user who does not have rights, then that's it. It's type safety.

R
Radjah, 2018-10-05
@Radjah

> root dialout
Add user to "dialout" group and re-login.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question