您的位置:首页 > 编程语言 > C语言/C++

sim900芯片—GPRS使用C语言接电话和收短信应用程序

2016-08-10 20:29 447 查看
————————————————————————————————————————————— 
主机操作系统:Centos 6.7
交叉编译器环境:arm-linux-gcc-4.5.4 
开发板平台: FL2440 
Linux内核版本: linux-3.0 
开发模块: SIM900 GPRS 
邮箱:leiyuxing205@gmail.com 
—————————————————————————————————————————————




开发提醒:在开发gprs模块打电话发短信之前需满足开发板能正常加载linux内核及文件系统,并且开发板的串口已经使能,同时需准备一张开通gprs流量的sim卡。

一:GPRS介绍 

GSM模块,是将GSM射频芯片、基带处理芯片、存储器、功放器件等集成在一块线路板上,具有独立的操作系统、GSM射频处理、基带处理并提供标准接口的功能模块。GSM模块根据其提供的数据传输速率又可以分为GPRS模块、EDGE模块和纯短信模块。短信模块只支持语音和短信服务。GPRS,可说是GSM的延续。它经常被描述成“2.5G”,也就是说这项技术位于第二代(2G)和第三代(3G)移动通讯技术之间。GPRS的传输速率从56K到114Kbps不等,理论速度最高达171k。相对于GSM的9.6kbps的访问速度而言,GPRS拥有更快的访问数据通信速度,GPRS技术还具有在任何时间、任何地点都能实现连接,永远在线、按流量计费等特点。EDGE技术进一步提升了数据传输的速率到384K-473K,被称为”2.75G”,数据传输速率更2倍于GPRS。目前,国内的GSM网络普遍具有GPRS通讯功能,移动和联通的网络都支持GPRS,EDGE在部分省市实现了网络覆盖。 
GPRS模块,是具有GPRS数据传输功能的GSM模块。GPRS模块就是一个精简版的手机,集成GSM通信的主要功能于一块电路板上,具有发送短消息、通话、数据传输等功能。GPRS模块相当于手机的核心部分,如果增加键盘和屏幕就是一个完整的手机。普通电脑或者单片机可以通过RS232串口与GPRS模块相连,通过AT指令控制GPRS模块实现各种基于GSM的通信功能。 
GPRS模块区别于传统的纯短信模块,两者都是GSM模块,但是短信模块只能收发短信和语音通讯,而GPRS模块还具有GPRS数据传输功能。”
二.接电话收短信代码如下

/********************************************************************************
*      Copyright:  (C) 2016  leiyuxing <leiyuxing205@gmail.com>
*                  All rights reserved.
*
*       Filename:  answer_call_receive_message.c
*    Description:  This file
*
*        Version:  1.0.0(2016年08月10日)
*         Author:  leiyuxing <leiyuxing205@gmail.com>
*      ChangeLog:  1, Release initial version on "2016年8月10日20时18分00秒"
*
********************************************************************************/

#include <stdio.h>         /*标准输入输出定义*/
#include <stdlib.h>        /*标准函数库定义*/
#include <unistd.h>        /*UNIX 标准函数定义*/
#include <sys/types.h>     /*基本系统数据类型定义*/
#include <sys/stat.h>      /*文件状态定义*/
#include <fcntl.h>         /*文件控制定义*/
#include <termios.h>       /*PPSIX 终端控制定义*/
#include <errno.h>         /*错误号定义*/
#include <string.h>        /*字符串操作的定义*/

void serial_init(int fd)<span style="white-space:pre">        </span>//串口初始化函数
{
struct termios options;
tcgetattr(fd, &options);//读取串口参数设置
options.c_cflag |= (CLOCAL | CREAD);//设置忽略modem控制线,打开接收者
options.c_cflag &= ~CRTSCTS;//设置CRTSCTS(不属于POSIX)启用RTS/CTS(硬件)流控制
options.c_cflag &= ~CSIZE;//设置数据位
options.c_cflag |= CS8;//设置8位数据位
options.c_cflag &= ~CSTOPB;//设置停止位 - 1位停止位
options.c_iflag |= IGNPAR;//设置忽略帧错误和奇偶校验错误
options.c_oflag  = 0;
options.c_lflag  = 0;

cfsetispeed(&options, B115200); //根据自己的波特率进行相应更改
cfsetospeed(&options, B115200);
tcsetattr(fd, TCSANOW, &options);//读取串口的参数设置并立即生效
}

int receive_call(int fd)<span style="white-space:pre">        </span>//接听电话函数
{
char buff[16];
char reply[16];
int nread;
int nwrite;
int choice;

getchar();

memset(buff,0,sizeof(buff));
strcpy(buff,"ata\r");
nwrite=write(fd,buff,strlen(buff));
printf("nwrite1=%d,%s\n",nwrite,buff);

memset(reply,0,sizeof(reply));
sleep(1);
nread=read(fd,reply,sizeof(reply));
printf("nread1=%d,%s\n",nread,reply);

printf("input 1 to ath.\n");<span style="white-space:pre">        </span>//接听结束挂电话
choice = getchar();
getchar();
switch(choice)
{
case '1':memset(buff,0,sizeof(buff));
strcpy(buff,"ath\r");
nwrite=write(fd,buff,strlen(buff));
printf("nwrite1=%d,%s\n",nwrite,buff);
break;
default : break;
}

}

int reject_call(int fd)  <span style="white-space:pre">       </span>//拒接电话函数
{
char buff[16];
char reply[16];
int nread;
int nwrite;

getchar();

memset(buff,0,sizeof(buff));
strcpy(buff,"ath\r");
nwrite=write(fd,buff,strlen(buff));
printf("nwrite=%d,%s\n",nwrite,buff);

memset(reply,0,sizeof(reply));
sleep(1);
nread=read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);

}

int main(int argc, char **argv)
{
int fd;
char buff[16];
char reply[16];
int choice;
int nread;
int nwrite;
char str[]="\n\nRING";      //定义AT指令测试来电时的响应,要和AT指令测试时一致
[cpp] view plain copy

fd=open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if(fd<0)
{
perror("Can't open the serial port!\n");
}
serial_init(fd);

memset(buff,0,16);

strcpy(buff,"at+cnmi=3,2");<span style="white-space:pre">     </span>//新短信息的提示接收
strcpy(buff,"at\r");
strcat(buff,"\r");
nwrite=write(fd,buff,strlen(buff));
printf("nwrite=%d,%s\n",nwrite,buff);

memset(reply,0,16);
sleep(1);
nread=read(fd,reply,sizeof(reply));
printf("nread=%d,%s\n",nread,reply);

while(1)
{
memset(reply,0,16);
sleep(1);
nread=read(fd,reply,sizeof(reply));
if(!(strncmp(reply,str,6)))  //比较两个字符串,若不相等则实现下列功能
{
printf("Ring is coming\n ");
printf("please enter you select: 1,answer the call 2,reject the call 3,receive message \n");

choice = getchar();
switch(choice)
{

case '1':answer_the_call(fd);
break;
case '2':reject_the_call(fd);
break;
case '3':receive_message(fd);
break;
default :break;
}
}
}
close(fd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息