gpib_plotter.py

changeset 12
a04bea92c509
parent 8
aa3d55f78563
child 27
0f8f2621418f
equal deleted inserted replaced
11:3ccb0023cf41 12:a04bea92c509
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 import gpib
6 from prologix import GPIB
7 class GPIBplotter(GPIB):
6 _replies={ 8 _replies={
7 "OE": "0", 9 "OE": "0",
8 "OH": "0,0,10000,7500", 10 "OH": "0,0,10000,7500",
9 "OI": "7470A", 11 "OI": "7470A",
10 "OP": "0,0,10000,7500", 12 "OP": "0,0,10000,7500",
11 "OO": "0,1,0,0,0,0,0,0", 13 "OO": "0,1,0,0,0,0,0,0",
12 "OF": "40,40", 14 "OF": "40,40",
13 "OS": "24", 15 "OS": "24",
14 } 16 }
15 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, 17 def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1,
16 address=0): 18 address=5):
17 super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0) 19 super(GPIBplotter, self).__init__(device, baudrate, timeout, address, mode=0)
18 20
19 def load_plot(self, wait_timeout=0): 21 def load_plot(self, wait_timeout=0):
20 """ 22 """
21 Make a full plot process. 23 Make a full plot process.
33 if wait_timeout<0: 35 if wait_timeout<0:
34 raise ValueError 36 raise ValueError
35 37
36 self._cnx.timeout = wait_timeout 38 self._cnx.timeout = wait_timeout
37 firstloop = True 39 firstloop = True
40 newdata = False
38 while i<self._retries: 41 while i<self._retries:
39 l = self._cnx.readline().strip() 42 l = self._cnx.readline().strip()
40 if firstloop: 43 if firstloop:
41 self._cnx.timeout = self._timeout 44 self._cnx.timeout = self._timeout
42 firstloop = False 45 firstloop = False
43 if l == "": 46 if l == "":
44 i += 1 47 if i == 0:# > (self._retries/2):
45 if i == (self._retries-1): 48 # ie. we just received new stuffs (i is reset in the else block)
46 for k, v in replies.items(): 49 for k, v in replies.items():
47 eres = res.strip() 50 # check wether we should reply smthg
51 eres = res.replace('\n', '').strip()
48 if eres.endswith(k) or eres.endswith(k+';') or eres.endswith(k+';OE'): 52 if eres.endswith(k) or eres.endswith(k+';') or eres.endswith(k+';OE'):
49 self._cnx.write("%s"%v) 53 self._cnx.write("%s"%v)
50 if k == "OS": 54 if k == "OS":
51 replies[k] = "16" 55 replies[k] = "16"
52 break 56 break
53 self._cnx.write('\r') 57 self._cnx.write('\r')
54 if i > self._retries/2: 58 #time.sleep(0.1)
55 time.sleep(self._timeout) 59 i += 1
56 else: 60 else:
57 res += l + "\n" 61 if not res:
62 print "Plotting..."
63 res += l + '\n'
58 i = 0 64 i = 0
65 #time.sleep(0.1)
66 if res:
67 print "DONE (received %d characters)"%len(res)
59 return res 68 return res
60 69
61 if __name__ == '__main__': 70 if __name__ == '__main__':
62 import optparse 71 import optparse
63 opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)') 72 opt = optparse.OptionParser('A simple HP7470A GPIB plotter emulator for USB-GPIB bundle (ProLogix)')
103 sys.stderr.write('connection established\n') 112 sys.stderr.write('connection established\n')
104 113
105 loop = True 114 loop = True
106 nloop = 0 115 nloop = 0
107 while loop: 116 while loop:
108 plot = plotter.load_plot(wait_timeout=1) 117 plot = plotter.load_plot(wait_timeout=0.1)
109 if options.verbose: 118 if options.verbose:
110 sys.stderr.write('.') 119 sys.stderr.write('.')
111 if plot: 120 if plot:
112 outf.write(plot) 121 outf.write(plot)
113 if options.verbose: 122 if options.verbose:

mercurial