preritpradeep
Posts: 3
Joined: Thu Jun 22, 2017 7:36 am

Serial port Pemission Denied

Thu Jun 22, 2017 7:50 am

I am trying to send and receive messages using GSM sim900 which is connected to Raspberry pi 3 through Rx and Tx. Raspberry pi is running Jessie on it. I had run the code last time, and it had successfully sent and received messages. However, now when I am running the same code, Python is giving me an error that the "Serial Port can't be accessed. Permission denied "

I know in Raspberry pi 3, Bluetooth is attached to ttyAMA0. So I had swaped that port miniuart port by adding the line dtoverlay=pi3-miniuart-bt in /boot/config.txt . It worked before, however now it just refuses to.

Below is my code.

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

GPIO.setmode(GPIO.BOARD)

# Enable Serial Communication
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)

# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('ATE0'+'\r\n') # Disable the Echo
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications
rcv = port.read(10)
print rcv
time.sleep(1)

# Sending a message to a particular Number

port.write('AT+CMGS="9495353464"'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('Hello User'+'\r\n') # Message
rcv = port.read(10)
print rcv

port.write("\x1A") # Enable to send SMS
for i in range(10):
rcv = port.read(10)
print rcv

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Serial port Pemission Denied

Thu Jun 22, 2017 12:22 pm

Do you sudo your app ?

Then you also might try to disable BT by adding another line to /boot/config.txt :

Code: Select all

# Enable UART
enable_uart=1

## Disable RPi3 bluetooth to enable serial console on UART0
dtoverlay=pi3-disable-bt
Does it works?

YCV-

User avatar
scruss
Posts: 2823
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Serial port Pemission Denied

Thu Jun 22, 2017 2:48 pm

are you a member of the dialout group? (Run groups to find out)
If not,

Code: Select all

    sudo usermod -a -G dialout pi
then logout and log back in again.

Recent Raspbian distributions may have fixed this omission, so you might not need to do this.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

dgordon42
Posts: 777
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Serial port Pemission Denied

Thu Jun 22, 2017 7:37 pm

Your Pi may not be configured correctly to use the Serial Port.
Post the output of the following commands:

Code: Select all

uname -a
cat /boot/cmdline.txt
grep -v ^\# /boot/config.txt
ls -lh /dev/serial*
In addition, you should refer to your Serial Port in code as "/dev/serial0", not "/dev/ttyAMA0" or "/dev/ttyS0". The 'serial0' alias will ensure that your code addresses the correct Serial UART, regardless of which model of Pi you have, and what way you have configured Bluetooth on a Pi 3B or ZeroW.

If you are using "dtoverlay=pi3-miniuart-bt" in your '/boot/config.txt' file, then you should also include the line:

Code: Select all

core_freq=250
in the file. Details in '/boot/overlays/README'.

Finally, can you confirm that your GSM sim900 device's Serial Port operates at 3.3V, and not 5V like a lot of TTL Serial Ports. Putting 5V onto the Pi's Serial Port pins can permanently damage the Pi.

Hope this helps,
Dave.

User avatar
bensimmo
Posts: 4227
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial port Pemission Denied

Thu Jun 22, 2017 8:08 pm

A quick check (ignoring the fact you are turning off bluetooth as it may be as simple as you need to run systemctl disable hciuart)
As I don't know when you started using the Pi3 and if like me it was when the serial0 was a bit of a mess and getting anything going bar turning BT off was the only way.

So,
remove that line, reboot and start from scratch, this will show is the default setup will work.

Now
sudo raspi-config
then
- 5 Interfacing Options
- P6 Serial.
Select NO to Would you like a login console...
Select YES to Would you like the serial port hardware ...

It will then show
The serial login shell is disabled
The serial interface is enabled

- Ok
- Finish
- Reboot - Yes.

This sets the serial port up for use and turns off the console part you also need to remove (and probably some other things? services and such like ?)
Note: if you didn't get the two options, you'll need to update Raspbian.

Now try seeing if you can see what the GSM is sending
cat /dev/serial0
this should see a stream of data, at least with GPS it does, other serial devices should be no different.

Now try whatever you need for the GSM, but using /dev/serial0
See if that works.
Last edited by bensimmo on Thu Jun 22, 2017 8:25 pm, edited 2 times in total.

User avatar
bensimmo
Posts: 4227
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial port Pemission Denied

Thu Jun 22, 2017 8:14 pm

Once you've tried that (it should see something, it should have access, if not it's a missed step I've forgotten about)

Only if it is not stable would you switch to the full UART, from what people have said, way back when.

These are the parts you would need,
So now the serial port is setup you can start to use them
Name: pi3-disable-bt
Info: Disable Pi3 Bluetooth and restore UART0/ttyAMA0 over GPIOs 14 & 15
N.B. To disable the systemd service that initialises the modem so it
doesn't use the UART, use 'sudo systemctl disable hciuart'.
Load: dtoverlay=pi3-disable-bt
Params: <None>


Name: pi3-disable-wifi
Info: Disable Pi3 onboard WiFi
Load: dtoverlay=pi3-disable-wifi
Params: <None>


Name: pi3-miniuart-bt
Info: Switch Pi3 Bluetooth function to use the mini-UART (ttyS0) and restore
UART0/ttyAMA0 over GPIOs 14 & 15. Note that this may reduce the maximum
usable baudrate.
N.B. It is also necessary to edit /lib/systemd/system/hciuart.service
and replace ttyAMA0 with ttyS0, unless you have a system with udev rules
that create /dev/serial0 and /dev/serial1, in which case use
/dev/serial1 instead because it will always be correct. Furthermore,
you must also set core_freq=250 in config.txt or the miniuart will not
work.
Load: dtoverlay=pi3-miniuart-bt
Params: <None>
https://github.com/raspberrypi/firmware ... ays/README

preritpradeep
Posts: 3
Joined: Thu Jun 22, 2017 7:36 am

Re: Serial port Pemission Denied

Mon Jun 26, 2017 6:18 am

Thank You guys for the help. However, It still wont work properly. For the error of serial port permission denied, I am adding "sudo chmod" and "sudo chown" at the start of my code.
There's another issue that I am facing now. Whenever, I run my program, it gives me an error that resource is unavailable. I tried using try and exception, but all in vain. And the real problem is, this error is random. The code runs sometimes, and sometimes it gives me this error.
The transmission code works perfectly, so I know that there is no issue with the module or my connections with raspberry pi.

User avatar
bensimmo
Posts: 4227
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial port Pemission Denied

Mon Jun 26, 2017 8:27 am

What did you try.
Just the permissions part?

What do you see from the commands asked above?

Pop your code up in 'code' tags so people can check it for you (your first posts doesn't have the code tags so all formatting is lost)

preritpradeep
Posts: 3
Joined: Thu Jun 22, 2017 7:36 am

Re: Serial port Pemission Denied

Tue Jun 27, 2017 6:20 am

bensimmo wrote:What did you try.
Just the permissions part?

What do you see from the commands asked above?

Pop your code up in 'code' tags so people can check it for you (your first posts doesn't have the code tags so all formatting is lost)
Below are the lines I used for permissions at the top of new code.

Code: Select all

os.write("sudo chmod 777 /dev/serial0")
os.write("sudo chown pi:pi /dev/serial0")
Below is my entire code

Code: Select all

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

os.write("sudo chmod 777 /dev/serial0")
os.write("sudo chown pi:pi /dev/serial0")

GPIO.setmode(GPIO.BOARD) 

# Enable Serial Communication
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1) #Already swapped the BT port with mini uart. So serial0 and ttyAMA0 are aliases

# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('ATE0'+'\r\n') # Disable the Echo
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('AT+CMGF=1'+'\r\n') # Select Message format as Text mode 
rcv = port.read(10)
print rcv
time.sleep(1)

port.write('AT+CNMI=2,1,0,0,0'+'\r\n') # New SMS Message Indications
rcv = port.read(10)
print rcv
time.sleep(1)
ck=1
while ck==1:
    rcv = port.read(10)
    print rcv
    fd=rcv
    if len(rcv)>3:                   # check if any data received 
        ck=12
        for i in range(5):            
            rcv = port.read(10)
            print rcv
            fd=fd+rcv


            
port.write('AT+CMGR=1'+'\r\n')            # Extract the complete data 
asd=port.read(200)
print(asd)
The previously uploaded code is my sms transmission code which is working properly (Though I am not sure how :? ). The new code is for receiving sms.

dgordon42
Posts: 777
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Serial port Pemission Denied

Tue Jun 27, 2017 11:02 am

As a general rule, you should never change the ownership or permissions of Linux system files.
It is not necessary to change any ownership or permissions of any files to use the serial port on any model of Raspberry Pi.
Changing ownership or permissions of Linux system files just opens up security holes in your system, and may make your system unbootable, and/or result in file or data loss. It will not help you to get your serial port working.
preritpradeep wrote:Below are the lines I used for permissions at the top of new code.

Code: Select all
os.write("sudo chmod 777 /dev/serial0")
os.write("sudo chown pi:pi /dev/serial0")
Please remove these two lines from your code, they are not helping, and not doing what you think they are doing!

If you re-read the post I made earlier to this thread, and post the output of the commands I asked for, I can determine the configuration state of your Pi's serial port, and provide advice on what to do to get it to work.

Dave.

Return to “Troubleshooting”