1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* ******************************************************************************** *
* *
* 嵌入式资源免费下载专区: http://www.2beanet.com *
* *
* *
* ******************************************************************************** */
// this is a test about GPRS
#include
#include
#include
#include // open() close()
#include // read() write()
#include // set baud rate
#include
#include
#include
#include
#define FUNC_RUN 0
#define FUNC_NOT_RUN 1
#define SIMPLE_TEST 1
#define READ_SIM_CARD_ID 2
#define MAKE_A_CALL 3
#define WAIT_A_CALL 4
#define SHORT_MESSAGE 5
#define FUNC_QUIT 6
#define SEND_SHORT_MESSAGE 1
#define READ_SHORT_MESSAGE 2
#define CONFIG_SHORT_MESSAGE_ENV 3
#define QUIT_SHORT_MESSAGE 4
#define DEVICE_TTYS "/dev/ttyS0"
#define MAX_LEN_OF_SHORT_MESSAGE 140
#define RECEIVE_BUF_WAIT_1S 1
#define RECEIVE_BUF_WAIT_2S 2
#define RECEIVE_BUF_WAIT_3S 3
#define RECEIVE_BUF_WAIT_4S 4
#define RECEIVE_BUF_WAIT_5S 5
//------------------------------------- read datas from GSM/GPRS ---------------------------
// succese return 1
// error return 0
int read_GSM_GPRS_datas(int fd, char *rcv_buf,int rcv_wait)
{
int retval;
fd_set rfds;
struct timeval tv;
int ret,pos;
tv.tv_sec = rcv_wait; // wait 2.5s
tv.tv_usec = 0;
pos = 0; // point to rceeive buf
while (1)
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
retval = select(fd+1 , &rfds, NULL, NULL, &tv);
if (retval == -1)
{
perror("select()");
break;
}
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 1 of 6
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
else if (retval)
{// pan duan shi fou hai you shu ju
ret = read(fd, rcv_buf+pos, 2048);
pos += ret;
if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n')
{
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
retval = select(fd+1 , &rfds, NULL, NULL, &tv);
if (!retval) break;// no datas, break
}
}
else
{
printf("No data\n");
break;
}
}
return 1;
} // end read_GSM_GPRS_datas
//------------------------------------- send cmd -------------------------------------------
// succese return 1
// error return 0
int send_GSM_GPRS_cmd(int fd, char *send_buf)
{
ssize_t ret;
ret = write(fd,send_buf,strlen(send_buf));
if (ret == -1)
{
printf ("write device %s error\n", DEVICE_TTYS);
return -1;
}
return 1;
} // end send_GSM_GPRS_cmd
//------------------------------------- send cmd and read back result ----------------------
void GSM_GPRS_send_cmd_read_result(int fd, char *send_buf, int rcv_wait)
{
char rcv_buf[2048];
if((send_buf==NULL) || (send_GSM_GPRS_cmd(fd,send_buf)))
{ // send success , then read
bzero(rcv_buf,sizeof(rcv_buf));
if (read_GSM_GPRS_datas(fd,rcv_buf,rcv_wait))
{
printf ("%s\n",rcv_buf);
}
else
{
printf ("read error\n");
}
}
else
{
printf("write error\n");
}
} // end GSM_GPRS_send_cmd_read_result
//------------------------------------- send cmd : "at" to GSM/GPRS MODEM ------------------
void GSM_simple_test(int fd)
{
char *send_buf="at\r";
GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);
} // end GSM_simple_test
//------------------------------------- send cmd : "at+ccid" to GSM/GPRS MODEM -------------
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 2 of 6
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
void GSM_read_sim_card_id(int fd)
{
char *send_buf="at+ccid\r";
GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);
} // end GSM_read_sim_card_id
//------------------------------------- send cmd : "atd;" to GSM/GPRS MODEM -------
//------------------------------------- finish call, send cmd: "ath" to GSM MODEM ---------
void GSM_call(int fd)
{
char send_buf[17];
char *send_cmd_ath = "ath\r";
int i;
// input telephone number
bzero(send_buf,sizeof(send_buf));
send_buf[0]='a';
send_buf[1]='t';
send_buf[2]='d';
send_buf[16] = '\0';
printf("please input telephone number:");
i = 3;
while (1)
{
send_buf[i]=getchar();
if (send_buf[i]=='\n') break;
i++;
}
send_buf[i]=';';
send_buf[i+1]='\r';
// end input telphone number
// send cmd
GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);
//quit call
printf("press ENTER for quit:");
getchar();
// send cmd
GSM_GPRS_send_cmd_read_result(fd,send_cmd_ath,RECEIVE_BUF_WAIT_1S);
} // end GSM_call
//------------------------------------- wait for GSM/GPRS call signal ----------------------
void GSM_wait_call(int fd)
{
char rcv_buf[2048];
char *send_cmd_ath = "ath\r";
int wait_RING;
wait_RING = 10;
while (wait_RING!=0)
{
bzero(rcv_buf,sizeof(rcv_buf));
if (read_GSM_GPRS_datas(fd,rcv_buf,RECEIVE_BUF_WAIT_1S))
{
printf ("%s\n",rcv_buf);
}
else
{
printf ("read error\n");
}
wait_RING--;
}
GSM_GPRS_send_cmd_read_result(fd,send_cmd_ath,RECEIVE_BUF_WAIT_1S);
printf("quit wait_call\n");
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 3 of 6
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
} // end GSM_wait_call
//------------------------------------- GSM/GPRS send short message ------------------------
void GSM_Send_Message(int fd)
{
char cmd_buf[23];
char short_message_buf[MAX_LEN_OF_SHORT_MESSAGE];
int i;
char rcv_buf;
bzero(cmd_buf,sizeof(cmd_buf));
bzero(short_message_buf,sizeof(short_message_buf));
printf ("send short message:\n");
cmd_buf[0]='a';
cmd_buf[1]='t';
cmd_buf[2]='+';
cmd_buf[3]='c';
cmd_buf[4]='m';
cmd_buf[5]='g';
cmd_buf[6]='s';
cmd_buf[7]='=';
cmd_buf[8]='"';
printf ("please input telephone number:");
i = 9;
while (1)
{
cmd_buf[i]=getchar();
if (cmd_buf[i]=='\n') break;
i++;
}
cmd_buf[i]='"';
cmd_buf[i+1]='\r';
cmd_buf[i+2]='\0';
// send cmd : at+cmgs="(telephone number)"
GSM_GPRS_send_cmd_read_result(fd,cmd_buf,RECEIVE_BUF_WAIT_1S);
// input short message
printf("please input short message:");
i = 0;
while(i < MAX_LEN_OF_SHORT_MESSAGE-2)
{
short_message_buf[i] = getchar();
if (short_message_buf[i]=='\n') break;
i++;
}
short_message_buf[i] = 0x1A;
short_message_buf[i+1] = '\r';
short_message_buf[i+2] = '\0';
// send short message
GSM_GPRS_send_cmd_read_result(fd, short_message_buf,RECEIVE_BUF_WAIT_4S);
printf("\nend send short message\n");
} // end GSM_Send_Message
//------------------------------------- GSM/GPRS read all short message --------------------
void GSM_Read_Message(int fd)
{
char *send_buf="at+cmgl=\"ALL\"\r";
char rcv_buf[2048];
GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_3S);
printf("end read all short message\n");
} // end GSM_Read_Message
//------------------------------------- GSM/GPRS Config short message env ------------------
void GSM_Conf_Message(int fd)
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 4 of 6
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
{
char *send_buf="at+cmgf=1\r";
char *send_center_buf="at+csca=\"+8613800250500\"\r";
GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);
// set short message center number
GSM_GPRS_send_cmd_read_result(fd,send_center_buf,RECEIVE_BUF_WAIT_1S);
printf("end config short message env\n");
} // end GSM_Conf_Message
//------------------------------------- GSM/GPRS short message -----------------------------
void GSM_short_mesg(int fd)
{
int flag_sm_run, flag_sm_select;
flag_sm_run = FUNC_RUN;
while (flag_sm_run == FUNC_RUN)
{
printf ("\n Select:\n");
printf ("1 : Send short message \n");
printf ("2 : Read all short message \n");
printf ("3 : Config short message env\n");
printf ("4 : quit\n");
printf (">");
scanf("%d",&flag_sm_select);
getchar();
// temp
// printf ("input select:%d\n",flag_sm_select);
// end temp
switch (flag_sm_select)
{
case SEND_SHORT_MESSAGE : { GSM_Send_Message(fd);
case READ_SHORT_MESSAGE : { GSM_Read_Message(fd);
case CONFIG_SHORT_MESSAGE_ENV : { GSM_Conf_Message(fd);
case QUIT_SHORT_MESSAGE : { flag_sm_run = FUNC_NOT_RUN
default :
{
printf("please input your select use 1 to 3\n");
}
}
}
printf ("\n");
} // end GSM_send_mesg
//------------------------------------- print ----------------------------------------------
void print_prompt(void)
{
printf ("Select what you want to do:\n");
printf ("1 : Simple Test\n");
printf ("2 : Read SIM CARD ID\n");
printf ("3 : Make A Call\n");
printf ("4 : Wait A Call\n");
printf ("5 : Short message\n");
printf ("6 : Quit\n");
printf (">");
} // end print_prompt
//------------------------------------- Control GSM/GPRS MODULE ----------------------------
void func_GSM(int fd)
{
int flag_func_run;
int flag_select_func;
ssize_t ret;
flag_func_run = FUNC_RUN;
while (flag_func_run == FUNC_RUN)
{
print_prompt(); // print select functions
scanf("%d",&flag_select_func); // user input select
getchar();
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 5 of 6
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
switch(flag_select_func)
{
case SIMPLE_TEST : {GSM_simple_test(fd); break;}
case READ_SIM_CARD_ID : {GSM_read_sim_card_id(fd); break;}
case MAKE_A_CALL : {GSM_call(fd); break;}
case WAIT_A_CALL : {GSM_wait_call(fd); break;}
case SHORT_MESSAGE : {GSM_short_mesg(fd); break;}
case FUNC_QUIT :
{
flag_func_run = FUNC_NOT_RUN;
printf("Quit GSM/GPRS function. byeb
break;
}
default :
{
printf("please input your select use 1 to 7\n");
}
}
}
}// end func_GPRS
//------------------------------------- init seriel port ----------------------------------
void init_ttyS(int fd)
{
struct termios options;
bzero(&options, sizeof(options)); // clear options
cfsetispeed(&options,B9600); // setup baud rate
cfsetospeed(&options,B9600);
options.c_cflag |= (CRTSCTS | CS8 | CLOCAL | CREAD);
options.c_iflag = IGNPAR;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
}//end init_ttyS
//------------------------------------- main -----------------------------------------------
int main(void)
{
int fd;
printf("\nGSM/GPRS TESTS\n\n");
// open seriel port
fd = open(DEVICE_TTYS, O_RDWR);
if (fd == -1)
{
printf("open device %s error\n",DEVICE_TTYS);
}
else
{
init_ttyS(fd); // init device
func_GSM(fd); // GSM/GPRS functions
// close ttyS0
if (close(fd)!=0) printf("close device %s error",DEVICE_TTYS);
}
return 0;
}// end main
Linux下通过串口收发短信源码.c 2010-3-26 20:53
Page 6 of 6