2015年06月26日 星期五 10:46
SIM900A是一款国内常见的GSM\GPRS模块,可以用来发送短信、打电话、连接GPRS网络等。淘宝上搜索SIM900A可以找到一大堆开发板,价格也有高有低。我主要用于发短信,所以就买了一块最简单的,价格在70块钱左右,看上去质量还不错。
我们可以使用USB-TTL转换器连接SIM900A模块,可选5V和3.3V电平,注意电源接口和RX、TX接口是分开的,下图供参考:
我是在一台台式电脑上进行测试的,系统是CentOS Linux,USB-TTL接口在电脑中表现为一个串口设备,设备名称是/dev/ttyUSB0。
在Linux下读写串口设备,其实很简单,不需要任何特殊的Library,我们只需要像正常文件或者Socket那样读写即可。在读写之前,需要进行简单的设置(波特率等参数)。
设置参数的命令如下:
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
其实我也不是很精确的指导上述命令到底干了些什么,我也是从网上找来的,经过我的测试,确实能用,所以就分享给大家。
我使用PHP语言编写了发送短信的测试程序,可以发送中英文短信,大家可以参考:
<?php
/*
We should execute the command below to set option of the serial device.
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
*/
$debug=true;
function debug($str) {
global $debug;
if($debug) echo "debug: $str\n";
}
function send_ctrl_z($fp) {
fwrite($fp,"\x1A");
}
function send_end($fp) {
send_ctrl_z($fp);
fread($fp,1024);
die();
}
function read_result($fp){
$crlf=fread($fp,2);
if($crlf!= "\r\n") {
debug("protocol error, should begin with \\r\\n.");
return false;
}
$line=fgets($fp,1024);
if(substr($line,-2) != "\r\n") {
debug("protocol error, should end with \\r\\n.");
return false;
}
$result=substr($line,0,-2);
debug("<== " . $result);
return $result;
}
function ucs2($str){
$ustr=iconv("utf8","ucs2",$str);
$hexstr=bin2hex($ustr);
$result="";
for($i=0;$i<strlen($hexstr);$i+=4){
$result .= $hexstr[$i+2] . $hexstr[$i+3] . $hexstr[$i] . $hexstr[$i+1];
}
return strtoupper($result);
}
function open_serial_port() {
$fp =fopen("/dev/ttyUSB0", "w+");
if( !$fp) {
debug("Can not open serial device.");
return false;
}
if(false === simple_cmd($fp,"ATE0&W")) {
return false;
}
if(false === simple_cmd($fp,"AT")) {
return false;
}
return $fp;
}
function simple_cmd($fp,$cmd){
debug("==> " . $cmd);
sleep(0.1);
if(strlen($cmd) == 0) return false;
if($cmd[strlen($cmd)-1] != '\r') {
$cmd.="\r";
}
$ncmd=strlen($cmd);
$nw=fwrite($fp, $cmd);
if($nw != $ncmd) {
debug("send command error: $cmd .");
return false;
}
sleep(0.1);
$result = read_result($fp);
return $result;
}
function send_sms($fp,$phone,$content){
debug("==> " . "send sms: $phone, $content");
sleep(0.1);
if( false === simple_cmd($fp,"AT+CMGF=1")){
return false;
}
if( false === simple_cmd($fp,"AT+CSCS=\"GSM\"")){
return false;
}
$cmd="AT+CMGS=\"$phone\"\r";
$ncmd=strlen($cmd);
$nw=fwrite($fp, $cmd);
if($nw != $ncmd) {
debug("send command error: $cmd .");
return false;
}
sleep(0.1);
$str=fread($fp,4);
if($str != "\r\n> ") {
debug("protocol error: should got \\r\\n> ");
return false;
}
$content.="\x1A";
fwrite($fp,$content);
if(false === read_result($fp)){
return false;
}
if(false === read_result($fp)){
return false;
}
return true;
}
function send_chinese_sms($fp,$phone,$content){
debug("==> " . "send sms: $phone, $content");
sleep(0.1);
if(false === simple_cmd($fp,"AT+CSMP=17,167,2,225")){
return false;
}
if(false === simple_cmd($fp,"AT+CSCS=\"UCS2\"")){
return false;
}
$phone=ucs2($phone);
$content=ucs2($content);
$cmd="AT+CMGS=\"$phone\"\r";
$ncmd=strlen($cmd);
$nw=fwrite($fp, $cmd);
if($nw != $ncmd) {
debug("send command error: $cmd .");
return false;
}
sleep(0.1);
$str=fread($fp,4);
if($str != "\r\n> ") {
debug("protocol error: should got \\r\\n> ");
return false;
}
$content.="\x1A";
fwrite($fp,$content);
if(false === read_result($fp)){
return false;
}
if(false === read_result($fp)){
return false;
}
return true;
}
$fp=open_serial_port();
//send_end($fp);
//simple_cmd($fp,"ATI");
send_sms($fp,"+8618601350000","Good Luck!");
send_chinese_sms($fp,"+8618601350000","好大的风!");
SIM900A的功能非常丰富,更多的信息可以参考官方的文档,链接如下:
http://wm.sim.com/product.aspx?id=1007
Zeuux © 2025
京ICP备05028076号