linux c语言使用sim900打电话发短信

===================================================

GPRS模块:sim900

开发板:fl2440

内核版本:linux3.0

交叉编译器:arm-Linux  4.5.4

=====================================================

   使用C语言编写的一个简单的控制sim900打电话和发送短信,下面是代码

[cpp]  view plain  copy
  1. #include <termios.h>  
  2.   2  #include <stdio.h>  
  3.   3  #include <stdlib.h>  
  4.   4  #include <unistd.h>  
  5.   5  #include <fcntl.h>  
  6.   6  #include <string.h>  
  7.   7  #include <sys/types.h>  
  8.   8  #include <sys/stat.h>  
  9.   9 struct message_info{  
  10.  10                 char cnnu[16];  
  11.  11                 char phnu[16];  
  12.  12                 char message[128];  
  13.  13         };  
  14.  14 struct pdu_info {  
  15.  15                 char cnswap[32];  
  16.  16                 char phswap[32];  
  17.  17         };  
  18.  18         void serial_init(int fd)  
  19.  19         {  
  20.  20                 struct termios options;  /**/  
  21.  21                 tcgetattr(fd, &options);  /*该函数与设备文件绑定*/  
  22.  22                 options.c_cflag |= ( CLOCAL | CREAD ); /*这两个保证程序不会成为终端的所有者,用于本地接受和发送*/  
  23.  23                 options.c_cflag &= ~CSIZE; /*字符大小屏蔽*/  
  24.  24                 options.c_cflag &= ~CRTSCTS;/*不使用数据控制流,RTS/CTS流控制  rts ctc 是一种通信协议*/  
  25.  25                 options.c_cflag |= CS8;  /* 把数据设置为8位*/  
  26.  26                 options.c_cflag &= ~CSTOPB; /*空格校验*/  
  27.  27                 options.c_iflag |= IGNPAR; /*忽略奇偶错字符*/  
  28.  28                 options.c_oflag = 0;  
  29.  29                 options.c_lflag = 0;  
  30.  30                 cfsetispeed(&options, B115200); /*设置端口的输入波特率*/  
  31.  31                 cfsetospeed(&options, B115200);  /*设置端口的输出波特率*/  
  32.  32                 tcsetattr(fd,TCSANOW,&options); /*激活配置,使其生效,第二个参数为 更改立即生效*/  
  33.  33         }  
  34. void swap(char number[],char swap[])  
  35.  35         {  
  36.  36                 char ch1[] = "86";  
  37.  37                 char tmp[16];  
  38.  38                 int i;  
  39.  39         memset(swap,0,32);  
  40.  40                 memset(tmp,0,16);  
  41.  41                 strcpy(swap,number);  
  42.  42                 strcat(swap,"f");  
  43.  43                 strcat(ch1,swap);  
  44.  44                 strcpy(swap,ch1);  
  45.  45   
  46.  46         for(i = 0;i <= strlen(swap) - 1;i += 2){  
  47.  47                         tmp[i + 1] = swap[i];  
  48.  48                         tmp[i] = swap[i + 1];  
  49.  49                 }  
  50.  50                 strcpy(swap,tmp);  
  51.  51         }  
  52.  52 int send(int fd,char *cmgf,char *cmgs,char *message)  
  53.  53         {  
  54.  54                 int nread,nwrite;  
  55.  55                 char buff[128];  
  56.  56                 char reply[128];  
  57.  57         memset(buff,0,sizeof(buff));  
  58.  58                 strcpy(buff,"at\r");  
  59.  59                 nwrite = write(fd,buff,strlen(buff));  
  60.  60                 printf("nwrite=%d,%s\n",nwrite,buff);  
  61.  61         memset(reply,0,sizeof(reply));  
  62.  62                 sleep(1);  
  63.  63                 nread = read(fd,reply,sizeof(reply));  
  64.  64                 printf("nread=%d,%s\n",nread,reply);  
  65. 65         memset(buff,0,sizeof(buff));  
  66.  66                 strcpy(buff,"AT+CMGF=");  
  67.  67                 strcat(buff,cmgf);  
  68.  68                 strcat(buff,"\r");  
  69.  69                 nwrite = write(fd,buff,strlen(buff));  
  70.  70                 printf("nwrite=%d,%s\n",nwrite,buff);  
  71.  71         memset(reply,0,sizeof(reply));  
  72.  72                 sleep(1);  
  73.  73                 nread = read(fd,reply,sizeof(reply));  
  74.  74                 printf("nread=%d,%s\n",nread,reply);  
  75.  75         memset(buff,0,sizeof(buff));  
  76.  76                 strcpy(buff,"AT+CMGS=\"");  
  77.  77                 strcat(buff,cmgs);  
  78.  78   
  79.  79                 strcat(buff,"\"");  
  80.  80                 strcat(buff,"\r");  
  81.  81                 nwrite = write(fd,buff,strlen(buff));  
  82.  82                 printf("nwrite=%d,%s\n",nwrite,buff);  
  83.  83         memset(reply,0,sizeof(reply));  
  84.  84                 sleep(1);  
  85.  85                 nread = read(fd,reply,sizeof(reply));  
  86.  86                 printf("nread=%d,%s\n",nread,reply);  
  87.  87         memset(buff,0,sizeof(buff));  
  88.  88                 strcpy(buff,message);  
  89.  89                 nwrite = write(fd,buff,strlen(buff));  
  90.  90                 printf("nwrite=%d,%s\n",nwrite,buff);  
  91.  91         memset(reply,0,sizeof(reply));  
  92.  92                 sleep(1);  
  93.  93                 nread = read(fd,reply,sizeof(reply));  
  94.  94                 printf("nread=%d,%s\n",nread,reply);  
  95.  95         }  
  96.  96 int send_en_message(int fd,struct message_info info)  
  97.  97         {  
  98.  98                 getchar();  /*把缓冲区的回车吃掉*/  
  99.  99                 char cmgf[] = "1";  
  100. 100                 int conter = 0;  
  101. 101                 char cmgs[16] = {'\0'};  
  102. 102         printf("enter recever phnumber :\n");  
  103. 103                 gets(info.phnu);  
  104. 104                 while(strlen(info.phnu) != 11){  
  105. 105                         if(conter >= 3){  
  106. 106                                 printf("conter out !\n");  
  107. 107                                 return -1;  
  108. 108                         }  
  109. 109                         printf("number shuld be --11-- bits ! enter agin :\n");  
  110. 110                         gets(info.phnu);  
  111. 111                         conter ++;  
  112. 112                 }  
  113. 113         printf("enter you message !\n");  
  114. 114                 gets(info.message);  
  115. 115                 strcat(info.message,"\x1a");  
  116. 116                 strcat(cmgs,info.phnu);  
  117. 117         send(fd,cmgf,cmgs,info.message);  
  118. 118         }  
  119. 119   
  120. 120 int  call_telephone(int fd)  
  121. 121 {  
  122. 122     getchar();  
  123. 123     int count=0;  
  124. 124     int num_lenth;  
  125. 125     char call_num[128];  
  126. 126     int nwrite,nread;  
  127. 127     int * a;  
  128. 128     int  h;  
  129. 129     char buff[128];  
  130. 130   
  131. 131   
  132. 132     printf("enter your centre phnumber :\n");  
  133. 133     gets(call_num);  
  134. 134      num_lenth=strlen(call_num);  
  135. 135     while(num_lenth!=11)  
  136. 136   {  
  137. 137           if(count>3)  
  138. 138           {  
  139. 139               printf("count out !!\n");  
  140. 140               return -1;  
  141. 141           }  
  142. 142   
  143. 143            count++;  
  144. 144            printf("please input -- 11--bits!\n");  
  145. 145            gets(call_num);  
  146. 146            num_lenth=strlen(call_num);  
  147. 147   }  
  148. 148   memset(buff,0,sizeof(buff));  
  149. 149   strcpy(buff,"atd");  
  150. 150   printf("buff=%s\n",buff);  
  151. 151   strcat(buff,call_num);  
  152. 152   strcat(buff,";\r");  
  153. 153   nwrite=write(fd,buff,strlen(buff));  
  154. 154   sleep(3);  
  155. 155   memset(buff,0,sizeof(buff));  
  156. 156   nread=read(fd,buff,sizeof(buff));  
  157. 157   printf("buff=%s\n",buff);  
  158. 158   printf("++++++++++++++++++++++\n");  
  159. 159   
  160. 160   printf("press 2 hung up\n");  
  161. 161   printf("++++++++++++++++++++++\n");  
  162. 162   sleep(1);  
  163. 163   scanf("%d",&h);  
  164. 164   printf("h=%d",h) ;  
  165. 165   while(h!=2)  
  166. 166   {  
  167. 167     printf("please input currect num\n");  
  168. 168     scanf("%d",&h);  
  169. 169   }  
  170. 170   memset(buff,0,sizeof(buff));  
  171. 171   strcpy(buff,"ATH\r");  
  172. 172   write(fd,buff,sizeof(buff));  
  173. 173   printf("has hunged up");  
  174. 174   return 0;  
  175. 175 /*  while(1) 
  176. 176   { 
  177. 177      
  178. 178     memset(buff,0,sizeof(buff));  
  179. 179     nread=read(fd,buff,sizeof(buff)); 
  180. 180     a=strstr(buff,"BUSY"); 
  181. 181     return 0; 
  182. 182   } 
  183. 183   */  
  184. 184   
  185. 185   
  186. 186 }  
  187. 187   
  188.   
  189. 188 int main()  
  190. 189         {  
  191. 190                 int fd;  
  192. 191                 char choice;  
  193. 192                 struct message_info info;  
  194. 193                 fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);  
  195. 194                 if (-1 == fd){  
  196. 195                         perror("Can't Open Serial Port");  
  197. 196                 }  
  198. 197                serial_init(fd);  
  199. 198                 printf("\n============================================\n");  
  200. 199                 printf("\tthis is a gprs test program !\n");  
  201. 200                 printf("============================================\n");  
  202. 201                 printf("enter your selete :\n");  
  203. 202                 printf("1.send english message.\n");  
  204. 203                 printf("2.call telephone\n");  
  205. 204                 printf("3.exit.\n");  
  206. 205                 choice = getchar();  
  207. 206                 switch(choice)  
  208. 207                  {  
  209. 208                         case '1': send_en_message(fd,info);  
  210. 209                                 break;  
  211. 210                         case '2': call_telephone(fd);  
  212. 211                                    break;  
  213. 212                         case '3'break;  
  214. 213                                 default : break;  
  215. 214                 }  
  216. 215                 close(fd);  
  217. 216                 return 0;  
  218. 217 }  

猜你喜欢

转载自blog.csdn.net/u012654756/article/details/57128752