Mon, 17 Dec 2007 18:57:18 +0100
created a HP3562A submodule for specific code
0 | 1 | import sys |
2 | import time | |
4 | 3 | import gpib |
0 | 4 | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
5 | class HP3562dumper(gpib.GPIB): |
5
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
6 | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
7 | MODES = {'trace': 'DD', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
8 | 'state': 'DS', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
9 | 'coord': 'DC', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
10 | } |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
11 | |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
12 | FORMATS = {'binary': 'BN', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
13 | 'ascii': 'AS', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
14 | 'ansi': 'AN'} |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
15 | |
5
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
16 | 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
|
17 | address=0): |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
18 | super(HP3562dumper, self).__init__(device, baudrate, timeout, address, mode=1) |
0 | 19 | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
20 | def dump(self, mode='trace', format="binary"): |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
21 | format = format.lower() |
5
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
22 | mode = mode.lower() |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
23 | assert mode in self.MODES |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
24 | assert format in self.FORMATS |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
25 | cmd = self.MODES[mode] + self.FORMATS[format] |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
26 | |
5
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
27 | res = "" |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
28 | print "command = ", cmd |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
29 | self._cnx.write('%s\r'%cmd) |
0 | 30 | i = 0 |
5
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
31 | while i<self._retries: |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
32 | l = self._cnx.readline() |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
33 | if l.strip() == "": |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
34 | i += 1 |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
35 | time.sleep(self._timeout) |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
36 | continue |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
37 | res += l |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
38 | i = 0 |
4d86b11abb82
ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents:
4
diff
changeset
|
39 | return res |
0 | 40 | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
41 | |
0 | 42 | |
4 | 43 | if __name__=='__main__': |
44 | import optparse | |
45 | opt = optparse.OptionParser("A simple tool for dumping the current trace") | |
46 | opt.add_option('-f', '--filename', default=None, | |
47 | dest='filename', | |
48 | help='Output filename. If not set, write to stdout') | |
49 | opt.add_option('-d', '--device', default='/dev/ttyUSB0', | |
50 | dest='device', | |
51 | help='Device of the RS232 connection (default: /dev/ttyUSB0)', | |
52 | ) | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
53 | opt.add_option('-a', '--address', default=0, |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
54 | dest='address', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
55 | help='GPIB address of the device', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
56 | ) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
57 | opt.add_option('-b', '--block', default='trace', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
58 | dest='block', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
59 | help='Data block to dump (may be "trace" [default], "state" or "coord")', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
60 | ) |
4 | 61 | opt.add_option('-m', '--mode', default='binary', |
62 | dest='mode', | |
63 | help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', | |
64 | ) | |
65 | options, argv = opt.parse_args(sys.argv) | |
0 | 66 | |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
67 | cnx = HP3562dumper(device=options.device, address=int(options.address)) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
68 | res = cnx.dump(mode=options.block, format=options.mode) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
5
diff
changeset
|
69 | sys.stderr.write("read %s bytes\n"%(len(res))) |
4 | 70 | if options.filename: |
71 | open(options.filename, 'w').write(res) | |
72 | else: | |
73 | print res | |
0 | 74 |