赞
踩
C语言sscanf函数作用是用一字符串(buffer)来格式化输入数据。头文件为<stdio.h>。函数原型为:
int sscanf(const char *buffer,const char *format, [ argument ] ... );
其中buffer为格式化输入数据的来源。
在AT指令返回的字符串中,会有不同类型的数据,比如十进制数,十六进制字符串,字符串等,要根据数据类型来解析。假设要解析的AT指令中其中6为十进制数,Hello_World位置为不定长字符串,-77为十进制数,dc:bc:ff:44:66:77为MAC地址,为十六进制字符串。如果使用指针偏移的形式解析,6和-77可以使用atoi()函数解析,字符串可以逐个解析,MAC地址就需要自己写字符串转十六进制函数实现,不仅需要注意指针的偏移量,而且代码量也会增加。使用sscanf可以一次性解析出不同的数据,不需要通过指针逐步偏移实现。
//示例:
+ATTest:(6,"Hello_World",-77,"dc:bc:ff:44:66:77")
①使用sscanf解析:
//解析Hello_World时可以使用正则表达式%[^\"]。 其中[^\"]表示负值字符集合,匹配未包含”的任意字符,\为转义字符。
sscanf(pstr+strlen("+ATTest:"), "(%d,\"%[^\"]\",%d,\"%2x:%2x:%2x:%2x:%2x:%2x\",%d)", &ecn, ssid, &rssi, &mac[0], &mac[1], &mac[2], &mac[3],&mac[4],&mac[5]);
②使用指针偏移逐个解析:
pstr += strlen("+ATTest:("); ecn = atoi(pstr); pstr = strstr(pstr, ",\""); pstr += 2; while(*pstr != '\"') { ssid[i++] = *pstr; pstr++; } pstr = strstr(pstr, ","); pstr++; rssi = atoi(pstr); pstr = strstr(pstr, ",\""); pstr += 2; String_To_Hex(&mac[0],pstr,2); pstr += 3; String_To_Hex(&mac[1],pstr,2); pstr += 3; String_To_Hex(&mac[2],pstr,2); pstr += 3; String_To_Hex(&mac[3],pstr,2); pstr += 3; String_To_Hex(&mac[4],pstr,2); pstr += 3; String_To_Hex(&mac[5],pstr,2);
使用两种方式的运行结果是相同的:
可以看到使用sscanf可以一次性解析出不同类型的数据,比较方便。同样在发送AT指令数据时,针对不同类型的数据,可以使用sprintf()函数来进行格式化输出。
//以下是解析AT指令的测试代码 #include <stdio.h> #include <string.h> #include <stdlib.h> #define USE_SSCANF unsigned char str[] = {"+ATTest:(6,\"Hello_World\",-77,\"dc:bc:ff:44:66:77\")"}; unsigned char ecn = 0; unsigned char ssid[30]; char rssi = 0; unsigned char mac[6]; const char hexChr[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; static unsigned char CharToHex(unsigned char chr) { unsigned char i = 0; for(; i < 0x0F; i++) { if(chr == hexChr[i]) { break; } } return i & 0x0F; } void String_To_Hex(unsigned char *destStr, unsigned char* srcStr, unsigned short len) { unsigned char i = 0, j = 0; unsigned char highByte = 0, lowByte = 0; if(destStr == NULL || srcStr == NULL || len == 0) { return ; } for(; (i < len) && (j < (len) / 2); i += 2, j++) { highByte = CharToHex(srcStr[i]); lowByte = CharToHex(srcStr[i + 1]); destStr[j] = (highByte << 4) | lowByte; } } int main(void) { char *pstr = NULL; char i = 0; #ifdef USE_SSCANF if((pstr = strstr(str, "+ATTest:")) != NULL) { printf("str = %s\r\n",&str); printf("pstr = %s\r\n",pstr); sscanf(pstr+strlen("+ATTest:"), "(%d,\"%[^\"]\",%d,\"%2x:%2x:%2x:%2x:%2x:%2x\",%d)", &ecn, ssid, &rssi, &mac[0], &mac[1], &mac[2], &mac[3],&mac[4],&mac[5]); printf("ecn = %d, rssi = %d\r\n",ecn,rssi); printf("ssid = %s\r\n",&ssid); printf("mac = %x %x %x %x %x %x\r\n",mac[0], mac[1], mac[2], mac[3],mac[4],mac[5]); } #else if((pstr = strstr(str, "+ATTest:")) != NULL) { printf("str = %s\r\n",&str); printf("pstr = %s\r\n",pstr); pstr += strlen("+ATTest:("); ecn = atoi(pstr); pstr = strstr(pstr, ",\""); pstr += 2; while(*pstr != '\"') { ssid[i++] = *pstr; pstr++; } pstr = strstr(pstr, ","); pstr++; rssi = atoi(pstr); pstr = strstr(pstr, ",\""); pstr += 2; String_To_Hex(&mac[0],pstr,2); pstr += 3; String_To_Hex(&mac[1],pstr,2); pstr += 3; String_To_Hex(&mac[2],pstr,2); pstr += 3; String_To_Hex(&mac[3],pstr,2); pstr += 3; String_To_Hex(&mac[4],pstr,2); pstr += 3; String_To_Hex(&mac[5],pstr,2); printf("ecn = %d, rssi = %d\r\n",ecn,rssi); printf("ssid = %s\r\n",&ssid); printf("mac = %x %x %x %x %x %x\r\n",mac[0], mac[1], mac[2], mac[3],mac[4],mac[5]); } #endif return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。