当前位置:   article > 正文

sim900芯片—GPRS打电话和发短信应用程序_sim900 at+email

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

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

一:AT命令
    使用sim900发短信打电话之前,必须了解at命令怎么使用。下面是一些常用的命令
AT+CMGC   Send an SMS command(发出一条短消息命令)    
AT+CMGD   Delete SMS message(删除 SIM 卡内存的短消息)    
AT+CMGF   Select SMS message formate (选择短消息信息收发格式: 0-PDU;1-文本)
AT+CMGL   List SMS message from preferred store(列出 SIM 卡中的短消息
AT+CMGR   Read SMS message(读短消息)    
AT+CMGS   Send SMS message(发送短消息)    
AT+CMGW   Write SMS message to memory(向 SIM 内存中写入待发的短消息)
AT+CMSS   Send SMS message from storage(从 SIN |M 内存中发送短消息)
AT+CNMI   New SMS message indications(显示新收到的短消息)    
AT+CPMS   Preferred SMS message storage(选择短消息内存)    
AT+CSCA   SMS service center address(短消息中心地址)    
AT+CSCB   Select cell broadcast messages(选择蜂窝广播消息)   
AT+CSMP   Set SMS text mode parameters(设置短消息文本模式参数)
AT+CSMS   Select Message Service(选择短消息服务)
AT+CNMI=2,1,0,0,0          //设置收到新短信存于SIM卡中并发CMTI通知
+CMTI:”SM”,1           //收到了短信,自动弹出,其中1表示存在SIM中的序号
AT+CMGR=1             //读取短信,其中1要与上面序号对应
AT+CMGD=1             //删除短信,其中1为短信序号
ATD188********;       //拨打电话,后面一定要加‘;’
ATA //接听电话
ATH //挂断电话


在这博主所使用串口连接板子输入  microcom -s115200 /dev/ttyS1     这里我链接的串口是dev目录下的ttyS1,串口波特率是115200。

然后可以使用 AT指令测试。

PS:(这里可以根据自己的串口信息进行更改。)


以下是发送短信和拨打电话的C代码:


  1. 1 /*********************************************************************************
  2. 2 * Copyright: (C) 2016 Yuzhonghan<754270866@qq.com>
  3. 3 * All rights reserved.
  4. 4 *
  5. 5 * Filename: gprs.c
  6. 6 * Description: This file
  7. 7 *
  8. 8 * Version: 1.0.0(07/30/2016)
  9. 9 * Author: Yuzhonghan <754270866@qq.com>
  10. 10 * ChangeLog: 1, Release initial version on "07/30/2016 05:23:56 PM"
  11. 11 *
  12. 12 ********************************************************************************/
  13. 13 #include <sys/types.h>
  14. 14 #include <sys/stat.h>
  15. 15 #include <fcntl.h>
  16. 16 #include<stdio.h>
  17. 17 #include <stdlib.h>
  18. 18 #include <unistd.h>
  19. 19 #include <string.h>
  20. 20 #include<termios.h>
  21. 21
  22. 22
  23. 23 #define len_num 16
  24. 24 #define len_mes 128
  25. 25
  26. 26 struct message_info
  27. 27 {
  28. 28 char cnu[len_num];
  29. 29 char pnu[len_num];
  30. 30 char message[len_mes];
  31. 31 };
  32. 32
  33. 33 void serial_init(int fd) //初始化串口
  34. 34 {
  35. 35 struct termios options;
  36. 36 tcgetattr(fd, &options);
  37. 37 options.c_cflag |= (CLOCAL | CREAD);
  38. 38 options.c_cflag &= ~CSIZE;
  39. 39 options.c_cflag &= ~CRTSCTS;
  40. 40 options.c_cflag |= CS8;
  41. 41 options.c_cflag &= ~CSTOPB;
  42. 42 options.c_iflag |= IGNPAR;
  43. 43 options.c_oflag = 0;
  44. 44 options.c_lflag = 0;
  45. 45
  46. 46
  47. 47 cfsetispeed(&options, B115200); //根据自己的波特率进行相应更改
  48. 48 cfsetospeed(&options, B115200);
  49. 49 tcsetattr(fd, TCSANOW, &options);
  50. 50 }
  51. 51
  52. 52
  53. 53 int send(int fd,char *cmgf,char *cmgs,char *message)
  54. 54 {
  55. 55 int nread;
  56. 56 int nwrite;
  57. 57 char buff[len_mes];
  58. 58 char reply[len_mes];
  59. 59
  60. 60
  61. 61 memset(buff,0,len_mes);
  62. 62 strcpy(buff,"at\r");
  63. 63 nwrite=write(fd,buff,strlen(buff));
  64. 64 printf("nwrite=%d,%s\n",nwrite,buff);
  65. 65
  66. 66
  67. 67 memset(reply,0,len_mes);
  68. 68 sleep(1);
  69. 69 nread=read(fd,reply,sizeof(reply));
  70. 70 printf("nread=%d,%s\n",nread,reply);
  71. 71
  72. 72
  73. 73 memset(buff,0,len_mes);
  74. 74 strcpy(buff,"AT+CMGF=");
  75. 75 strcat(buff,cmgf);
  76. 76 strcat(buff,"\r");
  77. 77 nwrite=write(fd,buff,strlen(buff));
  78. 78 printf("nwrite=%d,%s\n",nwrite,buff);
  79. 79
  80. 80 memset(reply,0,len_mes);
  81. 81 sleep(1);
  82. 82 nread=read(fd,reply,sizeof(reply));
  83. 83 printf("nread=%d,%s\n",nread,reply);
  84. 84
  85. 85 memset(buff,0,len_mes);
  86. 86 strcpy(buff,"AT+CMGS=");
  87. 87 strcat(buff,cmgs);
  88. 88 strcat(buff,"\r");
  89. 89 nwrite=write(fd,buff,strlen(buff));
  90. 90 printf("nwrite=%d,%s\n",nwrite,buff);
  91. 91
  92. 92 memset(reply,0,len_mes);
  93. 93 sleep(1);
  94. 94 nread=read(fd,reply,sizeof(reply));
  95. 95 printf("nread=%d,%s\n",nread,reply);
  96. 96
  97. 97 memset(buff,0,len_mes);
  98. 98 strcpy(buff,message);
  99. 99 nwrite=write(fd,buff,strlen(buff));
  100. 100 printf("nwrite=%d,%s\n",nwrite,buff);
  101. 101
  102. 102 memset(reply,0,len_mes);
  103. 103 sleep(1);
  104. 104 nread=read(fd,reply,sizeof(reply));
  105. 105 printf("nread=%d,%s\n",nread,reply);
  106. 106
  107. 107 }
  108. 108
  109. 109 int send_message(int fd,struct message_info info)
  110. 110 {
  111. 111 getchar();
  112. 112 char cmgf[]="1";
  113. 113 int conter=0;
  114. 114 char cmgs[16]={'\0'};
  115. 115
  116. 116 printf("Enter you number,please: \n");
  117. 117 fgets(info.pnu,(len_num-1),stdin);
  118. 118 while(strlen(info.pnu)!=12)
  119. 119 {
  120. 120 if(conter>=3)
  121. 121 {
  122. 122 printf("conter out!\n");
  123. 123 return -1;
  124. 124 }
  125. 125 printf("You should enter a 11 digit number! Again: \n");
  126. 126 fgets(info.pnu,(len_num-1),stdin);
  127. 127 conter++;
  128. 128 }
  129. 129
  130. 130 printf("Please enter you want to send a message!\n");
  131. 131 fgets(info.message,(len_mes),stdin);
  132. 132 strcat(info.message,"\x1a");
  133. 133 strcat(cmgs,"\"");
  134. 134 strcat(cmgs,info.pnu);
  135. 135
  136. 136 cmgs[12] =(char) {'\"'};
  137. 137
  138. 138 send(fd,cmgf,cmgs,info.message);
  139. 139
  140. 140 }
  141. 141
  142. 142
  143. 143 int call(int fd,char *atd)
  144. 144 {
  145. 145 int nread;
  146. 146 int nwrite;
  147. 147 char buff[len_mes];
  148. 148 char reply[len_mes];
  149. 149
  150. 150 memset(buff,0,len_mes);
  151. 151 strcpy(buff,"at\r");
  152. 152 nwrite=write(fd,buff,strlen(buff));
  153. 153 printf("nwrite=%d,%s\n",nwrite,buff);
  154. 154
  155. 155 memset(reply,0,len_mes);
  156. 156 sleep(1);
  157. 157 nread=read(fd,reply,sizeof(reply));
  158. 158 printf("nread=%d,%s\n",nread,reply);
  159. 159
  160. 160
  161. 161 memset(buff,0,len_mes);
  162. 162 strcpy(buff,"atd");
  163. 163 strcat(buff,atd);
  164. 164 strcat(buff,"\r");
  165. 165 nwrite=write(fd,buff,strlen(buff));
  166. 166 printf("nwrite=%d,%s\n",nwrite,buff);
  167. 167
  168. 168 memset(reply,0,len_mes);
  169. 169 sleep(1);
  170. 170 nread=read(fd,reply,sizeof(reply));
  171. 171 printf("nread=%d,%s\n",nread,reply);
  172. 172
  173. 173
  174. 174 }
  175. 175
  176. 176 int call_phone(int fd,struct message_info info)
  177. 177 {
  178. 178 getchar();
  179. 179 int conter=0;
  180. 180 char atd[16]={'\0'};
  181. 181
  182. 182 printf("Please enter the number you need to dial: \n");
  183. 183 fgets(info.pnu,(len_num-1),stdin);
  184. 184 while(strlen(info.pnu)!=12)
  185. 185 {
  186. 186 if(conter>=3)
  187. 187 {
  188. 188 printf("conter out!\n");
  189. 189 return -1;
  190. 190
  191. 191 }
  192. 192 printf("You should enter a 11 digit number! Again: \n");
  193. 193 fgets(info.pnu,(len_num-1),stdin);
  194. 194 conter++;
  195. 195 }
  196. 196 strcat(atd,info.pnu);
  197. 197 atd[11]=(char){';'};
  198. 198
  199. 199 call(fd,atd);
  200. 200 }
  201. 201
  202. 202
  203. 203
  204. 204 int main(int argc, char **argv)
  205. 205 {
  206. 206 int fd;
  207. 207 struct message_info info;
  208. 208 char choice;
  209. 209
  210. 210 fd=open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
  211. 211 if(fd<0)
  212. 212 {
  213. 213 perror("Can't open the serial port!\n");
  214. 214 }
  215. 215
  216. 216 serial_init(fd);
  217. 217 printf("\n------------------------------------\n");
  218. 218 printf("Welcome to GPRS!\n");
  219. 219 printf("\n------------------------------------\n");
  220. 220 printf("Please input you select:\n");
  221. 221 printf("1.Send a Enlish message.\n ");
  222. 222 printf("2.Call phone.\n");
  223. 223 printf("3.Thinking fou using\n");
  224. 224 choice = getchar();
  225. 225 switch(choice)
  226. 226 {
  227. 227 case '1':send_message(fd,info);
  228. 228 break;
  229. 229 case '2':call_phone(fd,info);
  230. 230 break;
  231. 231 case '3':break;
  232. 232 default : break;
  233. 233 }
  234. 234
  235. 235
  236. 236
  237. 237
  238. 238 close(fd);
  239. 239 return 0;
  240. 240 }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/361038
推荐阅读
相关标签
  

闽ICP备14008679号