comparison specan.py @ 31:c6c86dcb54ba

Add code to automate a sitesurvey (to some degree).
author Daniel O'Connor <darius@dons.net.au>
date Wed, 21 Sep 2011 15:00:24 +0930
parents
children 182c42e7bf03
comparison
equal deleted inserted replaced
30:9ce709b7da4b 31:c6c86dcb54ba
1 import exceptions
2 import numpy
3 import scpi
4
5 class Traceinst(object):
6 '''Generic class for a trace based instrument'''
7 attrs = {}
8 tracetypename = None
9 tracedtype = None
10 tracequery = None
11
12 def __init__(self, con):
13 self.con = con
14 # Set trace format
15 self.con.write("FORM " + self.tracetypename)
16
17 def setconf(self, name, value):
18 if name not in self.attrs:
19 raise exceptions.KeyError(name + " not supported")
20 # Check value is correct type
21 tmp = self.attrs[name][1](value)
22 # Run validation function (if necessary)
23 if self.attrs[name][2] != None:
24 self.attrs[name][2](value)
25 #print "Setting %s to %s" % (self.attrs[name][0], str(value))
26 self.con.write("%s %s" % (self.attrs[name][0], str(value)))
27
28 def getconf(self, name):
29 if name not in self.attrs:
30 raise exceptions.KeyError(name + " not supported")
31 self.con.write("%s?" % (self.attrs[name][0]))
32 r = self.con.read()
33 return self.attrs[name][1](r)
34
35 def write(self, *args):
36 return self.con.write(*args)
37
38 def read(self, *args):
39 return self.con.read(*args)
40
41 def gettrace(self, timeout = 10):
42 # Trigger the sweep
43 self.con.write("INIT;*WAI")
44
45 # Wait for it to be done
46 if False:
47 self.con.write("*OPC?")
48 opc = scpi.getdata(self.con.read(timeout), int)
49 if opc != 1:
50 return None
51 else:
52 while True:
53 self.con.write(':STATus:OPERation?')
54 i = scpi.getdata(self.con.read(timeout), int)
55 if i & 256:
56 break
57
58
59 # Grab trace data
60 self.con.write(self.tracequery)
61 dat = self.con.read(10)
62
63 # Parse into array
64 ary = scpi.bindecode(dat, dtype = self.tracedtype)
65 return ary
66
67 def dumpconf(self):
68 rtn = {}
69 for k in self.attrs:
70 self.con.write(self.attrs[k][0] + '?')
71 res = self.con.read()
72 #print "Getting " + k + " / " + self.attrs[k][0] + " = " + res
73 rtn[k] = self.attrs[k][1](res)
74 return rtn
75
76 class RSSPA(Traceinst):
77 '''Rhode & Schwartz Spectrum Analyzer'''
78
79 attrs = { 'fstart' : ['FREQ:START', float, None], # Page 561
80 'fstop' : ['FREQ:STOP', float, None],
81 'atten' : ['INP:ATT', float, None], # Page 518
82 'resbw' : ['SENSE:BANDWIDTH:RESOLUTION', float, None], # Page 539
83 'vidbw' : ['SENSE:BANDWIDTH:VIDEO', float, None], # Page 541
84 'sweept' : ['SENSE:SWEEP:TIME', float, None], # Page 599
85 #'sweeppts' : ['SWEEP:POINTS', int, RSSPA.sweepptscheck],
86 'sweeppts' : ['SWEEP:POINTS', int, None], # Page 599
87 'sweepcnt' : ['SENSE1:AVERAGE:COUNT', int, None], # Page 595
88 'reflev' : ['DISPLAY:WINDOW1:TRACE1:Y:SCALE:RLEVEL', float, None], # Page 506
89 'detector' : ['SENSE1:DETECTOR1:FUNCTION', str, None] , # Page 552
90 }
91
92 tracetypename = 'REAL,32'
93 tracedtype = numpy.float32
94 tracequery = 'TRAC1? TRACE1'
95 swptslist = [125, 251, 501, 1001, 2001, 4001, 8001]
96
97 # def sweepptscheck(npts):
98 # if x not in RSSPA.swptslist:
99 # raise exceptions.ValueError("Sweep value not supported, must be one of " + str(RSSPA.swptslist))
100
101
102 class AnSPA(Traceinst):
103 '''Anritsu Spectrum Analyzer'''
104 attrs = { 'fstart' : ['FREQ:START', float, None],
105 'fstop' : ['FREQ:STOP', float, None],
106 'atten' : ['SENSE:POWER:ATTENUATION', float, None],
107 'sweept' : ['SENSE:SWEEP:TIME', float, None],
108 'sweepcnt' : [':SENSe:AVERage:COUNt', int, None],
109 'tracemode' : [':SENSe:AVERage:TYPE', str, None],
110 'resbw' : ['SENSE:BANDWIDTH:RESOLUTION', float, None],
111 'vidbw' : ['SENSE:BANDWIDTH:VIDEO', float, None],
112 'reflev' : [':DISPLAY:WIND:TRACE:Y:SCALE:RLEVEL', float, None],
113 'detector' : [':SENSe:DETector:FUNCtion', str, None],
114 }
115
116 tracetypename = 'REAL,32'
117 tracedtype = numpy.float32
118 tracequery = 'TRACE:DATA?'
119
120 def getInst(inst):
121 if inst == "RSSPA":
122 return RSSPA
123 elif inst == "AnSPA":
124 return AnSPA
125 else:
126 raise exceptions.NotImplementedError("unknown instrument type " + inst)