dump_trace.py

Fri, 14 Dec 2007 00:21:47 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 14 Dec 2007 00:21:47 +0100
changeset 9
3b50c46fca56
parent 5
4d86b11abb82
child 10
2999318b49a2
permissions
-rw-r--r--

several updates to the gpib module

import sys
import time
import gpib

class GPIBtracedumper(gpib.GPIB):

    MODE = {'binary': 'DDBN',
            'ascii': 'DDAS',
            'ansi': 'DDAN',
            }
    def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
                 address=0):
        super(GPIBtracedumper, self).__init__(device, baudrate, timeout, address, mode=1)

    def read_trace(self, mode="binary"): 
        mode = mode.lower()
        assert mode in MODE
        mode = MODE[mode]
        res = ""
        self._cnx.write('%s\r'%mode)
        i = 0
        while i<self._retries:
            l = self._cnx.readline()
            if l.strip() == "":
                i += 1
                time.sleep(self._timeout)
                continue
            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 = GPIBtracedumper(device=options.device,
                          address=int(options.address))
    res = cnx.read_trace(mode=options.mode)

    if options.filename:
        open(options.filename, 'w').write(res)
    else:
        print res
    

mercurial