Mon, 10 Dec 2007 21:22:33 +0100
small cleanup
import sys import time import gpib MODE = {'binary': 'DDBN', 'ascii': 'DDAS', 'ansi': 'DDAN', } def read_trace(cnx, mode="binary"): mode = mode.lower() assert mode in MODE mode = MODE[mode] res = "" cnx.write('%s\r'%mode) i = 0 while i<5: l = cnx.readline() if l.strip() == "": i += 1 time.sleep(0.1) continue #print "got a new line (%s chars) [i=%s]"%(len(l), i) res += l i = 0 return res if __name__=='__main__': import optparse opt = optparse.OptionParser("A simple tool for dumping the current trace") opt.add_option('-f', '--filename', default=None, dest='filename', help='Output filename. If not set, write to stdout') opt.add_option('-d', '--device', default='/dev/ttyUSB0', dest='device', help='Device of the RS232 connection (default: /dev/ttyUSB0)', ) opt.add_option('-m', '--mode', default='binary', dest='mode', help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', ) opt.add_option('-a', '--address', default=0, dest='address', help='GPIB address of the device', ) options, argv = opt.parse_args(sys.argv) cnx = open_connection(device=options.device, address=int(options.address), mode=1) res = read_trace(cnx, mode=options.mode) if options.filename: open(options.filename, 'w').write(res) else: print res