sim900芯片—GPRS模块接电话应用程序

-----------------------------------------------------------------------
Cross compiler:arm-linux-gcc-4.5.4
Linux kernel version:linux-3.0
Development board:fl2440
Author:  Yuzhonghan <754270866@qq.com>

-----------------------------------------------------------------------

以下是接听电话的代码:

 <span style="white-space:pre">1 /*********************************************************************************</span>
  2  *      Copyright:  (C) 2016 Yuzhonghan<754270866@qq.com>
  3  *                  All rights reserved.
  4  *
  5  *       Filename:  answer.c
  6  *    Description:  This file 
  7  *                 
  8  *        Version:  1.0.0(08/02/2016)
  9  *         Author:  Yuzhonghan <754270866@qq.com>
 10  *      ChangeLog:  1, Release initial version on "08/02/2016 04:38:58 PM"
 11  *                 
 12  ********************************************************************************/
 13 
 14 #include <sys/types.h>
 15 #include <sys/stat.h>
 16 #include <fcntl.h>
 17 #include<stdio.h>
 18 #include <stdlib.h>
 19 #include <unistd.h>
 20 #include <string.h>
 21 #include<termios.h>
 22 
 23 
 24 void serial_init(int fd)<span style="white-space:pre">		</span>//串口初始化函数
 25 {
 26     struct termios options;
 27     tcgetattr(fd, &options);
 28     options.c_cflag |= (CLOCAL | CREAD);
 29     options.c_cflag &= ~CSIZE;
 30     options.c_cflag &= ~CRTSCTS;
 31     options.c_cflag |= CS8;
 32     options.c_cflag &= ~CSTOPB;
 33     options.c_iflag |= IGNPAR;
 34     options.c_oflag  = 0;
 35     options.c_lflag  = 0;
 36 
 37 
 38     cfsetispeed(&options, B115200);
 39     cfsetospeed(&options, B115200);
 40     tcsetattr(fd, TCSANOW, &options);
 41 }
 42 
 43 
 44 int receive_call(int fd)<span style="white-space:pre">		</span>//接听电话函数
 45 {
 46     char buff[16];
 47     char reply[16];
 48     int nread;
 49     int nwrite;
 50     int choice;
 51 
 52 
 53     getchar();
 54 
 55     memset(buff,0,sizeof(buff));
 56     strcpy(buff,"ata\r");
 57     nwrite=write(fd,buff,strlen(buff));
 58     printf("nwrite1=%d,%s\n",nwrite,buff);
 59 
 60     memset(reply,0,sizeof(reply));
 61     sleep(1);
 62     nread=read(fd,reply,sizeof(reply));
 63     printf("nread1=%d,%s\n",nread,reply);
 64 
 65     printf("input 1 to ath.\n");<span style="white-space:pre">		</span>//接听结束挂电话
 66     choice = getchar();
 67     getchar();
 68     switch(choice)
 69     {
 70         case '1':memset(buff,0,sizeof(buff));
 71                  strcpy(buff,"ath\r");
 72                  nwrite=write(fd,buff,strlen(buff));
 73                  printf("nwrite1=%d,%s\n",nwrite,buff);
 74                  break;
 75         default : break;
 76 
 77     }
 78 
 79 }
 80 
 81 int reject_call(int fd)  <span style="white-space:pre">		</span>//拒接电话函数
 82 {
 83     char buff[16];
 84     char reply[16];
 85     int nread;
 86     int nwrite;
 87 
 88     getchar();
 89 
 90     memset(buff,0,sizeof(buff));
 91     strcpy(buff,"ath\r");
 92     nwrite=write(fd,buff,strlen(buff));
 93     printf("nwrite=%d,%s\n",nwrite,buff);
 94 
 95     memset(reply,0,sizeof(reply));
 96     sleep(1);
 97     nread=read(fd,reply,sizeof(reply));
 98     printf("nread=%d,%s\n",nread,reply);
 99 
100 
101 }
102 
103 int main(int argc, char **argv)
104 {
105     int fd;
106     char buff[16];
107     char reply[16];
108     int choice;
109     int nread;
110     int nwrite;
111     char str[]="\n\nRING";      //定义AT指令测试来电时的响应,要和AT指令测试时一致
112 
113     fd=open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
114     if(fd<0)
115     {
116         perror("Can't open the serial port!\n");
117     }
118     serial_init(fd);
119 
120     memset(buff,0,16);
121     strcpy(buff,"at\r");
122     nwrite=write(fd,buff,strlen(buff));
123     printf("nwrite=%d,%s\n",nwrite,buff);
124 
125     memset(reply,0,16);
126     sleep(1);
127     nread=read(fd,reply,sizeof(reply));
128     printf("nread=%d,%s\n",nread,reply);
129 
130     while(1)
131     {
132         memset(reply,0,16);
133         sleep(1);
134         nread=read(fd,reply,sizeof(reply));
135         if(!(strncmp(reply,str,6)))  //比较两个字符串,若不相等则实现下列功能
136         {
137             printf("Ring is coming\n ");
138             printf("please enter you select: 1,receive 2,reject \n");
139 
140            choice = getchar();
141             switch(choice)
142             {
143                 case '1':receive_call(fd);
144                          break;
145                 case '2':reject_call(fd);
146                          break;
147                 default :break;
148             }
149         }
150     }
151     close(fd);
152     return 0;
153 
154 }

这就是接电话的C代码了 ,大致整体上和上一篇所说的发短信和拨打电话类似,可参考上一篇博客。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值