[CANoe] File processing_hex file reading and analysis

There are only three codes in the hex file: 00, 01, and 04. Then we only need to analyze these three different states differently when analyzing.

For analysis of hex file format, you can read: Detailed explanation of HEX file format

First create a Block structure. Based on experience, we know that a data block has: starting address, data length, and data Buffer. Three results. There may be multiple data blocks in a hex file, so we declare 5 data blocks next. This needs to be adjusted based on variables. Then initialize the number of data blocks according to the hex file. Then declare the variable HexBlockTotalNumber to be the total number of data blocks.

/*@!Encoding:936*/
variables
{<!-- -->
  struct Block {<!-- -->
  dword BlockStartAddr; //The address where the data starts
  dword BlockDataLength; //length of data
  byte dataBuffer[0x020FFFF]; //Data area (Buffer for single block data, if the hex file is large, you need to increase the parameters)
  };
  struct Block hexfile[5]; //Create 5 data blocks (mainly adjusted according to the HEX file, you need to set how many data blocks there are in hex)
  int HexBlockTotalNumber = 0; //Total number of data blocks
  dword t1; //Calculate the time spent in parsing
}
 
 
/****************************************************** *********************************
  *Function: //char2byte
  * Description: //Function to convert a single character to Byte
  *Input: //ch: ASCII encoded character, ranging from 0 to F
  *Return: //val, of byte type
*************************************************** *********************************/
byte char2byte(char ch)
{<!-- -->
   byte val;
   val = 0;
   if (ch >= '0' & amp; & amp; ch <= '9')
   {<!-- -->
      val = ch - '0';
   }
   if (ch >= 'a' & amp; & amp; ch <= 'f')
   {<!-- -->
      val = (ch - 'a') + 10;
   }
   if (ch >= 'A' & amp; & amp; ch <= 'F')
   {<!-- -->
      val = (ch - 'A') + 10;
   }
   return val;
}
 
/****************************************************** *********************************
  *Function: //Read_hexFile
  * Description: //Decode HEX file, only supports 0x00, 0x04, 0x01 types
  *Input: //Filename: the file name to be decoded
  *Output: //hexfile
  *Return: //void
*************************************************** *********************************/
//Read HEXFILE
void Read_hexFile(char Filename[])
{<!-- -->
  
  long file_handle;
  char RowData[128]; //Read row by row, cache data in each row. When the data in each row is greater than 128, it needs to be adjusted.
dword i;
dword RowDataByte; //The number of bytes in a single data block
qword OffsetAddress; //Extended linear address
qword ReAddr; //Start address of the previous data line
dword Len; //The number of valid data bytes in each line of HEX
dword ReLen; //HEX previous data length
dword Addr; //HEX starting address of each line
dword Type; //HEX each line type, there are four types: 00, 01, 04
  RowDataByte = 0;i = 0;Len = 0;ReLen = 0;Addr=0;Type = 0;ReAddr = 0;
  file_handle = OpenFileRead(Filename,0);
  HexBlockTotalNumber = 0;
  if(file_handle!=0)
  {<!-- --> // Read all lines
    while ( fileGetStringSZ(RowData,elcount(RowData),file_handle)!=0 ){<!-- -->
      //Determine whether the first character is:
      if(RowData[0] == ':'){<!-- -->
        Len = (char2byte(RowData[1])*0x10 + char2byte(RowData[2]));
        Addr = char2byte(RowData[3])*0x1000 + char2byte(RowData[4])*0x100 + char2byte(RowData[5])*0x10 + char2byte(RowData[6]);
        Addr |= (OffsetAddress << 16);
        Type = char2byte(RowData[7])*0x10 + char2byte(RowData[8]);
        //The following is the process of printing and parsing, printing the variables during parsing
        //write("RowData:%s,HexBlockTotalNumber:%d,ReLen:%X,ReAddr:%X,Addr:%X,RowDataByte:%X",RowData,HexBlockTotalNumber,ReLen,ReAddr,Addr,RowDataByte);
        switch(Type){<!-- -->
          case 0x00: //data
            if (Addr > (ReLen + ReAddr)){<!-- --> //Judged to be a new data block
                if(RowDataByte == 0) //Whether it is the number of data bytes in the first row
  {<!-- -->
  hexfile[HexBlockTotalNumber].BlockStartAddr = Addr; //Record the starting address of the new data block
  }
  else //not the first line
  {<!-- -->
  hexfile[HexBlockTotalNumber].BlockDataLength = RowDataByte; //Data length
  RowDataByte = 0; //Restart counting
                  HexBlockTotalNumber + + ;
  hexfile[HexBlockTotalNumber].BlockStartAddr = Addr; //Record the starting address of the new data block
  }
            }
             for(i = 0; i< Len ; i + + )
                {<!-- -->
                  //Save the buffer. Note that the crc is not verified.
                  hexfile[HexBlockTotalNumber].dataBuffer[RowDataByte + + ]=(char2byte(RowData[2*i + 9])*0x10 + char2byte(RowData[2*i + 10]));
                }
            ReAddr = Addr; //Save the current address and use it next time
ReLen = Len; //Save the current length and use it next time
           break;
          case 0x04: //Extended linear address record
OffsetAddress = char2byte(RowData[9])*0x1000 + char2byte(RowData[10])*0x100 + char2byte(RowData[11])*0x10 + char2byte(RowData[12]); //Offset address
break;
          case 0x01: //address, end
           hexfile[HexBlockTotalNumber].BlockDataLength = RowDataByte; //Data length
           HexBlockTotalNumber + + ;
          break;
        }
      }
 
    }
  write("Hex file read successfully, data block: %d",HexBlockTotalNumber);
  for(i = 0; i < HexBlockTotalNumber; i + + )
{<!-- -->
write("Data block: %d, start address: 0x%X, end address: 0x%X, data length: m bytes\r\
", i + 1, hexfile[i].BlockStartAddr, hexfile[i ].BlockStartAddr + hexfile[i].BlockDataLength - 1, hexfile[i].BlockDataLength);
}
  fileClose(file_handle);
  }
  else{<!-- -->
     write("OpenFileRead,error occurs");
  }
 
}
on key 'f'
{<!-- -->
  t1 = timeNow();
  Read_hexFile(".//test.hex");
  write("Parse time: %f",t1-timeNow());
  write("data block:1");
  write(0);
  write("data block: 2");
  write(1);
}
 
write (byte blockNum)
{<!-- -->
   int i;
   for (i=0; i<16; i + + ) write("dataBuffer[%d]:0x%2X",i,hexfile[blockNum].dataBuffer[i]);
}

The result of text.hex opened with Hexview, we read the data in the circle:

operation result:

refer to:
CANOE CAPL programming HEX file reading
https://blog.csdn.net/zengqz123/article/details/106550213