diff --git a/ps2000.py b/ps2000.py index d41936f..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"): @@ -396,27 +408,32 @@ def print_info(ps): # print('set voltage %f %f' % (ps.set_voltage(12.34), ps.get_voltage_setpoint())) # ps.get_actual(True) - if __name__ == "__main__": parser = argparse.ArgumentParser(description='Control PS2000 power supply') parser.add_argument( '-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: + ps.set_voltage_and_current(args.voltage, args.current) if args.on: print("turning on") @@ -436,5 +453,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']}")