gpib_plotter.py

Fri, 14 Dec 2007 00:24:11 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 14 Dec 2007 00:24:11 +0100
changeset 10
2999318b49a2
parent 8
aa3d55f78563
child 12
a04bea92c509
permissions
-rw-r--r--

many improvements in the read/dump trace tools.
- read_trace can now read the 3 data blocks (trace, state and coord)
- many command line options added
- plot can now plot in linear, log or db vertical scale (command line options for that)
- plot now display the correct X axis ticks and label

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

mercurial