0

I want to communicate between Raspberry pi 3 B+ and GSM GPRS A6. I tried and I am unable to send data to GPRS Module from Raspberry pi.

Now, I know that GPIO serial port is disabled by default in newer Operating Systems (in my case Raspbian Stretch), so I have enabled it by adding following line in config.txt file,

enable_uart=1

Here's my Code:

import serial
import time

port = "/dev/ttyS0"
COMM = serial.Serial(port, baudrate=115200)

while(1):
    COMM.write("AT\r")
    print (COMM.read(5))

This command is supposed to return "OK", but it does not and nothing is printed. I am using python 2.7.

Some people suggested me to send data using this method,

COMM.write('AT' + '\r')

I tried but it didn't help.

There is no problem with my GPRS module. It works file with arduino. So, what am I doing wrong here?

Thanks in advance!

  • 1
    Try \n instead of \r and let us know whether it changes anything. If it does I'll explain why. – Mark Smith Dec 25 '17 at 18:35
  • @MarkSmith So I tried \n, and now sometimes it shows correct output and some time it shows garbage. – Muzahir Hussain Dec 26 '17 at 10:30
  • Do I understand correctly that it's better than it was? Now at least working somewhat, some of the time? – Mark Smith Dec 26 '17 at 10:34
  • yes... So, whats the reason behind '\n'? – Muzahir Hussain Dec 26 '17 at 10:42
2

When you write "\n" in a C program, the compiler replaces it with a single character. The \ is called an 'escape', and it gives a special meaning to the character following it -- in this case the n.

Commands sent to the devices must be terminated with a suitable line ending, so it knows where the command ends. Common line endings are \n (which is a single number 10, or hex 0x0A) used by Unix-like systems, and \r\n (which is a 13 and a 10, or 0x0D 0x0A), used by Windows. I imagine your modules might work either combination. Nothing, as far as I know, uses just a \r.

There are various ways you can send it.

COMM.write("AT\n")

is fine.

However

COMM.write('AT' + '\r')

will not do what you want. The reasoning is a bit involved, but for now, probably don't worry about it and just use the former.

  • In fact plain \r is the line terminator for AT (aka Hayes) commands. However, using \r\n is okay, and if you set the modem into verbose mode it will reply with \r\n as well. So your suggestion to just use "AT\n" will not work in this case. – goldilocks Dec 26 '17 at 16:00
  • Gosh, so it is. Strange thing then is that I don't know why changing to \n helped. – Mark Smith Dec 26 '17 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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