summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2013-07-02 14:47:39 +0000
committerChristian Pointner <equinox@spreadspace.org>2013-07-02 14:47:39 +0000
commit764112610a50dd0c1d7013e8541ff0fded49b7ac (patch)
tree1750ba179d6e2ed6887ea0d91024044ac301c5f4
parentimproved exception handling (diff)
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
-rwxr-xr-xdownloader/downloader.py70
1 files changed, 37 insertions, 33 deletions
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()