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