登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

梦想之鹰的天空

天高任鸟飞......放飞....心情..........放飞.....梦想

 
 
 

日志

 
 

通过串口+GSM MODEM发送短信PHP示例  

2010-12-31 12:31:31|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

<?php
/*-
* 版权没有,欢迎拷贝 (x) 2006 胖头鱼.
* 作者: pangty <pangty@ta139.com>
*
* $Id: RS232.php,v 0.1 2006-7-29 05:28 PM Exp $
*
* 运行环境:Windows XP,PHP 5.1.4 (cli),MySQL
* php.ini
* extension=php_mysql.dll
* extension=php_dio.dll
* 硬件要求:熊猫 PM818A GSM/GPRS MODEM或其他任何GSM MODEM模块
* 相关知识:GSM AT指令集和PDU格式,去google吧
*
* mysql> use smsc;
* mysql> desc sm_submit;
* +----------+----------------------+------+-----+---------+----------------+
* | Field    | Type                 | Null | Key | Default | Extra          |
* +----------+----------------------+------+-----+---------+----------------+
* | smid     | int(11) unsigned     |      | PRI | NULL    | auto_increment |
* | OrgAddr  | varchar(11)          | YES  |     | NULL    |                |
* | DestAddr | varchar(11)          |      | PRI |         |                |
* | Time     | datetime             | YES  |     | NULL    |                |
* | USERDATA | mediumtext           | YES  |     | NULL    |                |
* | stat     | smallint(3) unsigned | YES  | MUL | NULL    |                |
* +----------+----------------------+------+-----+---------+----------------+
*
*/
function gb2unicode($str) {
        return iconv("gb2312", "UCS-2", $str);
}

function hex2str($hexstring) {
        $str = '';
        for($i=0; $i<strlen($hexstring); $i++){
                $str .= sprintf("%02X",ord(substr($hexstring,$i,1)));
        }
        return $str;
}

function InvertNumbers($msisdn) {
        $len = strlen($msisdn);
        if ( 0 != fmod($len, 2) ) {
                $msisdn .= "F";
                $len = $len + 1;
        }

        for ($i=0; $i<$len; $i+=2) {
                $t = $msisdn[$i];
                $msisdn[$i] = $msisdn[$i+1];
                $msisdn[$i+1] = $t;
        }
        return $msisdn;
}

/*---------------主程序开始---------------*/
set_time_limit(0);
$DEBUG = 0;

exec('mode COM1: baud=115200 data=8 stop=1 parity=n xon=on');

$fd = dio_open('COM1:', O_RDWR);
if(!$fd)
{
        die("打开串口COM1失败");
}

$ff = dio_stat($fd);
print_r($ff);

echo "GSMAT is start on COM1\n";

// 短信中心号码
$smsc = "8613800538500";
// 短信最大长度70个汉字,Unicode表示需要280个字节
$max_len = 280;
$invert_smsc = InvertNumbers($smsc);

$sql = mysql_connect("localhost", "pangty", "pangty");
mysql_select_db("smsc",$sql);

$query = "select * from sm_submit where stat=0";
$result = mysql_query($query, $sql);

$len = 1; $s = chr(13);

while ( $row = mysql_fetch_object($result) ) {
        $msisdn = "86". $row->DestAddr;
        $sms_text = $row->USERDATA;
       
        if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $row->DestAddr\n";
        if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $sms_text\n";
       
        $pdu_text = hex2str(gb2unicode($sms_text));
        $invert_msisdn = InvertNumbers($msisdn);
       
        // 拆分发送超过70汉字的短信(todo: 没有判断全英文的情况)
        do {
                $pdu_len = strlen($pdu_text);
                if ( $pdu_len > $max_len ) {
                        $pdu_text1 = substr($pdu_text, 0, $max_len);
                        $pdu_text = substr($pdu_text, $max_len, $pdu_len - $max_len);
                } else {
                        $pdu_text1 = $pdu_text;
                        $pdu_text = "";
                }
       
                $pdu_len1 = sprintf("%02X", strlen($pdu_text1)/2);
                $pdu_text1 = $pdu_len1 . $pdu_text1;
       
                $pdu_text1 = "11000D91" . $invert_msisdn ."000800" . $pdu_text1;
       
                $atcmd = "AT+CMGS=" . sprintf("%d", strlen($pdu_text1)/2) . chr(13);
                $l = strlen($atcmd);
                $ll = @dio_write($fd,$atcmd);
                while ($l != $ll) {
                        sleep(10);
                        $ll = @dio_write($fd,$atcmd);
                }
                if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $atcmd\n";

                do {
                        $data = dio_read($fd, $len);
                        echo $data;
                } while ( $data != $s );

                sleep(1);
       
                $pdu_text1 = "0891" . $invert_smsc . $pdu_text1 . chr(26).chr(13);
                $l = strlen($pdu_text1);
                $ll = @dio_write($fd,$pdu_text1);
                while ($l != $ll) {
                        sleep(10);
                        $ll = @dio_write($fd,$pdu_text1);
                }
                if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $pdu_text1\n";

                do {
                        $data = dio_read($fd, $len);
                        echo $data;
                } while ( $data != $s );
               
                sleep(7);

        } while ( $pdu_text != "" );
       
        mysql_query("update sm_submit set stat=1 where smid=$row->smid", $sql);
}
mysql_close($sql);

dio_close($fd);

?>

  评论这张
 
阅读(617)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018