dump_trace.py

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

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

addede a __init__.py

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

mercurial