Skip to content

Instantly share code, notes, and snippets.

@mnaberez
Created June 14, 2018 15:53
Show Gist options
  • Save mnaberez/af710f74c71ac6b48189ae117be530f1 to your computer and use it in GitHub Desktop.
Save mnaberez/af710f74c71ac6b48189ae117be530f1 to your computer and use it in GitHub Desktop.
Parse a memory dump log from the KWP1281 tool and write it to a binary file (https://github.com/mnaberez/vwradio)
#!/usr/bin/env python
'''
Parse a log file containing "MEM:" lines from the KWP1281 tool
and write the data to a binary file.
Usage: %s file.log
'''
import os
import sys
def parse_dump_from_log(filename):
with open(filename, 'r') as f:
dump = bytearray()
for line in f.readlines():
if line.startswith('MEM:'):
# "MEM: 00A0: 03 31 00 ..." -> [0x03, 0x31, 0x00, ...]
linedata = [ int(s, 16) for s in line.split()[2:] ]
dump.extend(linedata)
return dump
def write_dump(filename, dump):
with open(filename, 'wb') as f:
f.write(dump)
def main():
if len(sys.argv) != 2:
sys.stderr.write(__doc__ % sys.argv[0])
sys.exit(1)
infile = os.path.abspath(sys.argv[1])
outfile = "%s.bin" % os.path.splitext(infile)[0]
dump = parse_dump_from_log(infile)
write_dump(outfile, dump)
print("Dump written to: %s" % outfile)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment