0

I am getting this error whenever I am trying to run this code:

import serial
from time import sleep

ser = serial.Serial ("/dev/ttyS0", 9600)    #Open port with baud rate
while True:
    received_data = ser.read()              #read serial port
    sleep(0.03)
    data_left = ser.inWaiting()             #check for remaining byte
    received_data += ser.read(data_left)
    print (received_data)                   #print received data
    ser.write(received_data)

Error: Could not open port /dev/ttyS0: Errno 13: Permission denied: '/dev/ttyS0'

I referred This link for configuring my RPI model 3B+ I also tried This link for this problem but not successful. My user "pi" is already the member of the dialout group.

pi@raspberrypi:~ $ groups pi
pi : pi adm tty dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

Still, whenever I am trying to run this code, this error pops up. Would be thankful for any help and suggestion.

3
  • 2
    Please edit the result of ls -l /dev/ttyS0 into your question.
    – joan
    Commented Dec 23, 2018 at 17:12
  • 1
    Are you running Raspbian or some other OS? What does a udevadm info -a -n /dev/ttyS0 command show?
    – Dougie
    Commented Dec 23, 2018 at 19:31
  • @Dougie I am running latest Raspbian, and will be editing this comment once I get to my system.
    – Sajal
    Commented Dec 23, 2018 at 23:20

1 Answer 1

1

Your process is likely not running with elevated permissions. You should run it using sudo. Also, your programming model should be an event-driven one, not a poll in place one using a constant read and sleep -- that won't work well at all. You can attach an event handler to the ser object and only read when data is present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.