Mon, 10 Dec 2007 21:18:31 +0100
make 7470 plotter emulator more user friendly & several file renames
dump_as_ascii.py | file | annotate | diff | comparison | revisions | |
dump_trace.py | file | annotate | diff | comparison | revisions | |
gpib_plotter.py | file | annotate | diff | comparison | revisions | |
load_gpib_plot.py | file | annotate | diff | comparison | revisions |
--- a/dump_as_ascii.py Mon Dec 10 20:05:39 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -import sys -import time -import serial - -MODE = {'binary': 'DDBN', - 'ascii': 'DDAS', - 'ansi': 'DDAN', - } -class ConnectionError(Exception): - pass - -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 - -def open_connection(device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, - address=0, mode=1): - p = serial.Serial(port=device, baudrate=baudrate, timeout=timeout) - - p.write('++addr %d\r'%address) # set address to 0 - p.write('++mode %d\r'%mode) # read listen only mode - p.write('++mode\r') - i = 0 - for i in range(10): - rmode = p.readline().strip() - if rmode != "": - break - time.sleep(timeout) - - if rmode == '' or int(rmode) != mode: - raise ConnectionError("Can't set GPIB mode to %s"%mode) - return p - - -import optparse -opt = optparse.OptionParser() -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 -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dump_trace.py Mon Dec 10 21:18:31 2007 +0100 @@ -0,0 +1,56 @@ +import sys +import time + +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 + + + +import optparse +opt = optparse.OptionParser() +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 +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpib_plotter.py Mon Dec 10 21:18:31 2007 +0100 @@ -0,0 +1,123 @@ +import sys, os +import time +import gpib + +class GPIBplotter(gpib.GPIB): + replies={ + "OE": "0", + "OH": "0,0,10000,7500", + "OI": "7470A", + "OP": "0,0,10000,7500", + "OO": "0,1,0,0,0,0,0,0", + "OF": "40,40", + "OS": "24", + } + def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, + address=0): + super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0) + + def load_plot(self, wait_timeout=0): + """ + Make a full plot process. + + 'wait_timeout' is the first timeout. Same semantic as the + 'timeout' property of a serial connection (if set to None, it + will block until the GPIB device actually perform a plot + command.) + """ + res = "" + i=0 + replies = self._replies.copy() + if wait_timeout is not None or not isinstance(wait_timeout, int): + raise TypeError + if wait_timeout<0: + raise ValueError + + self._cnx.timeout = wait_timeout + firstloop = True + while i<self._retries: + l = self._cnx.readline().strip() + if firstloop: + self._cnx.timeout = self._timeout + firstloop = False + if l == "": + i += 1 + for k, v in replies.items(): + if res.endswith(k) or res.endswith(k+';'): + self._cnx.write("%s"%v) + if k == "OS": + replies[k] = "16" + break + self._cnx.write('\r') + if i > self._retries/2: + time.sleep(self._timeout) + continue + res += l + "\n" + i = 0 + return res + +if __name__ == '__main__': + import optparse + opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)') + 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('-a', '--address', default=0, + dest='address', + help='GPIB address of the device', + ) + opt.add_option('-l', '--loop', default=False, + action="store_true", + dest="loop", + help="Continuously wait for new plots. If set, filename must be set and files will be created with _n at the end") + opt.add_option('-v', '--verbose', default=False, + action="store_true", + dest="verbose", + help="Verbose mode",) + + options, argv = opt.parse_args(sys.argv) + + if options.loop and not options.filename: + opt.error('If loop is set, you *must* provide a filename') + + if options.filename: + outf = open(options.filename, "w") + else: + outf = sys.stdout + + + try: + plotter = GPIBplotter(device=options.device, address=options.address) + except (gpib.SerialException, gpib.ConnectionError), e: + sys.stderr.write('Connection error:\n\t' + '\n\t'.join([str(x) for x in e.args]) + '\n') + sys.stderr.write('Check your parameters\n') + sys.exit(1) + if options.verbose: + sys.stderr.write('connection established\n') + + loop = True + nloop = 0 + while loop: + plot = plotter.load_plot(wait_timeout=1) + if options.verbose: + sys.stderr.write('.') + if plot: + outf.write(plot) + if options.verbose: + sys.stderr.write('\n') + sys.stderr.write('Received a new plot (written to %s)\n'%outf.name) + if not options.loop: + loop = False + else: + nloop += 1 + fname, ext = os.path.splitext(options.filename) + outf = open(fname + "_%d"%nloop + ext, 'w') + + + + +
--- a/load_gpib_plot.py Mon Dec 10 20:05:39 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -import sys -import time -import serial -p = serial.Serial(port="/dev/ttyUSB0", baudrate=115200, timeout=0.1) - -p.write('++addr 5\r') # set address to 0 -p.write('++mode 0\r') # read listen only mode -p.write('++mode\r') -for i in range(10): - mode = p.readline().strip() - if mode != "": - break - time.sleep(0.1) -print "mode = ", mode -if mode == '': - print "strange" - sys.exit() - -res = "" -i=0 - -replies={ - "OE": "0", - "OH": "0,0,10000,7500", - "OI": "7470A", - "OP": "0,0,10000,7500", - "OO": "0,1,0,0,0,0,0,0", - "OF": "40,40", - "OS": "24", - } - -while i<15: - l = p.readline().strip() - if l == "": - i += 1 - for k, v in replies.items(): - if res.endswith(k) or res.endswith(k+';'): - print "got commend", k - p.write("%s"%v) - if k == "OS": - replies[k] = "16" - break - p.write('\r') - time.sleep(0.1) - continue - print "got a new line (%s chars) [i=%s]"%(len(l), i) - res += l + "\n" - i = 0 -print "over" -print "res = ", res - -open('out.gpib', 'w').write(res) - -