From 9e33162e30c3867f5b4f73ac754d0e28c608b650 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Mon, 8 Aug 2016 22:13:16 +0200 Subject: [PATCH 1/2] General regexp to handle more hex formats and Python3 compatibility --- cfetool.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cfetool.py b/cfetool.py index 4ebaead..cb0709d 100755 --- a/cfetool.py +++ b/cfetool.py @@ -6,9 +6,9 @@ import serial import sys import re +import binascii -lineregex = re.compile(r'(?:[0-9a-f]{8})(?:[:])((?: [0-9a-f]{2}){1,16})') -#lineregex = re.compile(r'(?:[0-9a-f]{8})(?:[:])((?: [0-9a-f]{2}){1,16})(?:\s{4})(?:.{16})') +lineregex = re.compile(r'(?:[0-9a-f]{8})(?:[:])((?: [0-9a-f]*){1,16})') def printf(string): sys.stdout.write(string) @@ -21,7 +21,7 @@ def skip_prompt(ser): def wait_prompt(ser): printf("Waiting for a prompt...") while True: - ser.write("\x03") + ser.write(b"\x03") if(ser.read(1) == 'C' and ser.read(1) == 'F' and ser.read(1) == 'E' and ser.read(1) == '>'): skip_prompt(ser) printf(" OK\n") @@ -33,9 +33,10 @@ def memreadblock(ser, addr, size): buf='' m = False while not m: - m = lineregex.match(ser.readline().strip()) + rsp = ser.readline().strip() + m = lineregex.match(rsp) while m: - bytes = [chr(int(x, 16)) for x in m.group(1)[1:].split(' ')] + bytes = [binascii.unhexlify(x) for x in m.group(1)[1:].split(' ')] buf+=''.join(bytes) m = lineregex.match(ser.readline().strip()) return buf From 6f6183503b69e7d3b9a0a5fa1e101027fd397a55 Mon Sep 17 00:00:00 2001 From: Sylvain Pelissier Date: Thu, 25 Aug 2016 08:33:05 +0200 Subject: [PATCH 2/2] Regexp corrected --- cfetool.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cfetool.py b/cfetool.py index cb0709d..8c45098 100755 --- a/cfetool.py +++ b/cfetool.py @@ -8,7 +8,7 @@ import re import binascii -lineregex = re.compile(r'(?:[0-9a-f]{8})(?:[:])((?: [0-9a-f]*){1,16})') +lineregex = re.compile(r'(?:[0-9a-f]{8})(?:[:])((?: [0-9a-f]*){1,16}) ') def printf(string): sys.stdout.write(string) @@ -38,7 +38,8 @@ def memreadblock(ser, addr, size): while m: bytes = [binascii.unhexlify(x) for x in m.group(1)[1:].split(' ')] buf+=''.join(bytes) - m = lineregex.match(ser.readline().strip()) + rsp = ser.readline().strip() + m = lineregex.match(rsp.strip()) return buf def memreadblock2file(ser, fd, addr, size):