2012-08-31 | #1 (permalink) |
论坛管理员
注册日期: 2009-06-30
帖子: 861
|
PHP编程:通过串口+GSM MODEM发送短信示例
<?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); ?>
__________________
让世界倾听我们的笛声 |
书签 |
当前查看此主题的会员: 1 (0 位会员和 1 位游客) | |
主题工具 | |
显示模式 | |
|
|
相似的主题 | ||||
主题 | 主题作者 | 版面 | 回复 | 最后发表 |
AT指令编程相关技术问题 | admin | 短信开发资料 | 0 | 2012-08-25 18:22 |
嵌入式GSM短信息接口的软硬件设计 | admin | 短信开发资料 | 0 | 2012-08-24 17:46 |
CDMA,GSM,WCDMA,TD-SCDMA,CDMA2000,3G的区别 | hek320 | 产品资料专区 | 0 | 2012-02-18 11:17 |
PCI GSM MODEM使用指南 | 波斯猫 | 产品资料专区 | 0 | 2009-07-06 10:55 |
GSM Modem常见问题 | s0z0y | 售后服务专区 | 0 | 2009-07-05 22:56 |