dump_trace.py

Mon, 10 Dec 2007 21:40:39 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 10 Dec 2007 21:40:39 +0100
changeset 6
6b6dc68588b2
parent 5
4d86b11abb82
child 10
2999318b49a2
permissions
-rw-r--r--

completed missing constants

0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 import sys
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 import time
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
3 import gpib
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
5 class GPIBtracedumper(gpib.GPIB):
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
6
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
7 MODE = {'binary': 'DDBN',
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
8 'ascii': 'DDAS',
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
9 'ansi': 'DDAN',
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
10 }
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
11 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
12 address=0):
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
13 super(GPIBtracedumper, self).__init__(device, baudrate, timeout, address, mode=1)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
14
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
15 def read_trace(self, mode="binary"):
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
16 mode = mode.lower()
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
17 assert mode in MODE
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
18 mode = MODE[mode]
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
19 res = ""
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
20 self._cnx.write('%s\r'%mode)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 i = 0
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
22 while i<self._retries:
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
23 l = self._cnx.readline()
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
24 if l.strip() == "":
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
25 i += 1
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
26 time.sleep(self._timeout)
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
27 continue
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
28 res += l
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
29 i = 0
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
30 return res
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
33 if __name__=='__main__':
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
34 import optparse
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
35 opt = optparse.OptionParser("A simple tool for dumping the current trace")
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
36 opt.add_option('-f', '--filename', default=None,
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
37 dest='filename',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
38 help='Output filename. If not set, write to stdout')
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
39 opt.add_option('-d', '--device', default='/dev/ttyUSB0',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
40 dest='device',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
41 help='Device of the RS232 connection (default: /dev/ttyUSB0)',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
42 )
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
43 opt.add_option('-m', '--mode', default='binary',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
44 dest='mode',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
45 help='Dumping mode (may be "binary" [default], "ascii" or "ansi")',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
46 )
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
47 opt.add_option('-a', '--address', default=0,
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
48 dest='address',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
49 help='GPIB address of the device',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
50 )
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
51 options, argv = opt.parse_args(sys.argv)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
53 cnx = GPIBtracedumper(device=options.device,
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
54 address=int(options.address))
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
55 res = cnx.read_trace(mode=options.mode)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
57 if options.filename:
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
58 open(options.filename, 'w').write(res)
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
59 else:
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
60 print res
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61

mercurial