GPRS驱动

  1. gsm_gprs.c
  2. // this is a test about GPRS  
  3.   
  4. #include <stdio.h>  
  5. #include <string.h>  
  6. #include <stdlib.h>  
  7.   
  8. #include <fcntl.h>    // open() close()   
  9. #include <unistd.h>   // read() write()  
  10.   
  11. #include <termios.h>  // set baud rate  
  12.   
  13. #include <fcntl.h>  
  14. #include <sys/select.h>  
  15. #include <sys/time.h>  
  16. #include <sys/types.h>  
  17.   
  18. #define FUNC_RUN        0  
  19. #define FUNC_NOT_RUN        1  
  20.   
  21. #define SIMPLE_TEST         1  
  22. #define READ_SIM_CARD_ID    2  
  23. #define MAKE_A_CALL         3  
  24. #define WAIT_A_CALL     4  
  25. #define SHORT_MESSAGE       5  
  26. #define FUNC_QUIT       6  
  27.   
  28. #define SEND_SHORT_MESSAGE      1  
  29. #define READ_SHORT_MESSAGE      2  
  30. #define CONFIG_SHORT_MESSAGE_ENV        3  
  31. #define QUIT_SHORT_MESSAGE      4  
  32.   
  33. #define DEVICE_TTYS "/dev/ttyS1"  
  34. #define MAX_LEN_OF_SHORT_MESSAGE    140  
  35.   
  36.   
  37. #define RECEIVE_BUF_WAIT_1S 1  
  38. #define RECEIVE_BUF_WAIT_2S 2  
  39. #define RECEIVE_BUF_WAIT_3S 3  
  40. #define RECEIVE_BUF_WAIT_4S 4  
  41. #define RECEIVE_BUF_WAIT_5S 5  
  42.   
  43. //------------------------------------- read datas from GSM/GPRS --------------------------------------------  
  44. // succese return 1  
  45. // error   return 0  
  46. int read_GSM_GPRS_datas(int fd, char *rcv_buf,int rcv_wait)  
  47. {  
  48.     int retval;  
  49.     fd_set rfds;  
  50.     struct timeval tv;  
  51.     /* 
  52.     struct timeval{ 
  53.                 long tv_sec;   秒 
  54.                 long tv_usec; 微秒 
  55.                 } 
  56.     */  
  57.   
  58.     int ret,pos;  
  59.   
  60.     tv.tv_sec = rcv_wait;   // wait 2.5s  
  61.         tv.tv_usec = 0;  
  62.   
  63.     pos = 0; // point to rceeive buf  
  64.       
  65.     while (1)  
  66.     {  
  67.         FD_ZERO(&rfds);  //设置轮询的一些相关  
  68.                 FD_SET(fd, &rfds);  
  69.   
  70.         retval = select(fd+1 , &rfds, NULL, NULL, &tv);  
  71.   
  72.                 if (retval == -1)   
  73.         {  
  74.                         perror("select()");  
  75.                         break;  
  76.                 }  
  77.         else if (retval)   
  78.         {// pan duan shi fou hai you shu ju   
  79.                         ret = read(fd, rcv_buf+pos, 2048);  
  80.                         pos += ret;  
  81.                         if (rcv_buf[pos-2] == '\r' && rcv_buf[pos-1] == '\n')   //\r 表示回车的意思  
  82.             {  
  83.                                 FD_ZERO(&rfds);  
  84.                                 FD_SET(fd, &rfds);  
  85.   
  86.                                 retval = select(fd+1 , &rfds, NULL, NULL, &tv);  
  87.   
  88.                                 if (!retval) break;// no datas, break  
  89.                         }  
  90.                 }   
  91.         else   
  92.         {  
  93.                         printf("No data\n");  
  94.                         break;  
  95.                 }  
  96.     }  
  97.   
  98.     return 1;  
  99. // end read_GSM_GPRS_datas  
  100.   
  101.   
  102. //------------------------------------- send cmd ------------------------------------------------------------  
  103. // succese return 1  
  104. // error   return 0  
  105. int send_GSM_GPRS_cmd(int fd, char *send_buf)  
  106. {  
  107.     ssize_t ret;  
  108.       
  109.     ret = write(fd,send_buf,strlen(send_buf));  
  110.     if (ret == -1)  
  111.         {  
  112.                 printf ("write device %s error\n", DEVICE_TTYS);  
  113.                 return -1;  
  114.         }  
  115.   
  116.     return 1;  
  117. // end send_GSM_GPRS_cmd  
  118.   
  119. //------------------------------------- send cmd and read back result ---------------------------------------  
  120. void GSM_GPRS_send_cmd_read_result(int fd, char *send_buf, int rcv_wait)  
  121. {  
  122.         char rcv_buf[2048];  
  123.   
  124.     if((send_buf==NULL) || (send_GSM_GPRS_cmd(fd,send_buf)))  //判断send_buf是否为空或都发送过去的返来值是否为1  
  125.     {   // send success , then read  
  126.         bzero(rcv_buf,sizeof(rcv_buf));  
  127.         if (read_GSM_GPRS_datas(fd,rcv_buf,rcv_wait))  
  128.                 {  
  129.                         printf ("%s\n",rcv_buf);  
  130.                 }  
  131.                 else  
  132.                 {  
  133.                         printf ("read error\n");  
  134.                 }  
  135.   
  136.     }  
  137.     else  
  138.     {  
  139.         printf("write error\n");  
  140.     }  
  141. // end GSM_GPRS_send_cmd_read_result  
  142.   
  143. //------------------------------------- send cmd : "at" to GSM/GPRS MODEM -----------------------------------  
  144. void GSM_simple_test(int fd)  
  145. {  
  146.     char *send_buf="at\r";  
  147.       
  148.     GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);  
  149.   
  150. // end GSM_simple_test  
  151.   
  152. //------------------------------------- send cmd : "at+ccid" to GSM/GPRS MODEM ------------------------------  
  153. void GSM_read_sim_card_id(int fd)  
  154. {  
  155.         char *send_buf="at+ccid\r";  
  156.   
  157.     GSM_GPRS_send_cmd_read_result(fd,send_buf,RECEIVE_BUF_WAIT_1S);  
  158.   
  159. // end GSM_read_sim_card_id  
  160.   
  161. //------------------------------------- print ---------------------------------------------------------------  
  162. void print_prompt(void)  
  163. {  
  164.         printf ("Select what you want to do:\n");  
  165.         printf ("1 : Simple Test\n");  
  166.         printf ("2 : Read SIM CARD ID\n");  
  167.         printf ("3 : Make A Call\n");  
  168.         printf ("4 : Wait A Call\n");  
  169.         printf ("5 : Short message\n");  
  170.         printf ("6 : Quit\n");  
  171.         printf (">");  
  172. // end print_prompt  
  173.   
  174. //------------------------------------- Control GSM/GPRS MODULE ---------------------------------------------  
  175. void func_GSM(int fd)  
  176. {  
  177.         int flag_func_run;  
  178.         int flag_select_func;  
  179.         ssize_t ret;  
  180.   
  181.         flag_func_run = FUNC_RUN;  
  182.   
  183.         while (flag_func_run == FUNC_RUN)  
  184.         {  
  185.                 print_prompt();         // print select functions  
  186.                 scanf("%d",&flag_select_func);  // user input select  
  187.                 getchar();  
  188.   
  189.                 switch(flag_select_func)  
  190.                 {  
  191.                         case SIMPLE_TEST        : {GSM_simple_test(fd);         break;}  
  192.                         case READ_SIM_CARD_ID   : {GSM_read_sim_card_id(fd);    break;}  
  193.                         case MAKE_A_CALL        : {GSM_call(fd);                break;}  
  194.                         case WAIT_A_CALL        : {GSM_wait_call(fd);           break;}  
  195.                         case SHORT_MESSAGE      : {GSM_short_mesg(fd);          break;}  
  196.                         case FUNC_QUIT          :  
  197.                                                 {  
  198.                                                         flag_func_run = FUNC_NOT_RUN;  
  199.                                                         printf("Quit GSM/GPRS function.  byebye\n");  
  200.                                                         break;  
  201.                                                 }  
  202.                         default :  
  203.                         {  
  204.                                 printf("please input your select use 1 to 7\n");  
  205.                         }  
  206.                 }  
  207.   
  208.         }  
  209. }// end func_GPRS  
  210.   
  211. //------------------------------------- init seriel port  ---------------------------------------------------  
  212. void init_ttyS(int fd)  
  213. {  
  214.         struct termios options;  
  215.   
  216.         bzero(&options, sizeof(options));       // clear options  
  217.   
  218.         cfsetispeed(&options,B9600);            // setup baud rate  
  219.         cfsetospeed(&options,B9600);  
  220.   
  221.         options.c_cflag |= (CRTSCTS | CS8 | CLOCAL | CREAD);  
  222.     options.c_iflag = IGNPAR;  
  223.   
  224.     tcflush(fd, TCIFLUSH);  
  225.   
  226.         tcsetattr(fd, TCSANOW, &options);  
  227.   
  228. }//end init_ttyS  
  229.   
  230.   
  231. //------------------------------------- main ----------------------------------------------------------------  
  232. int main(void)  
  233. {  
  234.         int fd;  
  235.   
  236.         printf("\nGSM/GPRS TESTS\n\n");  
  237.   
  238.         // open seriel port  
  239.         fd = open(DEVICE_TTYS, O_RDWR);  
  240.   
  241.         if (fd == -1)  
  242.         {  
  243.                 printf("open device %s error\n",DEVICE_TTYS);  
  244.         }  
  245.         else  
  246.         {  
  247.         init_ttyS(fd);  // init device  
  248.         func_GSM(fd);   // GSM/GPRS functions  
  249.                   
  250.         // close ttyS0  
  251.                 if (close(fd)!=0) printf("close device %s error",DEVICE_TTYS);  
  252.         }  
  253.   
  254.         return 0;  
  255. }// end main  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值