HP3562A/dump_datablock.py

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

mercurial