frappy/frappy_psi/bridge.py
Oksana Shliakhtun 4f9372066a New driver for AC Resistance Bridge (with SIM921 modules)
Change-Id: I2c2da421453af0f41703805092423f9b02d1f9b4
2024-04-23 17:14:36 +02:00

120 lines
3.2 KiB
Python

# *****************************************************************************
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Module authors: Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
# *****************************************************************************
import re
from frappy.core import HasIO, Readable, Drivable, Parameter, FloatRange, IntRange, EnumType, \
Enum
class BridgeIO(HasIO):
end_of_line = b'\r' # should be <lf> or <cr>
identification = [('*IDN?', r'Stanford_Research_Systems,.*')]
def comm(self, port, cmd):
self.communicate(f'conn {port}')
return self.communicate(f'send {port}, "{cmd}\r"')
def set_par(self, cmd, *args):
head = ','.join([cmd] + [a if isinstance(a, str) else f'{a:g}' for a in args])
tail = cmd.replace(' ', '? ')
new_tail = re.sub(r'[0-9.]+', '', tail)
reply = self.comm(f'{head};{new_tail}')
result = []
for num in reply.split(','):
try:
result.append(float(num))
except ValueError:
result.append(num)
if len(result) == 1:
return result[0]
return result
def get_par(self, cmd):
reply = self.comm(cmd)
result = []
for num in reply.split(','):
try:
result.append(float(num))
except ValueError:
result.append(num)
if len(result) == 1:
return result[0]
return result
class SIM921(BridgeIO, Readable, Drivable):
#port = Parameter()
value = Parameter('temperature', unit='K')
setpoint = Parameter('temperature deviation', datatype=FloatRange, unit='K')
resistance = Parameter('resistance', datatype=FloatRange, unit='ohm')
irange = Parameter('resistance range index', IntRange)
phase = Parameter('phase', FloatRange, unit='ang')
tc = Parameter('time constant', FloatRange)
autorange = Parameter('autorange_on', EnumType('autorange', off=0, soft=1, hard=2),
readonly=False, default=0)
ioClass = BridgeIO
def read_resistance(self):
return self.get_par('rval')
def read_value(self):
return self.get_par('tval')
def read_phase(self):
return self.get_par('phase')
def read_setpoint(self):
return self.get_par('tset')
def write_setpoint(self, setpoint):
return self.comm(f'tset {setpoint}')
def read_tc(self):
return self.get_par('tcon')
def write_tc(self, tc):
return self.set_par(f'tcon {tc}')