dump_trace.py

Mon, 10 Dec 2007 21:22:33 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 10 Dec 2007 21:22:33 +0100
changeset 4
269aacbb2bf2
parent 1
0670b1f5c155
child 5
4d86b11abb82
permissions
-rw-r--r--

small cleanup

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

mercurial