|
1 import sys, os |
|
2 import time |
|
3 import gpib |
|
4 |
|
5 class GPIBplotter(gpib.GPIB): |
|
6 replies={ |
|
7 "OE": "0", |
|
8 "OH": "0,0,10000,7500", |
|
9 "OI": "7470A", |
|
10 "OP": "0,0,10000,7500", |
|
11 "OO": "0,1,0,0,0,0,0,0", |
|
12 "OF": "40,40", |
|
13 "OS": "24", |
|
14 } |
|
15 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, |
|
16 address=0): |
|
17 super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0) |
|
18 |
|
19 def load_plot(self, wait_timeout=0): |
|
20 """ |
|
21 Make a full plot process. |
|
22 |
|
23 'wait_timeout' is the first timeout. Same semantic as the |
|
24 'timeout' property of a serial connection (if set to None, it |
|
25 will block until the GPIB device actually perform a plot |
|
26 command.) |
|
27 """ |
|
28 res = "" |
|
29 i=0 |
|
30 replies = self._replies.copy() |
|
31 if wait_timeout is not None or not isinstance(wait_timeout, int): |
|
32 raise TypeError |
|
33 if wait_timeout<0: |
|
34 raise ValueError |
|
35 |
|
36 self._cnx.timeout = wait_timeout |
|
37 firstloop = True |
|
38 while i<self._retries: |
|
39 l = self._cnx.readline().strip() |
|
40 if firstloop: |
|
41 self._cnx.timeout = self._timeout |
|
42 firstloop = False |
|
43 if l == "": |
|
44 i += 1 |
|
45 for k, v in replies.items(): |
|
46 if res.endswith(k) or res.endswith(k+';'): |
|
47 self._cnx.write("%s"%v) |
|
48 if k == "OS": |
|
49 replies[k] = "16" |
|
50 break |
|
51 self._cnx.write('\r') |
|
52 if i > self._retries/2: |
|
53 time.sleep(self._timeout) |
|
54 continue |
|
55 res += l + "\n" |
|
56 i = 0 |
|
57 return res |
|
58 |
|
59 if __name__ == '__main__': |
|
60 import optparse |
|
61 opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)') |
|
62 opt.add_option('-f', '--filename', default=None, |
|
63 dest='filename', |
|
64 help='Output filename. If not set, write to stdout') |
|
65 opt.add_option('-d', '--device', default='/dev/ttyUSB0', |
|
66 dest='device', |
|
67 help='Device of the RS232 connection (default: /dev/ttyUSB0)', |
|
68 ) |
|
69 opt.add_option('-a', '--address', default=0, |
|
70 dest='address', |
|
71 help='GPIB address of the device', |
|
72 ) |
|
73 opt.add_option('-l', '--loop', default=False, |
|
74 action="store_true", |
|
75 dest="loop", |
|
76 help="Continuously wait for new plots. If set, filename must be set and files will be created with _n at the end") |
|
77 opt.add_option('-v', '--verbose', default=False, |
|
78 action="store_true", |
|
79 dest="verbose", |
|
80 help="Verbose mode",) |
|
81 |
|
82 options, argv = opt.parse_args(sys.argv) |
|
83 |
|
84 if options.loop and not options.filename: |
|
85 opt.error('If loop is set, you *must* provide a filename') |
|
86 |
|
87 if options.filename: |
|
88 outf = open(options.filename, "w") |
|
89 else: |
|
90 outf = sys.stdout |
|
91 |
|
92 |
|
93 try: |
|
94 plotter = GPIBplotter(device=options.device, address=options.address) |
|
95 except (gpib.SerialException, gpib.ConnectionError), e: |
|
96 sys.stderr.write('Connection error:\n\t' + '\n\t'.join([str(x) for x in e.args]) + '\n') |
|
97 sys.stderr.write('Check your parameters\n') |
|
98 sys.exit(1) |
|
99 if options.verbose: |
|
100 sys.stderr.write('connection established\n') |
|
101 |
|
102 loop = True |
|
103 nloop = 0 |
|
104 while loop: |
|
105 plot = plotter.load_plot(wait_timeout=1) |
|
106 if options.verbose: |
|
107 sys.stderr.write('.') |
|
108 if plot: |
|
109 outf.write(plot) |
|
110 if options.verbose: |
|
111 sys.stderr.write('\n') |
|
112 sys.stderr.write('Received a new plot (written to %s)\n'%outf.name) |
|
113 if not options.loop: |
|
114 loop = False |
|
115 else: |
|
116 nloop += 1 |
|
117 fname, ext = os.path.splitext(options.filename) |
|
118 outf = open(fname + "_%d"%nloop + ext, 'w') |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |