您的位置:首页 > 其它

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

2016-08-02 20:45 369 查看
-----------------------------------------------------------------------

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