1

I'm trying to send SMS via PHP using AT commands via USB modem. I can send SMS from PHP server to specific phone numbers. But the problem is, when I'm sending some text to a phone, I'm also getting at commands that I have used to send the SMS.

server.php

$fp = fopen('COM4', 'r+');
$writtenBytes = fputs($fp, "AT\r\n");
$writtenBytes = fputs($fp, "AT+CMGF=1\r\n");
$writtenBytes = fputs($fp, "WAIT=1\r\n");
$writtenBytes = fputs($fp, "AT+CSCS=\"GSM\"\n\r");
$writtenBytes = fputs($fp, "WAIT=1\r\n");
$writtenBytes = fputs($fp, "AT+CMGS=\"+91xxxxxxxxxx\"\r\n");
$writtenBytes = fputs($fp, "sample text here\n\r");
$writtenBytes = fputs($fp, chr(26));

expected output:

sample text here

But what I'm receiving as SMS is:

AT

AT+CMGF=1

WAIT=1

AT+CSCS="GSM"

WAIT=1

AT+CMGS="+91xxxxxxxxxx"

sample text here

How can I send SMS in such a way that no AT commands are send to the phone?

Sumithran
  • 6,217
  • 4
  • 40
  • 54

1 Answers1

2

Your code contains three fundamental misconceptions that if you do not fix, you will never be able to have successful AT command communication.

First the most important ting: You must read and parse everything the modem sends back to you. Sometimes people incorrectly use delay, sleep etc instead of parsing the response, but here you are not even doing that but instead calling fputs as fast as possible. That is a disaster recipy and will not be even remotely close to sometimes working reliably. I highly recommend abandoning/unsubscribing from any blog/website/video channel that shows such code examples1.

Then the one the question is asking about. If you care about whether echo is on or not, you're doing it wrong.

And specifically for AT+CMGS you also MUST wait for the "ready to receive payload data" prompt before sending the SMS payload.


1 In the same way that if someone recommends cleaning the inside of a car engine with soap and water, that is someone you never should listen to for anything related to car maintenance. They might still provide valuable information in other areas (say food recipes and cooking advice), but do not pick up car mechanics tips from them.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • thanks for your valuable information and time,that was helpfull. But still having doubts, instead of sleep() what can i use in order it make a delay (in PHP). – Sumithran Aug 02 '19 at 14:39
  • I dont know how, but i solved the problem by just adding ob_flush() at the end of the code!! – Sumithran Aug 02 '19 at 14:40