0

I want to automatically have my serial connection get permission through the code instead of having to use terminal to give permission every time.

I tried using import os to use os.system() to automatically give permission but permission is always denied.

def checkConnection(password):
    if os.path.exists('/dev/ttyUSB0'):
        usb = '/dev/ttyUSB0'
        os.system('echo password | sudo chmod 666 /dev/ttyUSB0')
        return usb
    elif os.path.exists('/dev/ttyUSB1'):
        usb = '/dev/ttyUSB0'
        os.system('echo password | sudo chmod 666 /dev/ttyUSB1')
        return usb
    else:
        print('Is there a console connected?')

The error that I keep getting is

 self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
 PermissionError: [Errno 13] Permission denied: '/dev/ttyUSB0'
sidfvdx125
  • 15
  • 5

1 Answers1

0

I see what you're trying to achieve, I hear what you're saying, but this is not the right way to go about it. Neither the use of echo password is sound, nor is the idea of giving 666 to the device. Just add your user to the group that owns the devices, and leave it at that.

tink
  • 14,342
  • 4
  • 46
  • 50