From c95f2405896516c19d3db25dae66180fd4fb20e4 Mon Sep 17 00:00:00 2001 From: Anik Stark Date: Wed, 18 Mar 2026 11:22:19 +0100 Subject: [PATCH] frappy_psi.fungen: tested and adapted --- frappy_psi/fungen.py | 139 +++++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 57 deletions(-) diff --git a/frappy_psi/fungen.py b/frappy_psi/fungen.py index 3e8b637d..821853c7 100644 --- a/frappy_psi/fungen.py +++ b/frappy_psi/fungen.py @@ -20,7 +20,7 @@ import re from frappy.core import StringIO, HasIO, Writable, Parameter, FloatRange, EnumType, \ - IDLE, WARN, DISABLED + IDLE, WARN, nopoll class IO(StringIO): @@ -42,22 +42,94 @@ class Frequency(HasIO, Writable): mode = Parameter('device mode', datatype=EnumType(continuous=0, burst=1, off=2), readonly=False) _prev_mode = '' + def parse_reply(self, reply): + match = re.match(r'"([A-Z]*) (.*),(.*),(.*)"', reply) + self.function = match.group(1) + self.value = float(match.group(2)) + self.voltage = float(match.group(3)) + self.offset = float(match.group(4)) + def read_value(self): reply = self.communicate('APPL?') - vals = re.split(r'[ ,]+', reply) - self.function = vals[0] - self.value = float(vals[1]) - self.voltage = float(vals[2]) - self.offset = float(vals[3]) + self.parse_reply(reply) return self.value + @nopoll def read_target(self): return self.read_value() def write_target(self, target): - cmd = f'APPL:{int(self.function)} {target} HZ, {self.voltage:.1f} VPP, {self.offset:.1f}' - self.communicate(cmd) - return self.read_target() + cmd = f'APPL:{self.function.name} {float(target)} HZ, {float(self.voltage)} VPP, {float(self.offset)}' + reply = self.communicate(f'{cmd}\nAPPL?') + self.parse_reply(reply) + return self.value + + def write_function(self, function): + self.function = function + self.write_target(self.target) + self.read_value() + + def write_voltage(self, voltage): + self.voltage = voltage + self.write_target(self.target) + self.read_value() + + def write_offset(self, offset): + self.offset = offset + self.write_target(self.target) + self.read_value() + + def read_width(self): + return float(self.communicate('FUNC:PULS:WIDT?')) + + def write_width(self, width): + self.communicate(f'FUNC:PULS:WIDT {width}') + return self.read_width() + + def read_burstcycles(self): + return int(float(self.communicate('BURS:NCYC?'))) + + def write_burstcycles(self, burstcycles): + self.communicate(f'BURS:NCYC {int(burstcycles)}') + return self.read_burstcycles() + + def read_burstperiod(self): + return float(self.communicate('BURS:INT:PER?')) + + def write_burstperiod(self, burstperiod): + self.communicate(f'BURS:INT:PER {burstperiod}') + return self.read_burstperiod() + + def read_mode(self): + reply = self.communicate('OUTP?') + if reply == '0': + return 'off' + reply = self.communicate('BURS:STAT?') + if reply == 'ON': + return 'burst' + return 'continuous' + + def write_mode(self, mode): + if mode == 'off': + reply = self.communicate('OUTP OFF;OUTP?') + if reply == '0': + self.status = WARN, 'device is turned off' + else: + self.status = WARN, 'error when turning device off' + if mode == 'burst': + reply = self.communicate('OUTP ON;BURS:STAT ON\nBURS:STAT?') + if reply == '1': + self.status = IDLE, 'burst mode' + else: + self.status = WARN, 'error when turning burst mode on' + if mode == 'continuous': + reply = self.communicate('OUTP ON;BURS:STAT OFF\nBURS:STAT?') + if reply == '0': + self.status = IDLE, 'continuous mode' + else: + self.status = WARN, 'error when turning continuous mode on' + self._prev_mode = mode + return mode # def read_function(self): # return self.communicate('FUNC?') @@ -80,51 +152,4 @@ class Frequency(HasIO, Writable): # def write_offset(self, offset): # self.communicate(f'VOLT:OFFS {offset:.1f}') # return self.read_offset() - - def read_width(self): - return float(self.communicate('FUNC:PULS:WIDT?')) - - def write_width(self, width): - self.communicate(f'FUNC:PULS:WIDT {width}') - return self.read_width() - - def read_burstcycles(self): - return int(self.communicate('BURS:NCYC?')) - - def write_burstcycles(self, burstcycles): - self.communicate(f'BURS:NCYC {int(burstcycles)}') - return self.read_burstcycles() - - def read_burstperiod(self): - return float(self.communicate('BURS:INT:PER')) - - def write_burstperiod(self, burstperiod): - self.communicate(f'BURS:INT:PER {burstperiod}') - return self.read_burstperiod() - - def write_mode(self, mode): - if mode == 'off': - reply = self.communicate('OUTP OFF\nOUTP?') - if reply == 'OFF': - self.status = DISABLED, 'device is turned off' - else: - self.status = WARN, 'error when turning device off' - if self._prev_mode == 'off' and (mode != 'off'): - reply = self.communicate('OUTP ON\nOUTP?') - if reply != 'ON': - self.status = WARN, 'error when turning device on' - # try again ? - if mode == 'burst': - reply = self.communicate('BURS:STAT ON') - if reply == 'ON': - self.status = IDLE, 'burst mode' - else: - self.status = WARN, 'error when turning burst mode on' - if mode == 'continuous': - reply = self.communicate('BURS:STAT OFF') - if reply == 'OFF': - self.status = IDLE, 'continuous mode' - else: - self.status = WARN, 'error when turning continuous mode on' - self._prev_mode = mode - return mode + \ No newline at end of file