#! /usr/bin/env python import sys import getopt import os import serial import binascii import string import time def usage(utyp, *msg): sys.stderr.write('Usage: %s\n' % os.path.split(sys.argv[0])[1]) if msg: sys.stderr.write('Error: %s\n' % `msg`) sys.exit(1) class Global: tr1 = string.maketrans('','') def __init__(self): self.filename = None self.nb = 0 return def writeb2(self,d): d2 = binascii.b2a_hex(d) self.fout.write('%s' % (d2,)) if self.fout2: self.fout2.write('%s' % (d2,)) self.fout2.flush() return def writeb(self,d): return self.writeb2(d) def doit(self,args): fout = sys.stdout self.fout = fout if self.filename: fout2 = file(self.filename,'w') else: fout2 = None self.fout2 = fout2 port = args.pop(0) speed = int(args.pop(0)) ser = serial.Serial(port, speed) ser.open() d = sys.stdin.read() d = d.translate(self.tr1,' \t\r\n') d = binascii.a2b_hex(d) a = 0 bl = 64 olen = len(d) while d: d0 = d[:bl] d = d[len(d0):] bl0 = len(d0) # d0 = binascii.b2a_hex(d0) ser.write(d0) # sys.stdout.write('\n %s\n' % (repr(d0),)) a = a + bl0 sys.stdout.write(' 0x%06x / 0x%06x\r' % (a,olen)) sys.stdout.flush() time.sleep(0.1) sys.stdout.write('\n') return def main(argv): self = Global() try: opts, args = getopt.getopt(argv[1:], 'hf:c:', ['help', 'file=', 'count=', ]) except getopt.error, msg: usage(1, msg) for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-f', '--file'): self.filename = arg elif opt in ('-c', '--count'): self.fetchcount = int(arg) self.doit(args) if __name__ == '__main__': main(sys.argv)