1
votes

I am using GPRS MODEM to send SMS using php with this device.

I tried my device using AT Tester to determine if my device works well and it successfully sent.

Then I tried this link which uses PHP to send SMS via GPRS Modem: https://www.sitepoint.com/community/t/send-sms-using-gsm-modem/28584 .

Here is the code that was provided :

<?php 

//Example

error_reporting(E_ALL);

//Example

$gsm_send_sms = new gsm_send_sms();
$gsm_send_sms->debug = true;
$gsm_send_sms->port = 'COM4';
$gsm_send_sms->baud = 9600;
$gsm_send_sms->init();

$status = $gsm_send_sms->send('+639153380630', 'testing 123');
if ($status) {
    echo "Message sent\
";
} else {
    echo "Message not sent\
";
}

$gsm_send_sms->close();


//Send SMS via serial SMS modem
class gsm_send_sms {

    public $port = 'COM4';
    public $baud = 9600;

    public $debug = true;

    private $fp;
    private $buffer;

    //Setup COM port
    public function init() {

        $this->debugmsg("Setting up port: \"{$this->port} @ \"{$this->baud}\" baud");

        exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);

        if ($retval != 0) {
            throw new Exception('Unable to setup COM port, check it is correct');
        }

        $this->debugmsg(implode("\n", $output));

        $this->debugmsg("Opening port");

        //Open COM port
        $this->fp = fopen($this->port . ':', 'r+');

        //Check port opened
        if (!$this->fp) {
            throw new Exception("Unable to open port \"{$this->port}\"");
        }

        $this->debugmsg("Port opened");
        $this->debugmsg("Checking for responce from modem");

        //Check modem connected
        fputs($this->fp, "AT\r");

        //Wait for ok
        $status = $this->wait_reply("OK\r\n", 5);

        if (!$status) {
            throw new Exception('Did not receive responce from modem');
        }

        $this->debugmsg('Modem connected');

        //Set modem to SMS text mode
        $this->debugmsg('Setting text mode');
        fputs($this->fp, "AT+CMGF=1\r");

        $status = $this->wait_reply("OK\r\n", 5);

        if (!$status) {
            throw new Exception('Unable to set text mode');
        }

        $this->debugmsg('Text mode set');

    }

    //Wait for reply from modem
    private function wait_reply($expected_result, $timeout) {

        $this->debugmsg("Waiting {$timeout} seconds for expected result");

        //Clear buffer
        $this->buffer = '';

        //Set timeout
        $timeoutat = time() + $timeout;

        //Loop until timeout reached (or expected result found)
        do {

            $this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");

            $buffer = fread($this->fp, 1024);
            $this->buffer .= $buffer;

            usleep(200000);//0.2 sec

            $this->debugmsg("Received: {$buffer}");

            //Check if received expected responce
            if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
                $this->debugmsg('Found match');
                return true;
                //break;
            } else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
                return false;
            }

        } while ($timeoutat > time());

        $this->debugmsg('Timed out');

        return false;

    }

    //Print debug messages
    private function debugmsg($message) {

        if ($this->debug == true) {
            $message = preg_replace("%[^\040-\176\n\t]%", '', $message);
            echo $message . "\n";
        }

    }

    //Close port
    public function close() {

        $this->debugmsg('Closing port');

        fclose($this->fp);

    }

    //Send message
    public function send($tel, $message) {

        //Filter tel
        $tel = preg_replace("%[^0-9\+]%", '', $tel);

        //Filter message text
        $message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);

        $this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");

        //Start sending of message
        fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");

        //Wait for confirmation
        $status = $this->wait_reply("\r\n> ", 5);

        if (!$status) {
            //throw new Exception('Did not receive confirmation from modem');
            $this->debugmsg('Did not receive confirmation from modem');
            return false;
        }

        //Send message text
        fputs($this->fp, $message);

        //Send message finished indicator
        fputs($this->fp, chr(26));

        //Wait for confirmation
        $status = $this->wait_reply("OK\r\n", 180);

        if (!$status) {
            //throw new Exception('Did not receive confirmation of messgage sent');
            $this->debugmsg('Did not receive confirmation of messgage sent');
            return false;
        }

        $this->debugmsg("Message sent");

        return true;
    }
}
?>

After executing the code, PHP returns with this error :

Setting up port: "COM4 @ "9600" baud
Status for device COM4: -----------------------
Baud: 9600 Parity: None Data Bits: 8 Stop Bits: 1
Timeout: OFF XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: ON
RTS circuit: ON
Opening port
Port opened
Checking for responce from modem
Waiting 5 seconds for expected result
Now: 1580560507, Timeout at: 1580560512
Received:
Now: 1580560508, Timeout at: 1580560512 
Received: 
Now: 1580560508, Timeout at: 1580560512 
Received: 
Now: 1580560508, Timeout at: 1580560512 
Received: 
Now: 1580560508, Timeout at: 1580560512 
Received: 
Now: 1580560508, Timeout at: 1580560512 
Received: 
Now: 1580560509, Timeout at: 1580560512 
Received: 
Now: 1580560509, Timeout at: 1580560512 
Received: 
Now: 1580560509, Timeout at: 1580560512 
Received: 
Now: 1580560509, Timeout at: 1580560512 
Received: 
Now: 1580560509, Timeout at: 1580560512 
Received: 
Now: 1580560510, Timeout at: 1580560512 
Received: 
Now: 1580560510, Timeout at: 1580560512 
Received: 
Now: 1580560510, Timeout at: 1580560512 
Received: 
Now: 1580560510, Timeout at: 1580560512 
Received: 
Now: 1580560510, Timeout at: 1580560512 
Received: 
Now: 1580560511, Timeout at: 1580560512 
Received: 
Now: 1580560511, Timeout at: 1580560512 
Received: 
Now: 1580560511, Timeout at: 1580560512 
Received: 
Now: 1580560511, Timeout at: 1580560512 
Received: 
Now: 1580560511, Timeout at: 1580560512 
Received: 
**Timed out**
**Fatal error: Uncaught Exception: Did not receive responce from modem in C:\xampp\htdocs\sms_test\text1.php:77 **
Stack trace: 
#0 C:\xampp\htdocs\sms_test\text1.php(13): gsm_send_sms->init() 
#1 {main} thrown in C:\xampp\htdocs\sms_test\text1.php on line 77
1
Have you tried connecting to it via any other PC software (some form of terminal emulator).Nigel Ren
@NigelRen yes, i've tried using AT Tester and it successfully sent a message.Jc John
All I can suggest is to use AT Tester and use something to check the port settings to see if there are any differences to how you are configuring the port.Nigel Ren
@Nigel. is there any mistakes in the code or its all in port config?.Jc John
Could you please post a log result with different messages in different lines? It is currently not readable. Furthermore, it is not clear the modem model you are using: the link you provided brings to a google image search page. Finally: responce -> response.Roberto Caboni

1 Answers

0
votes

The root cause of your exception, that is a custom generated one, is not in the AT commands sequence you use in the attempt to send an SMS. I can confirm that it is correct, and it is also confirmed by your tests with AT tester:

/* Set SMS text mode */
AT+CMGF=1\r
OK

/* Send SMS */
AT+CMGS="---destination Number---"\r
> SMS Contents
CTRL+Z

+CGMS: <N>
OK

Just one preliminary step is missing: the one in which you set Service Center address through AT+CSCA command, but since you are able to send message with your AT tester I assume it is already set.


Let's analyze your log. The exception occurs because no response has been received after the very first command you send: AT. You wait up to 5 seconds for its response, that usually comes immediately (< 100ms). Since you don't receive it, it clearly means that there are some communication problems.

Let's review your communication parameters, directly from the attached debug log:

Setting up port: "COM4 @ "9600" baud
Status for device COM4: -----------------------
Baud: 9600 Parity: None Data Bits: 8 Stop Bits: 1
Timeout: OFF
XON/XOFF: OFF
CTS handshaking: OFF
DSR handshaking: OFF
DSR sensitivity: OFF
DTR circuit: ON
RTS circuit: ON

I can suggest the following check list:

  1. Is your device correctly powered on?
  2. Make sure you are using the correct COM port
  3. Make sure to disconnect the AT tester and any other COM terminal before launching your PHP script
  4. Check your baudrate. Usually default baudrate for cellular modems is 115200 (if autobauding is not enabled), so I would start trying this change.
  5. '8n1' (data bits-parity-stop bits) is usually the default setting, so it is probably correct. We can say the same for all other settings. Before changing them, as a last resort, check the modem's current settings through your AT tester by issuing AT+IPR? (to query current baudrate), AT+ICF? (to query character framing format, e.g. 8n1) and AT&V (to query current profile status, including flow control settings).