PHP通过串口实现发送短信(2)

<?php // Requirement dio, use cmd install: pecl install dio set_time_limit(0); // Windows use COM1: $fd=dio_open('/dev/ttyS0', O_RDWR); if(!$fd) { die("打开串口ttyS0失败"); } // dio_tcsetattr() only Linux // Windows 使用 exec('mode COM1: baud=9600 data=8 stop=1 parity=n xon=on'); dio_tcsetattr($fd, array( 'baud' => 9600, 'bits' => 8, 'stop' => 1, 'parity' => 0 )); //$ff=dio_stat($fd); //print_r($ff); //echo "GSM AT is start on ttyS0\n"; //短信中心号码 $smsc = "8613800571500"; $invert_smsc = invertNumbers($smsc); // 转换短信中心号码 $inter = chr(13); // 回车字符 $ctrlz = chr(26); // ctrl+z // 发送信息 $text = '你好'; $send_to = '8613638197275'; $pdu_phone = hex2str(utf82unicode($text)); $pdu_phone = sprintf("%02X", strlen($pdu_phone)/2) . $pdu_phone; $pdu_phone = '11000D91' . invertNumbers($send_to) . '000800' . $pdu_phone; $atcmd = 'AT+CMGF=0' . $inter; @dio_write($fd, $atcmd); $atcmd = 'AT+CMGS=' . sprintf("%d", strlen($pdu_phone)/2) . $inter; @dio_write($fd, $atcmd); $pdu_addr = '0891' . invertNumbers($smsc); $pdu_all = $pdu_addr . $pdu_phone . $ctrlz . $inter; @dio_write($fd, $pdu_all); dio_close($fd); // 我的是utf-8编码 function utf82unicode($str) { return iconv("utf-8", "UCS-2BE", $str); } function hex2str($hexstring) { $str = ''; for($i = 0, $len = strlen($hexstring); $i < $len; $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; } ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1a2c9383932e8a1f303450a1c650269a.html