以下是代碼:
#include
#include
#include
#define TAG_SIGN "$$TAG=NMS"
#define TAG_LOCK "Lock"
#define TAG_DEMOD "Demod"
#define TAG_CARDID "CardID"
#define TAG_AREAID "AreaID"
#define TAG_FVOLUME "FVolume"
#define TAG_FREQ "Freq"
#define TAG_NETID "NetID"
#define TAG_TSID "TSID"
#define TAG_SERVICEID "ServiceID"
#define TAG_PVOLUME "PVolume"
#define TAG_SWITCH "Switch"
#define TAG_TIME "Time"
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
enum
{
SWITCH_LOCK,
SWITCH_DEMOD,
SWITCH_CARDID,
SWITCH_AREAID,
SWITCH_FVOLUME,
SWITCH_FREQ,
SWITCH_NETID,
SWITCH_TSID,
SWITCH_SERVICEID,
SWITCH_PVOLUME,
SWITCH_SWITCH,
SWITCH_TIME,
SWITCH_DEFULT
};
typedef struct NmsDataStruct
{
uint8_t lock;
char demod;
char card_id[12];
char area_id[20];
uint8_t flash_volume;
uint8_t frequency;
char net_id[4];
char ts_id[4];
char service_id[4];
uint8_t present_volume;
uint8_t i_switch;
char present_time[16];
uint16_t crc;
}NmsData, *pNmsData;
uint8_t GetSign(char *name)
{
if(strcmp(name,TAG_LOCK)==0)
return SWITCH_LOCK;
else if(strcmp(name,TAG_DEMOD)==0)
return SWITCH_DEMOD;
else if(strcmp(name,TAG_CARDID)==0)
return SWITCH_CARDID;
else if(strcmp(name,TAG_AREAID)==0)
return SWITCH_AREAID;
else if(strcmp(name,TAG_FVOLUME)==0)
return SWITCH_FVOLUME;
else if(strcmp(name,TAG_FREQ)==0)
return SWITCH_FREQ;
else if(strcmp(name,TAG_NETID)==0)
return SWITCH_NETID;
else if(strcmp(name,TAG_TSID)==0)
return SWITCH_TSID;
else if(strcmp(name,TAG_SERVICEID)==0)
return SWITCH_SERVICEID;
else if(strcmp(name,TAG_PVOLUME)==0)
return SWITCH_PVOLUME;
else if(strcmp(name,TAG_SWITCH)==0)
return SWITCH_SWITCH;
else if(strcmp(name,TAG_TIME)==0)
return SWITCH_TIME;
else
return SWITCH_DEFULT;
}
void GetFlashParameter(char pstr,NmsData pNmsData)
{
char str_name[16],string[32];
strcpy(string,pstr);
char *str_value;
str_value = strchr(string,'=')+1;
strncpy(str_name,string,str_value-string-1);
str_name[str_value-string-1]='\0';
switch(GetSign(str_name))
{
case SWITCH_LOCK:
pNmsData->lock = 5;
break;
case SWITCH_DEMOD:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_CARDID:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_AREAID:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_FVOLUME:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_FREQ:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_NETID:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_TSID:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_SERVICEID:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_PVOLUME:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_SWITCH:
printf("%s=%s\n",str_name,str_value);
break;
case SWITCH_TIME:
printf("%s=%s\n",str_name,str_value);
break;
default:
break;
}
}
bool ParseBuf(char* buf, int buf_len, NmsData * nms_data)
{
char string[256];
strcpy(string,buf);
char *pstr;
pstr = strtok(string, ",");
if(strcmp(pstr,TAG_SIGN)!=0)
return false;
do
{
pstr = strtok(NULL, ",");
if(pstr)
GetFlashParameter(pstr,nms_data);
} while (pstr);
}
bool main()
{
char *buf = "$$TAG=NMS,Lock=1,Demod=C,CardID=00029696,AreaID=101716112233,FVolume=20,Freq=595000,NetID=0,TSID=1,ServiceID=514,PVolume=23,Switch=0,Time=150228093000,CRC=112233";
NmsData *pNmsData = NULL;
return ParseBuf(buf,strlen(buf),pNmsData);
}
指針沒有初始化造成的。很明顯指針地址不可能是0x0000000