如何进行字符串解析,并打印需要的字符串

一、应用场景。
需要进行字符解析的场景一般是走AT指令集的,比如GPS模块,GPRS模块,WIFI模块。下面给出一个WIFI模块接收到的数据包:

+IPD,260:{"results":[{"location":{"id":"WTTE97PU94T9","name":"Wuxi","country":"CN","path":"Wuxi,Wuxi,Jiangsu,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"Cloudy","code":"4","temperature":"36"},"last_update":"2017-08-07T1

这是wifi模块从串口输出的数据。一般情况下,我们是用单片机的串口进行接收,并把它存在数据里。

二、解析过程。

 char *Weather_text_Point;
char *Weather_temperature_Point;
unsigned char *NET_DEVICE_GetIPD(unsigned short timeOut)
{
char Param_out[1024]={0};
#if(NET_DEVICE_TRANS == 0)
    unsigned int  count = 0;
    unsigned int byte = 0;
    char sByte[5]={0};
    char *ptrIPD;
    unsigned char Weather_temperature_temp;
#endif

    do
    {
        if(NET_IO_WaitRecive() == REV_OK)                               
        {

            sprintf (Param_out ,"receive buf =%s\r\n", netIOInfo.buf);
UsartPrintf(USART_DEBUG, Param_out);                
#if(NET_DEVICE_TRANS == 0)
            ptrIPD = strstr((char *)netIOInfo.buf, "IPD,");             
            if(ptrIPD == NULL)                                          
            {
                UsartPrintf(USART_DEBUG, "\"IPD\" not found\r\n");
            }
            else
            {       
                ptrIPD = strstr(ptrIPD, "text");
                ptrIPD=ptrIPD+7;
                while(*ptrIPD !='"')
                {
                        Weather_text[count++]=*ptrIPD++ ;
                }
                Weather_text_Point=Weather_text ;

                sprintf (Param_out ,"Weather_text =%s\r\n", Weather_text);
                UsartPrintf(USART_DEBUG, Param_out);

                count=0;
                ptrIPD = strstr(ptrIPD, "temperature");
                ptrIPD=ptrIPD+14;
                while(*ptrIPD !='"')
                {
                        Weather_temperature[count++]=*ptrIPD++ ;
                }
                Weather_temperature_Point=Weather_temperature ;
                sprintf (Param_out ,"Weather_temperature =%s\r\n", Weather_temperature);
                UsartPrintf(USART_DEBUG, Param_out);
                //Weather_Point=Weather_temperature ;

//              while(*ptrIPD != ':')                                   
//              {
//                  sByte[count++] = *ptrIPD++;
//              }
//              byte = atoi(sByte);                     //
            //  ptrIPD++;                                               
//              for(count = 0; count < (byte+8); count++)                   
//              {
//                  netIOInfo.buf[count] = *ptrIPD++;
//              }
            //  memset(netIOInfo.buf + byte,0, sizeof(netIOInfo.buf) - byte);               
                return netIOInfo.buf;                                   
            }
#else
            return netIOInfo.buf;
#endif
        }

    } while(timeOut--);

    return NULL;                                                        

}

三、程序说明
1、首先利用strstr(str1,str2)函数,strstr是一种函数,从字符串str1中查找是否有符串str2,如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。
程序中利用这个函数可以迅速定位到某些关键字。进而获取关键字后的信息。

2、利用while(*ptrIPD!=‘!’)这行代码,实现把特定的字符串写入相应的数组里。

3、利用sprintf()函数,进行打印。
int sprintf( char *buffer, const char *format, [ argument] … );
说明1:该函数包含在stdio.h的头文件中,使用时需要加入:
说明2:sprintf与printf函数的区别:二者功能相似,但是sprintf函数打印到字符串中,而printf函数打印输出到屏幕上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。此函数的精华在于可以格式化输出。
比如,程序中解析出来一组温度数据,放在一个数组里。那么想用串口把这个数据打印出来,就需要用到这个函数。

4、aoti可以把数组里的字符串转换成实际的数字。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值