From 764112610a50dd0c1d7013e8541ff0fded49b7ac Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 2 Jul 2013 14:47:39 +0000 Subject: serial device handling and hex file loading in seperate functions git-svn-id: https://svn.spreadspace.org/pic/trunk@19 a09c6847-51d9-44de-8ef2-e725cf50f3c7 --- downloader/downloader.py | 70 +++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'downloader/downloader.py') diff --git a/downloader/downloader.py b/downloader/downloader.py index 4b4ee6c..49ffe03 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -25,6 +25,41 @@ VERSION = '0.1' +def open_serial(device): + try: + dev = os.open(device, os.O_RDWR | os.O_NOCTTY); + tty.setraw(dev, termios.TCSAFLUSH) + termios.tcflush(dev, termios.TCIFLUSH) + return dev + + except OSError, msg: + print "ERROR: opening serial device: %s" % msg + sys.exit(3) + except termios.error, msg: + print "ERROR: configuring serial device: %s" % msg + sys.exit(3) + +def load_hex(file): + fin = file + if fin == '-': + fin = sys.stdin + elif not os.path.isfile(fin): + print "ERROR: File not found: %s" % fin + sys.exit(1) + + data = {} + ih = IntelHex(fin) + for a in ih.addresses(): + if a/2 not in data.keys(): + data[a/2] = 0 + if a%2 == 0: + data[a/2] += ih[a] + else: + data[a/2] += (ih[a] << 8) + + return data + + if __name__ == '__main__': import getopt import os @@ -75,39 +110,8 @@ Options: print usage sys.exit(2) - - - - try: - dev = os.open(device, os.O_RDWR | os.O_NOCTTY); - tty.setraw(dev, termios.TCSAFLUSH) - termios.tcflush(dev, termios.TCIFLUSH) - - except OSError, msg: - print "ERROR: opening serial device: %s" % msg - sys.exit(3) - except termios.error, msg: - print "ERROR: configuring serial device: %s" % msg - sys.exit(3) - - - fin = args[0] - if fin == '-': - fin = sys.stdin - elif not os.path.isfile(fin): - print "ERROR: File not found: %s" % fin - sys.exit(1) - - data = {} - ih = IntelHex(fin) - for a in ih.addresses(): - if a/2 not in data.keys(): - data[a/2] = 0 - if a%2 == 0: - data[a/2] += ih[a] - else: - data[a/2] += (ih[a] << 8) - + dev = open_serial(device) + data = load_hex(args[0]) addrs = data.keys() addrs.sort() -- cgit v1.2.3