【问题标题】:Uncaught Exception: "Did not receive response from modem sending SMS using GSM Modem"未捕获的异常:“未收到来自使用 GSM 调制解调器发送 SMS 的调制解调器的响应”
【发布时间】:2020-05-17 21:30:42
【问题描述】:

我正在使用 GPRS MODEM 使用带有this device 的 php 发送短信。

我使用 AT Tester 尝试了我的设备,以确定我的设备是否运行良好并成功发送。

然后我尝试了这个使用 PHP 通过 GPRS 调制解调器发送短信的链接:https://www.sitepoint.com/community/t/send-sms-using-gsm-modem/28584

这是提供的代码:

<?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;
    }
}
?>

执行代码后,PHP 返回此错误:

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

【问题讨论】:

  • 您是否尝试通过任何其他 PC 软件(某种形式的终端仿真器)连接到它。
  • @NigelRen 是的,我尝试过使用 AT Tester,它成功发送了一条消息。
  • 我只能建议使用 AT Tester 并使用一些东西来检查端口设置,看看您配置端口的方式是否有任何差异。
  • @Nigel。代码中是否有任何错误或其全部在端口配置中?。
  • 能否请您在不同的行中发布包含不同消息的日志结果?目前不可读。此外,尚不清楚您使用的调制解调器型号:您提供的链接会指向谷歌图片搜索页面。最后:responce -> response.

标签: php gsm at-command modem


【解决方案1】:

您的异常(即自定义生成的异常)的根本原因不在您在尝试发送 SMS 时使用的 AT 命令序列中。我可以确认它是正确的,并且您使用 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

只缺少一个初步步骤:您通过AT+CSCA 命令设置服务中心地址,但由于您可以使用您的 AT 测试仪发送消息,我假设它已经设置。


让我们分析您的日志。发生异常是因为在您发送的第一个命令之后没有收到响应:AT。您最多等待 5 秒以等待它的响应,这通常会立即出现(分明就是沟通有问题。

让我们直接从附加的调试日志中查看您的通信参数:

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

我可以建议以下检查清单:

  1. 您的设备是否已正确开机?
  2. 确保您使用的是正确的 COM 端口
  3. 确保在启动 PHP 脚本之前断开 AT 测试器和任何其他 COM 终端的连接
  4. 检查您的波特率。通常蜂窝调制解调器的默认波特率是 115200(如果未启用自动波特率),所以我会开始尝试此更改。
  5. '8n1'(数据位-奇偶校验-停止位)通常是默认设置,因此它可能是正确的。我们可以对所有其他设置说同样的话。在更改它们之前,作为最后的手段,通过您的 AT 测试器检查调制解调器的当前设置,通过发出 AT+IPR?(查询当前波特率)、AT+ICF?(查询字符帧格式,例如 @987654328 @) 和AT&amp;V(用于查询当前配置文件状态,包括流量控制设置)。

【讨论】:

  • 感谢您的回答。此设备是否支持串行? google.com/…
  • 通常所有调制解调器都有一个串行/USB 接口(实际上您可以通过 AT 测试仪与之通信,不是吗?)无论如何,您的链接并没有特别指向任何设备。它只是一个谷歌搜索页面
  • @JcJohn 我的检查清单是否帮助您从 PHP 脚本成功发送 aT 命令?
  • @JcJohn 你能用 AT 测试仪发送它吗?只是为了验证其状态问题AT+CSCA?,并验证存储的号码(如果有)是您的运营商的正确服务中心号码。如果写错了,请发AT+CSCA=&lt;number&gt;写下正确的。
  • 我尝试使用 AT Tester 应用程序,它成功发送了一条消息,但是当我尝试上面的程序时。它没有。我正在检查 AT+Tester 的设置。我找不到任何差异。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多