gpib_plotter.py

changeset 8
aa3d55f78563
parent 1
0670b1f5c155
child 12
a04bea92c509
equal deleted inserted replaced
7:2e2742648546 8:aa3d55f78563
1 import sys, os 1 import sys, os
2 import time 2 import time
3 import gpib 3 import gpib
4 4
5 class GPIBplotter(gpib.GPIB): 5 class GPIBplotter(gpib.GPIB):
6 replies={ 6 _replies={
7 "OE": "0", 7 "OE": "0",
8 "OH": "0,0,10000,7500", 8 "OH": "0,0,10000,7500",
9 "OI": "7470A", 9 "OI": "7470A",
10 "OP": "0,0,10000,7500", 10 "OP": "0,0,10000,7500",
11 "OO": "0,1,0,0,0,0,0,0", 11 "OO": "0,1,0,0,0,0,0,0",
26 command.) 26 command.)
27 """ 27 """
28 res = "" 28 res = ""
29 i=0 29 i=0
30 replies = self._replies.copy() 30 replies = self._replies.copy()
31 if wait_timeout is not None or not isinstance(wait_timeout, int): 31 if wait_timeout is not None and not isinstance(wait_timeout, (float, int)):
32 raise TypeError 32 raise TypeError, "wait_timeout (%s:%s) has wrong type"%(repr(wait_timeout), type(wait_timeout))
33 if wait_timeout<0: 33 if wait_timeout<0:
34 raise ValueError 34 raise ValueError
35 35
36 self._cnx.timeout = wait_timeout 36 self._cnx.timeout = wait_timeout
37 firstloop = True 37 firstloop = True
40 if firstloop: 40 if firstloop:
41 self._cnx.timeout = self._timeout 41 self._cnx.timeout = self._timeout
42 firstloop = False 42 firstloop = False
43 if l == "": 43 if l == "":
44 i += 1 44 i += 1
45 for k, v in replies.items(): 45 if i == (self._retries-1):
46 if res.endswith(k) or res.endswith(k+';'): 46 for k, v in replies.items():
47 self._cnx.write("%s"%v) 47 eres = res.strip()
48 if k == "OS": 48 if eres.endswith(k) or eres.endswith(k+';') or eres.endswith(k+';OE'):
49 replies[k] = "16" 49 self._cnx.write("%s"%v)
50 break 50 if k == "OS":
51 self._cnx.write('\r') 51 replies[k] = "16"
52 break
53 self._cnx.write('\r')
52 if i > self._retries/2: 54 if i > self._retries/2:
53 time.sleep(self._timeout) 55 time.sleep(self._timeout)
54 continue 56 else:
55 res += l + "\n" 57 res += l + "\n"
56 i = 0 58 i = 0
57 return res 59 return res
58 60
59 if __name__ == '__main__': 61 if __name__ == '__main__':
60 import optparse 62 import optparse
61 opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)') 63 opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)')
89 else: 91 else:
90 outf = sys.stdout 92 outf = sys.stdout
91 93
92 94
93 try: 95 try:
94 plotter = GPIBplotter(device=options.device, address=options.address) 96 plotter = GPIBplotter(device=options.device, address=int(options.address),
97 timeout=0.06)
95 except (gpib.SerialException, gpib.ConnectionError), e: 98 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') 99 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') 100 sys.stderr.write('Check your parameters\n')
98 sys.exit(1) 101 sys.exit(1)
99 if options.verbose: 102 if options.verbose:

mercurial