dump_trace.py

changeset 11
3ccb0023cf41
parent 10
2999318b49a2
child 12
a04bea92c509
equal deleted inserted replaced
10:2999318b49a2 11:3ccb0023cf41
1 import sys
2 import time
3 import gpib
4
5 class HP3562dumper(gpib.GPIB):
6
7 MODES = {'trace': 'DD',
8 'state': 'DS',
9 'coord': 'DC',
10 }
11
12 FORMATS = {'binary': 'BN',
13 'ascii': 'AS',
14 'ansi': 'AN'}
15
16 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
17 address=0):
18 super(HP3562dumper, self).__init__(device, baudrate, timeout, address, mode=1)
19
20 def dump(self, mode='trace', format="binary"):
21 format = format.lower()
22 mode = mode.lower()
23 assert mode in self.MODES
24 assert format in self.FORMATS
25 cmd = self.MODES[mode] + self.FORMATS[format]
26
27 res = ""
28 print "command = ", cmd
29 self._cnx.write('%s\r'%cmd)
30 i = 0
31 while i<self._retries:
32 l = self._cnx.readline()
33 if l.strip() == "":
34 i += 1
35 time.sleep(self._timeout)
36 continue
37 res += l
38 i = 0
39 return res
40
41
42
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 )
53 opt.add_option('-a', '--address', default=0,
54 dest='address',
55 help='GPIB address of the device',
56 )
57 opt.add_option('-b', '--block', default='trace',
58 dest='block',
59 help='Data block to dump (may be "trace" [default], "state" or "coord")',
60 )
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)
66
67 cnx = HP3562dumper(device=options.device, address=int(options.address))
68 res = cnx.dump(mode=options.block, format=options.mode)
69 sys.stderr.write("read %s bytes\n"%(len(res)))
70 if options.filename:
71 open(options.filename, 'w').write(res)
72 else:
73 print res
74

mercurial