Skip to content

Instantly share code, notes, and snippets.

@lkarsten
Created July 9, 2017 12:09
Show Gist options
  • Save lkarsten/f6ad5a12c8f21013a3cd256f59a518a7 to your computer and use it in GitHub Desktop.
Save lkarsten/f6ad5a12c8f21013a3cd256f59a518a7 to your computer and use it in GitHub Desktop.
splitax.py
#!/usr/bin/env python3
"""
Split Airmar .ax files consisting of multiple SREC blocks into separate files,
so srec_cat/objcopy can read them.
Author: Lasse Karstensen <lasse.karstensen@gmail.com>, July 2017.
"""
from sys import argv
from os.path import basename
def split_ax(inputfile):
data = open(inputfile).readlines()
idx = []
start = 0
for i in range(0, len(data)):
if data[i].startswith("S9"):
idx.append((start, i+1))
start = i+1
if len(idx) == 0:
print("ERROR: File %s has no SREC start marker. Incorrect file type?" % inputfile)
return
else:
print("File %s has %i sections." % (inputfile, len(idx)))
for i, ptr in enumerate(idx):
start, end = ptr
filename = "%s__part%i.srec" % (basename(inputfile).replace(".ax", ""), i)
with open(filename, "w") as fp:
for line in data[start:end]:
fp.write(line)
print("Wrote %s" % filename)
if __name__ == "__main__":
if len(argv) == 1:
print("Usage: %s inputfile.ax <inputfile2.ax> <inputfileN.ax..>" % argv[0])
exit(1)
for inputfile in argv[1:]:
split_ax(inputfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment