#! /usr/bin/env python import sys import getopt import os import serial import binascii 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: 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() while 1: n = ser.inWaiting() if n == 0: if 0: fout.write('No data 1\n') fout2.flush() n = 1 pass if n: d = ser.read(n) if d: self.writeb(d) else: if 1: fout.write('No data\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)