HP3562A/dump_datablock.py

Mon, 04 Feb 2008 19:49:48 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 04 Feb 2008 19:49:48 +0100
changeset 39
8becd52c2171
parent 17
fb8aa055f6e4
permissions
-rw-r--r--

refactoring in progress

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
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
4 import prologix
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
6
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
7 class HP3562dumper(prologix.GPIB):
17
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
8 """
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
9 Class dedicated to dump data blocks from HP3562A DSA (trace,
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
10 internal state or coordinate).
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
11 """
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
12 MODES = {'trace': 'DD',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
13 'state': 'DS',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
14 'coord': 'DC',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
15 }
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
16
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
17 FORMATS = {'binary': 'BN',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
18 'ascii': 'AS',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
19 'ansi': 'AN'}
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
20
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
21 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
22 address=0):
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
23 super(HP3562dumper, self).__init__(device, baudrate, timeout, address, mode=1)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
25 def dump(self, mode='trace', format="binary"):
17
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
26 """
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
27 Dump the required data block and return it as a raw string buffer.
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
28
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
29 'mode' can be 'trace' [default], 'state' or 'coord'.
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
30 'format' can be 'binary' [default], 'ascii' or 'ansi'.
fb8aa055f6e4 docstrings added
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
31 """
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
32 format = format.lower()
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
33 mode = mode.lower()
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
34 assert mode in self.MODES
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
35 assert format in self.FORMATS
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
36 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
37
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
38 res = ""
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
39 print "command = ", cmd
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
40 self._cnx.write('%s\r'%cmd)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 i = 0
5
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
42 while i<self._retries:
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
43 l = self._cnx.readline()
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
44 if l.strip() == "":
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
45 i += 1
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
46 time.sleep(self._timeout)
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
47 continue
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
48 res += l
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
49 i = 0
4d86b11abb82 ported 'dump_trace' to gpib API
David Douard <david.douard@logilab.fr>
parents: 4
diff changeset
50 return res
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
52
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
54 def main():
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
55 import optparse
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
56 opt = optparse.OptionParser("A simple tool for dumping the current trace")
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
57 opt.add_option('-f', '--filename', default=None,
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
58 dest='filename',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
59 help='Output filename. If not set, write to stdout')
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
60 opt.add_option('-d', '--device', default='/dev/ttyUSB0',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
61 dest='device',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
62 help='Device of the RS232 connection (default: /dev/ttyUSB0)',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
63 )
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
64 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
65 dest='address',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
66 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
67 )
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
68 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
69 dest='block',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
70 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
71 )
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
72 opt.add_option('-m', '--mode', default='binary',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
73 dest='mode',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
74 help='Dumping mode (may be "binary" [default], "ascii" or "ansi")',
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
75 )
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
76 options, argv = opt.parse_args(sys.argv)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 5
diff changeset
78 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
79 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
80 sys.stderr.write("read %s bytes\n"%(len(res)))
4
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
81 if options.filename:
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
82 open(options.filename, 'w').write(res)
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
83 else:
269aacbb2bf2 small cleanup
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
84 print res
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
85
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
86 if __name__=='__main__':
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
87 main()

mercurial