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

[size=3]公司有发短信任务,!@#$%^&~。每月至少500条,还要发给不同的50个人,ft!刚好看到版主HQ的大作《[url=http://bbs.chinaunix.net/viewthread.php?tid=771726][硬件 通讯]PHP在RS232串口通讯协议的应用演示[测试环境PHP5.1.4]》,周末写一个群发短信的小脚本,2.5小时发1000条,超额完成任务,嘿嘿!贴上来凑个热闹。[/size]
[color=Green]

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

?>

[/color]

地址:http://www.chinaunix.net/old_jh/27/802131.html

posted @ 2012-08-15 15:02  phpbin  阅读(672)  评论(0编辑  收藏  举报