1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
"""
provide basic class to operate SCPI capable instruments
"""
import re
def response2numStr(strIn, firstSeparator=None, unit=None):
# Often an instrument reply is in the form 'TDIV 2.00E-08S' (for example Siglent Scope)
# i.e. "<prefix><firstSeparator><numberString><unit>
# prefix='TDIV', firstSeparator=' ', numberString='2.00E-08', unit='S'
# this function parses the reply
if firstSeparator is None or firstSeparator == '':
return (strIn, None, None)
spltStr = re.split(firstSeparator, strIn)
prefix = spltStr[0]
rstr = spltStr[1]
if unit is not None and unit != '':
spltStr = re.split(unit, rstr)
numberString = spltStr[0]
unit = spltStr[1]
else:
numberString = rstr
return (prefix, numberString, unit)
class SCPI_PROPERTY(property):
"""
Overrides property class and makes it suitable for SCPI set and query notation.
Works within SCPIinstr class since it assumes that owner has query() and write().
scpi_prfx - SCPI command prefix to get/set property, for example 'FreqInt'
query formed as 'scpi_prfx?' and setter as 'scpi_prfx val'
ptype - property type 'str', 'int', 'float', ...
doc - short description of property, for example 'Internal lockin frequency'
no_getter - typical for some sort of command
no_setter - typical for measured values (i.e. external to the instrument)
"""
def __init__(self, scpi_prfx=None, ptype=str, doc=None, no_getter=False, no_setter=False):
self.no_getter = no_getter
self.no_setter = no_setter
if no_getter:
fget=None
else:
fget = self.get_scpi
if no_setter:
fset=None
else:
fset = self.set_scpi
super().__init__(fget=fget, fset=fset)
self.scpi_prfx = scpi_prfx
self.ptype = ptype
self.__doc__ = doc
def __set_name__(self, owner, name):
self.public_name = name
self.private_name = '_' + name
def get_scpi(self, owner):
return self.ptype( owner.query(f'{self.scpi_prfx}?') )
def set_scpi(self, owner, val):
return owner.write(f'{self.scpi_prfx} {val}')
def __repr__(self):
sargs= []
sargs.append( f'scpi_prfx={self.scpi_prfx}')
sargs.append( f'ptype={self.ptype}')
sargs.append( f'doc={self.__doc__}')
sargs.append( f'no_getter={self.no_getter}')
sargs.append( f'no_setter={self.no_setter}')
sargs =', '.join(sargs)
s = ''.join( [ f'{self.__class__.__name__}(' , sargs, ')' ] )
return s
class SCPIinstr:
""" Basic class which support SCPI commands """
"""
Do not instantiate directly, use
rm = pyvisa.ResourceManager()
SCPIinstr(rm.open_resource('TCPIP::192.168.0.2::INSTR'))
"""
def __init__(self, resource):
self.resource = resource
# convenience pyvisa functions
self.write = self.resource.write
self.read = self.resource.read
self.query = self.resource.query
self.read_bytes = self.resource.read_bytes
self.read_binary_values = self.resource.read_binary_values
self.query_binary_values = self.resource.query_binary_values
@property
def idn(self):
return self.query("*IDN?")
def clear_status(self):
self.write("*CLS")
def set_event_status_enable(self):
self.write("*ESE")
def query_event_status_enable(self):
return self.query("*ESE?")
def query_event_status_register(self):
return self.query("*ESR?")
def set_wait_until_finished(self):
self.write("*OPC")
def wait_until_finished(self):
return self.query("*OPC?")
def reset(self):
self.write("*RST")
def set_service_request_enable(self):
self.write("*SRE")
def query_service_request_enable(self):
return self.query("*SRE?")
def query_status_byte(self):
return self.query("*STB?")
def self_test_result(self):
return self.query("*TST?")
def wait(self):
self.write("*WAI")
if __name__ == '__main__':
from qolab.hardware.basic import BasicInstrument
class DummyInstrument(BasicInstrument):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# in order to check SCPI_PROPERTY we need to implement write and query
def write(self, str):
print(f'write: {str=}')
def query(self, str):
print(f'query: {str=}')
return '123'
x = SCPI_PROPERTY(scpi_prfx='SETX', ptype=str, doc='property X')
y = SCPI_PROPERTY(scpi_prfx='SETY', ptype=int, no_setter=True, doc='property Y')
z = SCPI_PROPERTY(scpi_prfx='SETY', ptype=int, no_getter=True, doc='property Z')
c1= DummyInstrument()
c1.deviceProperties=['x', 'y']
c1.getConfig()
c2= DummyInstrument()
|