Intel Hex文件解析

ccs 属性设置可以 生成hex文件,hex是标准的16进制格式,包含数据起始标志、数据长度、数据地址、数据类型、数据内容

和校验和,下边是用vs 对hex文件进行读取和解析,得到地址和内容。 

  • include"stdio.h"
     #include"MCU_header.h"
    /*
    
    void writeExcel(void) {
    
    	FILE *fp = NULL;
    	float t;
    	char ch;
    
    	fp = fopen("F:\\CCSIntel.hex", "r");
    	for (int i = 0; i < 3; i++) {
    		printf("please input:");
    		scanf("%f %c", &t, &ch);
    		fprintf(fp, "%f\t%c\n", t, ch);
    	}
    	fclose(fp);
    
    }
    
    int main()
    {
    
    
    	return 0;
    }
    
    CCSIntel.h*/ 
    void CharToData(char Inchar[], int length, int outData[]);
    main()
    {
    
    	Uint8 s1[300] = { 0 };
    	int outData[300] = {0};
    	int32 baseAddress=0;
    	int16 i = 0;
    	FILE *fp;
    	fp = fopen("F:\\CCSIntel.hex", "r");
    	for (i = 0; i < 300; i++)  //大写数字比数字大48
    	{                          //字母
    		//首先读取字符串
    		fscanf(fp, "%s", s1);  //遇到回车自动停止输出 并且切换到下一个
    	 	printf("%s\n", s1);
    		// 然后把数组转换为数字
    		//对字符串进行判断执行
    		int16 i,sLength;
    		for (i=0;s1[i]!='\0';i++)
    		{ 		}
    		sLength = i;
    
    		CharToData(s1,sLength,outData);
    
    		
    
    
    		if (outData[0] != 58)
    		{
    			return 0;
    		}
    		else
    		{
    			//数据长度
    			int16 dataLength;
    			dataLength = outData[1] * 16 + outData[2];
    			if (dataLength == 0)
    			{
    				break;
    			}
    			printf("数据长度:0x%x\n", dataLength);
    			//数据地址
    			int32 dataAdrress,Address;
    			dataAdrress = outData[3] * 4096 + outData[4] * 256 + outData[5] * 16 + outData[6];
    			Address = (baseAddress << 16) + dataAdrress;//z在printf中运算出错
    			printf("数据地址:0x%x\n", ((baseAddress << 16) + dataAdrress));
    				//数据类型
    			int16 dataType;
    			dataType = outData[7] * 16 + outData[8];
    			printf("数据类型:0x%x\n", dataType);
    			if (dataType==0x04)
    			{
    				baseAddress = outData[9] * 4096 + outData[10]* 256 + outData[11] * 16 + outData[12];
    			}
    			//数据内容
    
    			//数据校验和
    			int16 checkSum;
    			checkSum = outData[8 + 2 * dataLength + 1] * 16 + outData[8 + 2 * dataLength + 2];
    			printf("数据校验和:0x%x\n", checkSum);
    
    		}		 
    	}
    	fclose(fp);
    	
    	
    	system("pause");
    
    
    }
    
    //
    void CharToData(char Inchar[], int length,int outData[])
    {
    
    	int i;
    	 
    	for (i = 0; i < length; i++)
    	{
    		if (Inchar[i] == ':')
    		{
    			outData[i] = 58;
    		}else if(Inchar[i] <58)
    		{
    			outData[i] = Inchar[i]-48;
    		}
    		else if (Inchar[i] < 71)
    		{
    			outData[i] = Inchar[i] - 55;
    		}else
    		{
    			printf("Error\n");
    		}
    
    	}
    
    }

     运行结果如下,可以实现正确解析并且程序结尾自动跳出Intel Hex文件解析_第1张图片

     

你可能感兴趣的:(dsp,C常用)