j123b567 / java-intelhex-parser

Java IntelHex parser library
15 stars 13 forks source link

Find a specific address in the .hex file #4

Open ridi-mz opened 7 years ago

ridi-mz commented 7 years ago

Hello, I'm trying to parse some .a2l and .hex files to extract variables and their values. So far l don't know how to find the values of the variables in the .hex file. Here is a link (https://wiki.asam.net/display/STANDARDS/ASAM+CDF#ASAMCDF-Downloads) to download an example of these files. To be more specific : How can I read the value at the address 0x810600 in the .hex file ?

/begin CHARACTERISTIC ASAM.C.DEPENDENT.REF_1.SWORD
  "Dependent SWORD" 
  VALUE 
  0x810600                                             
  RL.FNC.SWORD.ROW_DIR                                 
  0 
  CM.IDENTICAL
  -32268 32267
  /begin DEPENDENT_CHARACTERISTIC 
     "X1 + 5" 
     ASAM.C.SCALAR.SBYTE.IDENTICAL                    
  /end DEPENDENT_CHARACTERISTIC
  DISPLAY_IDENTIFIER DI.ASAM.C.DEPENDENT.REF_1.SWORD
/end CHARACTERISTIC
j123b567 commented 7 years ago

There are several strategies. If you need multiple (all) defined variables from .a2l file, you will prefere this method:

You can implement your own DataListener. It can create some binary representation of the .hex file in the memory. It can compact sequential regions in larger blocks. Have a look at class MemoryRegions used in class RangeDetector. These clases just calculates address ranges, but can be easilly extended to store also data.

Later you can implement lookup function on that data, that will find correct region containing that address and return required number of bytes. After that, it is easy to fill your Java model with data from HEX.

Other way is, if you need just one variable from that file. This is already implemented in my second project in C# (https://github.com/j123b567/net-intelhex-parser), where class BinWriter has two more parameters - input and output memory region. If you compile the latest example C# program (I'm using MonoDevelop/XamarinStudio for this). It can be also easilly rewritten to Java. To extract 2 byte long variable at address 0x810600, you will call this from commandline

hex2bin.exe -i=0x810600:0x810601 -o=0x0000:0x0001 ASAP2_Demo_V161.hex variable.bin

Parsing .a2l file is out of the scope of this library.

j123b567 commented 7 years ago

Here is precompiled latest version of the utility https://github.com/j123b567/net-intelhex-parser/releases/download/v2.0/hex2bin.exe