gpib_plotter.py

Wed, 19 Dec 2007 00:25:30 +0100

author
David Douard <david.douard@logilab.fr>
date
Wed, 19 Dec 2007 00:25:30 +0100
changeset 17
fb8aa055f6e4
parent 12
a04bea92c509
child 27
0f8f2621418f
permissions
-rw-r--r--

docstrings added

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
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
5 import gpib
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
6 from prologix import GPIB
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
7 class GPIBplotter(GPIB):
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
8 _replies={
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
9 "OE": "0",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
10 "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
11 "OI": "7470A",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
12 "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
13 "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
14 "OF": "40,40",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
15 "OS": "24",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
16 }
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
17 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
18 address=5):
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
19 super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
21 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
22 """
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
23 Make a full plot process.
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
25 '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
26 '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
27 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
28 command.)
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
29 """
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
30 res = ""
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
31 i=0
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
32 replies = self._replies.copy()
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
33 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
34 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
35 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
36 raise ValueError
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
37
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
38 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
39 firstloop = True
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
40 newdata = False
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
41 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
42 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
43 if firstloop:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
44 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
45 firstloop = False
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
46 if l == "":
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
47 if i == 0:# > (self._retries/2):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
48 # ie. we just received new stuffs (i is reset in the else block)
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
49 for k, v in replies.items():
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
50 # check wether we should reply smthg
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
51 eres = res.replace('\n', '').strip()
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
52 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
53 self._cnx.write("%s"%v)
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
54 if k == "OS":
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
55 replies[k] = "16"
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
56 break
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
57 self._cnx.write('\r')
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
58 #time.sleep(0.1)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
59 i += 1
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
60 else:
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
61 if not res:
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
62 print "Plotting..."
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
63 res += l + '\n'
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
64 i = 0
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
65 #time.sleep(0.1)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
66 if res:
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
67 print "DONE (received %d characters)"%len(res)
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
68 return res
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
70 if __name__ == '__main__':
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
71 import optparse
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
72 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
73 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
74 dest='filename',
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
75 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
76 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
77 dest='device',
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
78 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
79 )
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
80 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
81 dest='address',
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
82 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
83 )
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
84 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
85 action="store_true",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
86 dest="loop",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
87 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
88 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
89 action="store_true",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
90 dest="verbose",
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
91 help="Verbose mode",)
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
92
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
93 options, argv = opt.parse_args(sys.argv)
0
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 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
96 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
97
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
98 if options.filename:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
99 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
100 else:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
101 outf = sys.stdout
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
102
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
104 try:
8
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
105 plotter = GPIBplotter(device=options.device, address=int(options.address),
aa3d55f78563 adjust timing parameters & fixes
David Douard <david.douard@logilab.fr>
parents: 1
diff changeset
106 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
107 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
108 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
109 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
110 sys.exit(1)
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
111 if options.verbose:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
112 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
113
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
114 loop = True
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
115 nloop = 0
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
116 while loop:
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 8
diff changeset
117 plot = plotter.load_plot(wait_timeout=0.1)
1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
118 if options.verbose:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
119 sys.stderr.write('.')
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
120 if plot:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
121 outf.write(plot)
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
122 if options.verbose:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
123 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
124 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
125 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
126 loop = False
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
127 else:
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
128 nloop += 1
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
129 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
130 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
131
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
132
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
133
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
134
0670b1f5c155 make 7470 plotter emulator more user friendly & several file renames
David Douard <david.douard@logilab.fr>
parents: 0
diff changeset
135

mercurial