解决获取3G信号强度(转载)

39 篇文章 3 订阅

http://blog.csdn.net/lwpping/article/details/7530760


获取强度:AT+CSQ 以“\r","\n"结尾

遇到问题:

一方面是因为AT指令格式不对,最后只发了"\r",没有发"\n",另外 PPP与AT采用不同的tty,我的模块是BD_MF206(可以发AT+CGMR得到),对应的ttyUSB1为AT指令,ttyUSB2为PPP对应的,但有点不明白的是一开始用的是ttyUSB2,而且指令也没有加"\n",居然一样得到了信号强度。。。;而且使用ttyUSB1发送命令。。时常返回ERROR。。

是因为串初始化没做好。




[cpp]  view plain copy
  1. #include <stdio.h>          
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>    
  5. #include <fcntl.h>           
  6. #include <termios.h>       
  7. #include <errno.h>          
  8. #include <sys/ioctl.h>        
  9. #include <assert.h>           
  10. #include <pthread.h>  
  11.   
  12. #include <sys/wait.h>  
  13. #include <sys/klog.h>  
  14. #include <syslog.h>  
  15. #include <string.h>  
  16.   
  17.   
  18. static int is_value_get(void);  
  19. static int write_level(char *level);  
  20.   
  21.   
  22. pthread_mutex_t read_mutex;  
  23. pthread_cond_t  read_cond;  
  24.   
  25.   
  26. pthread_mutex_t wr_mutex;  
  27. pthread_cond_t  wr_cond;  
  28. static wr_num_sig = 0;  
  29. static re_num_sig = 0;  
  30.   
  31. static int m_fd;  
  32. static int get_level = -1;  
  33. static check_sec = 3 ;  
  34. static err_num = 0;  
  35. static int ok_flag = 0;  
  36. static int snum = 5;  
  37.   
  38. //#define DEBUG  
  39.   
  40. #ifdef DEBUG  
  41. #define debug(fmt,args...)   do { syslog(LOG_INFO,"%s: -->"fmt, __FUNCTION__,##args); } while (0)  
  42. #else  
  43. #define debug(fmt,args...)  while(0)  
  44. #endif  
  45.   
  46. #define STORE_3G_LEVEL  "/sys/devices/platform/netconstore/store_3g"  
  47.   
  48.   
  49. //write level value to the sys interface  
  50. int write_level(char *level)  
  51. {  
  52.   
  53.     char cmd[200];        
  54.     sprintf(cmd,"echo %s > /sys/devices/platform/netconstore/store_3g",level);  
  55.     debug("LWP========= the cmd is %s\n",cmd);  
  56.   
  57.     int ret = system(cmd);  
  58.     if (WEXITSTATUS (ret) == 0)  
  59.     {  
  60.         return 1;  
  61.     }  
  62.   
  63.     debug("LWP=========HAS write %s into sys \n",level);  
  64.   
  65.     return 1;  
  66.   
  67. }  
  68.   
  69.   
  70. #if 1  
  71. //send the at cmd to the 3G MODEM  
  72. int send_cmd(char *cmd)  
  73. {  
  74.     debug("HAS INTO send_cmd!!!!!!!!!!!!!!!!!!!!!!!!!\n");  
  75.   
  76.     int n;  
  77.     int m = 0;  
  78.   
  79. rewrite:  
  80.   
  81.     n = write(m_fd, cmd, strlen(cmd));  
  82.     if(n < 0)  
  83.     {  
  84.         fprintf(stderr,"write 3g device failed by:%s",strerror(errno));  
  85.         //open ttyusb1  
  86.         close(m_fd);  
  87.         open_3g_dev_ttyusb1();    
  88.         goto rewrite;  
  89.         //return -1;  
  90.     }  
  91.   
  92.     debug("WRITE AT+CSQ ok!!\n");  
  93.   
  94.   
  95.   
  96. #if 1  
  97.     n = write(m_fd, "\r",1);  
  98.     if(n < 0)  
  99.     {  
  100.         fprintf(stderr,"write R device failed by:%s",strerror(errno));  
  101.         return -1;  
  102.     }  
  103.   
  104. #endif  
  105.   
  106. #if 1  
  107.     n = write(m_fd, "\n",1);  
  108.     if(n < 0)  
  109.     {  
  110.         fprintf(stderr,"write R device failed by:%s",strerror(errno));  
  111.         return -1;  
  112.     }  
  113.     debug("WRITE \\r \\n ok!!\n");  
  114. #endif  
  115.   
  116.     usleep(300);  
  117.   
  118.     //send the signal to get_res  
  119.     pthread_mutex_lock(&read_mutex);  
  120.     if(re_num_sig > 0)  
  121.     {  
  122.         pthread_cond_signal(&read_cond);  
  123.         pthread_mutex_unlock(&read_mutex);  
  124.     }  
  125.     else  
  126.     {  
  127.         pthread_mutex_unlock(&read_mutex);  
  128.     }  
  129.     //then wait the get_res deal the result   
  130.     pthread_mutex_lock(&wr_mutex);  
  131.       
  132.     wr_num_sig++;  
  133.     pthread_cond_wait(&wr_cond, &wr_mutex);  
  134.     pthread_mutex_unlock(&wr_mutex);  
  135.       
  136.     return 1;  
  137. }  
  138. #endif  
  139.   
  140.   
  141. //get the at cmd result  
  142. void *get_res(void *para)  
  143. {  
  144.       
  145.     debug("lwp==========================HAS INTO GET RES\n!!!!!!!!!!!!!!!!!!!!!!!!!");  
  146.     int n;  
  147.     char res[512];  
  148.     char *g_tmp = NULL;  
  149.     char *g_token=NULL;  
  150.       
  151.       
  152. for(;;)  
  153. {  
  154.     //wait the write func to send the sigal to begin read  
  155.     pthread_mutex_lock(&read_mutex);  
  156.     re_num_sig++;  
  157.       
  158.     pthread_cond_wait(&read_cond, &read_mutex);  
  159.     pthread_mutex_unlock(&read_mutex);  
  160.           
  161.     memset(res,0,512);  
  162.       
  163.   
  164. #if 1         
  165.     while(read(m_fd, res, 512))  
  166.     {  
  167.         debug("lwp==========================[[IN while]]HAS read %s \n",res);  
  168.         if(strstr(res,"+CSQ:"))  
  169.         {             
  170.             g_tmp = res;  
  171.             if((g_token = strsep (&g_tmp,":"))!=NULL)  
  172.             {  
  173.                 debug("g_token is %s \n",g_token);  
  174.                 if((g_token = strsep (&g_tmp,","))!=NULL)  
  175.                 {  
  176.                     debug("g_token is %s \n",g_token);  
  177.                       
  178.                     write_level(g_token);  
  179.                                           
  180.                       
  181.                     get_level = atoi(g_token);  
  182.                     debug("after atoi g_token is %d\n",get_level);  
  183.                       
  184.                     //close(m_fd);  
  185.                     //send signal to write func to get level again  
  186.                     pthread_mutex_lock(&wr_mutex);  
  187.                     if(wr_num_sig > 0)  
  188.                     {  
  189.                         pthread_cond_signal(&wr_cond);  
  190.                         pthread_mutex_unlock(&wr_mutex);  
  191.                     }  
  192.                     else  
  193.                     {  
  194.                         pthread_mutex_unlock(&wr_mutex);  
  195.                     }  
  196.                     break;  
  197.                 }  
  198.             }  
  199.             break;  
  200.         }  
  201.         else if(strstr(res,"ERROR"))  
  202.         {  
  203.     #if 0     
  204.             close(m_fd);  
  205.             open_3g_dev();  
  206.       
  207.             err_num++;  
  208.               
  209.             if(err_num == 16)  
  210.             {  
  211.                 check_sec = 60;  
  212.             }  
  213.     #endif  
  214.       
  215.             err_num++;  
  216.             pthread_mutex_lock(&wr_mutex);  
  217.                     if(wr_num_sig > 0)  
  218.                     {  
  219.                         pthread_cond_signal(&wr_cond);  
  220.                         pthread_mutex_unlock(&wr_mutex);  
  221.                     }  
  222.                     else  
  223.                     {  
  224.                         pthread_mutex_unlock(&wr_mutex);  
  225.                     }  
  226.                       
  227.                       
  228.             break;  
  229.         }  
  230.         else if(strstr(res,"3GNET"))  
  231.         {  
  232.     #if 1     
  233.             close(m_fd);  
  234.             open_3g_dev_ttyusb1();  
  235.     #endif    
  236.               
  237.             pthread_mutex_lock(&wr_mutex);  
  238.                     if(wr_num_sig > 0)  
  239.                     {  
  240.                         pthread_cond_signal(&wr_cond);  
  241.                         pthread_mutex_unlock(&wr_mutex);  
  242.                     }  
  243.                     else  
  244.                     {  
  245.                         pthread_mutex_unlock(&wr_mutex);  
  246.                     }  
  247.                       
  248.                       
  249.             break;  
  250.         }  
  251.         //debug("lwp==========================HAS read %s \n",res);  
  252.     }  
  253.       
  254. #endif  
  255.   
  256.     debug("lwp==========================after one read...\n");  
  257. }  
  258.   
  259.     debug("lwp==========================++++++++++exit the read at result pthread++\n");  
  260. }  
  261.   
  262.   
  263. //creat the get the at cmd results thread  
  264. int init_get_res()  
  265. {  
  266.     int err;  
  267.     pthread_t       rtid;     
  268.     pthread_attr_t pthread_attr;  
  269.   
  270.     if(pthread_attr_init(&pthread_attr)){  
  271.         syslog(LOG_ERR, "Attribute creation failed in %s", __FUNCTION__);  
  272.         return -1;  
  273.     }  
  274.     if(pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) ){  
  275.         syslog(LOG_ERR,"Setting detach attribute failed in %s", __FUNCTION__);  
  276.         return -1;  
  277.     }  
  278.   
  279.     if((err = pthread_create(&rtid, &pthread_attr, get_res, NULL))) {  
  280.         fprintf(stderr,"creat init_get_res failed:%s\n",strerror(errno));  
  281.         return -1;  
  282.     }  
  283.   
  284.     return 1;  
  285. }  
  286.   
  287.   
  288. int open_3g_dev()  
  289. {  
  290.     debug("OPEN 3G DEVIECE ttyUSB2\n");  
  291.   
  292.     m_fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY);  
  293.     if(m_fd < 0)  
  294.     {  
  295.         fprintf(stderr,"open 3g device failed by:%s",strerror(errno));  
  296.         return -1;  
  297.     }     
  298.   
  299.     return 1;  
  300. }  
  301.   
  302. int open_3g_dev_ttyusb1()  
  303. {  
  304.     //open ttyUSB2 failed we try to open ttyUSB1  
  305.     debug("OPEN 3G DEVIECE ttyUSB1\n");       
  306.     m_fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);  
  307.     if(m_fd < 0)  
  308.     {  
  309.         fprintf(stderr,"open 3g device failed by:%s\n",strerror(errno));  
  310.         exit(-1);         
  311.     }     
  312.   
  313.     return 1;  
  314.   
  315. }  
  316.   
  317. //judge if has get the 3g level at begin  
  318. int is_value_get()  
  319. {  
  320.   
  321.     FILE *g_fp;  
  322.     int g_read = -1;  
  323.     char g_buffer[BUFSIZ];  
  324.   
  325.     g_fp = popen("cat /sys/devices/platform/netconstore/store_3g""r");  
  326.     if ( g_fp != NULL )   
  327.     {  
  328.         g_read = fread(g_buffer, sizeof(char), BUFSIZ-1, g_fp);  
  329.         if (g_read <= 0)   
  330.         {                 
  331.             pclose(g_fp);  
  332.             return -1;  
  333.         }  
  334.         else  
  335.         {  
  336.             pclose(g_fp);  
  337.             return 1;  
  338.         }  
  339.     }  
  340.     else  
  341.     {  
  342.         return -1;  
  343.     }  
  344. }  
  345.   
  346. int is_3g_on()  
  347. {  
  348.     debug("INTO the check modem!!!");  
  349.     FILE *fp;  
  350.     char line[1024];  
  351.   
  352.     syslog(LOG_INFO, "%s:--> begin to check ppp0", __FUNCTION__);  
  353.     if( (fp=fopen("/proc/net/route""r")) < 0) {  
  354.         syslog(LOG_ERR, "/proc/net/route open failed");  
  355.         return 0;  
  356.     }  
  357.   
  358.     while(fgets(line, 1023, fp)) {  
  359.         if(strstr(line, "ppp0")) {  
  360.             debug("3G IS ON!!!");  
  361.             fclose(fp);  
  362.             return 1;  
  363.         }  
  364.     }  
  365.   
  366.     syslog(LOG_INFO, "%s:--> 3g is off", __FUNCTION__);  
  367.     fclose(fp);  
  368.     return 0;     
  369.   
  370. }  
  371.   
  372.   
  373. void  init_log(void)  
  374. {  
  375.     int option = LOG_NDELAY | LOG_PID | LOG_PERROR;  
  376.     openlog("CONNDAEMON", option, LOG_DAEMON);  
  377.     syslog(LOG_INFO, "connd started !");  
  378. }  
  379.   
  380. static int open_dev(const char *dev)  
  381.   
  382. {  
  383.   
  384.     int  fd = open( dev, O_RDWR | O_NOCTTY | O_NDELAY );  
  385.   
  386.     if (fd != -1 ){  
  387.   
  388.         fcntl(fd, F_SETFL,0); //set to block  
  389.   
  390.     }  
  391.   
  392.     return fd;  
  393.   
  394. }  
  395.   
  396.   
  397. void close_serial_port(int fd)  
  398. {  
  399.   
  400.     close(fd);  
  401.   
  402. }  
  403.   
  404. int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)  
  405. {  
  406.     struct termios newtio,oldtio;  
  407.     //save old setting  
  408.     if ( tcgetattr( fd,&oldtio) != 0)   
  409.     {   
  410.         perror("SetupSerial 1");  
  411.         return -1;  
  412.     }  
  413.     //extern void bzero(void *s, int n); set s zero   
  414.     bzero( &newtio, sizeof( newtio ) );  
  415.     //set the size of char  
  416.     newtio.c_cflag |= CLOCAL | CREAD;   
  417.     newtio.c_cflag &= ~CSIZE;  
  418.     //set data mode  
  419.     switch( nBits )  
  420.     {  
  421.         case 7:  
  422.             newtio.c_cflag |= CS7;  
  423.             break;  
  424.         case 8:  
  425.             newtio.c_cflag |= CS8;  
  426.             break;  
  427.     }  
  428.     //set check  
  429.     switch( nEvent )  
  430.     {  
  431.         case 'O':  
  432.             newtio.c_cflag |= PARENB;  
  433.             newtio.c_cflag |= PARODD;  
  434.             newtio.c_iflag |= (INPCK | ISTRIP);  
  435.             break;  
  436.         case 'E':   
  437.             newtio.c_iflag |= (INPCK | ISTRIP);  
  438.             newtio.c_cflag |= PARENB;  
  439.             newtio.c_cflag &= ~PARODD;  
  440.             break;  
  441.         case 'N':   
  442.             newtio.c_cflag &= ~PARENB;  
  443.             break;  
  444.     }  
  445.     //set bound  
  446.     switch( nSpeed )  
  447.     {  
  448.         case 2400:  
  449.             cfsetispeed(&newtio, B2400);  
  450.             cfsetospeed(&newtio, B2400);  
  451.             break;  
  452.         case 4800:  
  453.             cfsetispeed(&newtio, B4800);  
  454.             cfsetospeed(&newtio, B4800);  
  455.             break;  
  456.         case 9600:  
  457.             cfsetispeed(&newtio, B9600);  
  458.             cfsetospeed(&newtio, B9600);  
  459.             break;  
  460.         case 115200:  
  461.             cfsetispeed(&newtio, B115200);  
  462.             cfsetospeed(&newtio, B115200);  
  463.             break;  
  464.         default:  
  465.             cfsetispeed(&newtio, B9600);  
  466.             cfsetospeed(&newtio, B9600);  
  467.             break;  
  468.     }  
  469.     //set stop  
  470.     if( nStop == 1 )  
  471.         newtio.c_cflag &= ~CSTOPB;  
  472.     else if ( nStop == 2 )  
  473.         newtio.c_cflag |= CSTOPB;  
  474.     //set wait time and less recv char   
  475.     newtio.c_cc[VTIME] = 0;  
  476.     newtio.c_cc[VMIN] = 0;  
  477.     //deal the char not recv  
  478.     tcflush(fd,TCIFLUSH);  
  479.     //new setting   
  480.     if((tcsetattr(fd,TCSANOW,&newtio))!=0)  
  481.     {  
  482.         perror("com set error");//  
  483.         return -1;  
  484.     }  
  485.     printf("set done!\n");  
  486.     return 0;  
  487. }  
  488.   
  489.   
  490. int main(int argc, char *argv[])  
  491. {     
  492.   
  493.     init_log();  
  494.   
  495. #if 1  
  496.     /* we should change daemon(0,1) to daemon(0,0) after finish all */  
  497.     if(daemon(1,1)<0){  
  498.         fprintf(stderr,"connd can't init daemon!");  
  499.         exit(-1);  
  500.     }  
  501.   
  502. #endif  
  503.   
  504.     if ((m_fd = open_dev("/dev/ttyUSB1")) < 0)  
  505.     {  
  506.         fprintf(stderr,"open 3g device failed \n");  
  507.         return -1;  
  508.     }  
  509.   
  510.     //set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);  
  511.   
  512.     if(set_opt(m_fd,115200,8,'N',1) < 0)  
  513.     {  
  514.         fprintf(stderr,"set serial  failed \n");  
  515.         return -1;  
  516.     }  
  517.   
  518.     debug("set serial ok\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");  
  519.     //creat the get result thread   
  520.     if(init_get_res() < 0 )  
  521.     {  
  522.         return -1;  
  523.     }  
  524.   
  525.   
  526. #if 1  
  527.     while(1)   
  528.     {  
  529.   
  530.         if(send_cmd("AT+CSQ") < 0)  
  531.         {  
  532.             fprintf(stderr,"the send cmd to 3g wrong!!\n");  
  533.             close(m_fd);  
  534.             return -1;  
  535.         }  
  536.   
  537.         if((is_value_get()) == 1)  
  538.         {  
  539.             check_sec = 40;  
  540.             err_num = 0;  
  541.   
  542.         }         
  543.         else  
  544.         {  
  545.             if(err_num > 10)  
  546.             {  
  547.                 check_sec = 120;  
  548.             }  
  549.             else  
  550.             {  
  551.                 check_sec = 2;  
  552.             }  
  553.         }     
  554.   
  555.         sleep(check_sec);         
  556.     }  
  557. #endif  
  558.   
  559.     return 0;  
  560. }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值