深圳市未来时代科技有限公司
标题:
[转]PHP通过串口+短信设备(GSM MODEM)实现短信发送事例
[打印本页]
作者:
admin
时间:
2014-9-11 11:21
标题:
[转]PHP通过串口+短信设备(GSM MODEM)实现短信发送事例
前几天老板拿来一个短信收发设备,让我用php做一个发短信的程序,哎,我可是菜鸟,什么都不懂,网上查了很多资料,终于找到了方法:php通过调用串口直接用 短信收发设备来发短信。
不过虽然源程序在手,但还是不能直接用,期间碰到很多很多问题,发中文短信啊,缺少dio库啊,最后还是解决了。我把代码贴出来,大家有需要的可以参考一 下:
注意几点:
1.必须要有php_dio.dll这个文件,原来我的php是5.2.1的,因为php在5.0的时候已经把dio给去掉了,所有不能用串口了, 说是放在pecl库里了,哎,网上找了半天,才找到php5.2.6和pecl5.2.6,把自己的版本也更新到5.2.6,这样就可以调用串口了
2.发中文遇乱码的问题,在测试的时候都是乱码,后来发现我的文件是utf-8的,改成gb2312就好了
以下是我的程序,我把一些不要的东西去掉了,但可以发短信,在xp+apache+php5.2.6测试成功!
1.<?php
header('Content-type: charset=gb2312');
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 COM3: baud=9600 data=8 stop=1 parity=n xon=on');
$fd = dio_open('COM3:', O_RDWR);//opens the file for both reading and writing.
if(!$fd)
{
die("打开串口COM3失败");
}
$ff = dio_stat($fd);
print_r($ff);
echo "GSMAT is start on COM3\n";
$smsc = "8613800571500";// 短信中心号码(我用的是杭州的,自己改)
$max_len = 280;// 短信最大长?0个汉字,Unicode表示需?80个字?
$invert_smsc = InvertNumbers($smsc);//683108501705F0
$len = 1; $s = chr(13);//'空格'
$msisdn = "86".'13757156000';(接收的号码)
$sms_text = '中文字符能用吗?';//短信内容
$pdu_text = hex2str(gb2unicode($sms_text));
$invert_msisdn = InvertNumbers($msisdn);
// 拆分发送超?0汉字的短?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);//Returns the number of bytes written to fd.
sleep(1);
$pdu_text1 = "0891" . $invert_smsc . $pdu_text1 . chr(26).chr(13);//26?13是回?
$l = strlen($pdu_text1);
$ll = @dio_write($fd,$pdu_text1);
} while ( $pdu_text != "" );
dio_close($fd);
?>
---------------------------------------------------------------------------------------------------------
PHP编程:通过串口+GSM MODEM发送短信示例
公司有发短信任务,!@#$%^&~。每月至少500条,还要发给不同的50个人,ft!刚好看到版主HQ的大作 《
[硬件 通讯]PHP在RS232串口通讯协议的应用演示[测试环境PHP5.1.4]》,周末写一个群发短信的小脚本,2.5小时发1000条,超额完成任务, 嘿嘿!贴上来凑个热闹。
<?php
/*-
* 版权没有,欢迎拷贝 (x) 2006 胖头鱼.
* 作者: pangty <[url=mailto:pangty@ta139.com]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);
?>
==================================
欢迎光临 深圳市未来时代科技有限公司 (http://inextera.com/)
Powered by Discuz! X3.1