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