From dca78f8b2ab00ba33bdf20391a93ed5aa216fdae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=83=D0=BD=D1=8F=D0=B4?= =?UTF-8?q?=D0=B0=D0=BC=D0=B1=D0=B8=D0=B7?= <5822148+ivannunyadambiz@users.noreply.github.com> Date: Thu, 10 Oct 2024 10:31:34 +0200 Subject: [PATCH 1/2] Update ps2000.py added arguments for voltage and current, removed required=True for the group (this lets you change the current or voltage without any other action), fixed help message for info argument --- ps2000.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/ps2000.py b/ps2000.py index d41936f..405dd73 100644 --- a/ps2000.py +++ b/ps2000.py @@ -395,6 +395,15 @@ def print_info(ps): ps.get_actual(True) # print('set voltage %f %f' % (ps.set_voltage(12.34), ps.get_voltage_setpoint())) # ps.get_actual(True) + +def set_voltage_and_current(ps, voltage, current): + if voltage is not None: + print(f"Setting voltage to {voltage}V") + ps.set_voltage(voltage) + if current is not None: + print(f"Setting current to {current}A") + ps.set_current(current) + print(f"New setpoints - Voltage: {ps.get_voltage_setpoint()}V, Current: {ps.get_current_setpoint()}A") if __name__ == "__main__": @@ -403,20 +412,26 @@ def print_info(ps): '-p', '--port', type=str, help='serial port to use', required=True) parser.add_argument('-v', '--verbose', action='store_true') - group = parser.add_mutually_exclusive_group(required=True) + group = parser.add_mutually_exclusive_group() group.add_argument('--on', help='turn on', action='store_true') group.add_argument('--off', help='turn off', action='store_true') group.add_argument('--toggle', help='toggle', action='store_true') - group.add_argument('--info', help='toggle', action='store_true') + group.add_argument('--info', help='show device info', action='store_true') + + parser.add_argument('--voltage', type=float, help='set voltage in volts') + parser.add_argument('--current', type=float, help='set current in amperes') + args = parser.parse_args() with ps2000(args.port) as ps: - if args.verbose: - print("Vset: {}".format(ps.get_voltage_setpoint())) - print("Iset: {}".format(ps.get_current_setpoint())) - print("Vact: {}".format(ps.get_actual()['v'])) - print("Iact: {}".format(ps.get_actual()['i'])) + print(f"Vset: {ps.get_voltage_setpoint()}") + print(f"Iset: {ps.get_current_setpoint()}") + print(f"Vact: {ps.get_actual()['v']}") + print(f"Iact: {ps.get_actual()['i']}") + + if args.voltage is not None or args.current is not None: + set_voltage_and_current(ps, args.voltage, args.current) if args.on: print("turning on") @@ -436,5 +451,5 @@ def print_info(ps): if args.verbose: time.sleep(1) - print("Vact: {}".format(ps.get_actual()['v'])) - print("Iact: {}".format(ps.get_actual()['i'])) + print(f"Vact: {ps.get_actual()['v']}") + print(f"Iact: {ps.get_actual()['i']}") From 3cbb54bee543357cd342c5a1075254fffa633dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=9D=D1=83=D0=BD=D1=8F=D0=B4?= =?UTF-8?q?=D0=B0=D0=BC=D0=B1=D0=B8=D0=B7?= <5822148+ivannunyadambiz@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:28:43 +0200 Subject: [PATCH 2/2] Update ps2000.py don't take this as my final commit. i'm going to have another go at this tomorrow when i'm in the office. I think I found a possible problem or two elsewhere and would like to test and if required, fix it --- ps2000.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/ps2000.py b/ps2000.py index 405dd73..5b9893b 100644 --- a/ps2000.py +++ b/ps2000.py @@ -368,6 +368,18 @@ def get_setpoints(self): actual['i'] = self.i_nom * ((ans[4] << 8) + ans[5]) / 25600 return actual + + def set_voltage_and_current(self, voltage, current): + if voltage is not None: + if self.verbose: + print(f"Setting voltage to {voltage}V") + self.set_voltage(voltage) + if current is not None: + if self.verbose: + print(f"Setting current to {current}A") + self.set_current(current) + if self.verbose: + print(f"New setpoints - Voltage: {self.get_voltage_setpoint()}V, Current: {self.get_current_setpoint()}A") def check_available(port, target="PS 2042"): @@ -395,16 +407,6 @@ def print_info(ps): ps.get_actual(True) # print('set voltage %f %f' % (ps.set_voltage(12.34), ps.get_voltage_setpoint())) # ps.get_actual(True) - -def set_voltage_and_current(ps, voltage, current): - if voltage is not None: - print(f"Setting voltage to {voltage}V") - ps.set_voltage(voltage) - if current is not None: - print(f"Setting current to {current}A") - ps.set_current(current) - print(f"New setpoints - Voltage: {ps.get_voltage_setpoint()}V, Current: {ps.get_current_setpoint()}A") - if __name__ == "__main__": parser = argparse.ArgumentParser(description='Control PS2000 power supply') @@ -431,7 +433,7 @@ def set_voltage_and_current(ps, voltage, current): print(f"Iact: {ps.get_actual()['i']}") if args.voltage is not None or args.current is not None: - set_voltage_and_current(ps, args.voltage, args.current) + ps.set_voltage_and_current(args.voltage, args.current) if args.on: print("turning on")