#Sample Code: Get the current output value #Description: This script demonstrates how to get the current output value. #Language: Python import serial import time class PowerSupplyController: def __init__(self, port , baudrate=9600, timeout=1): self.port = port self.baudrate = baudrate self.timeout = timeout self.serial_conn = None def open_connection(self): """Open the serial port.""" self.serial_conn = serial.Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout) print(f"Connected to {self.port} at {self.baudrate} baudrate.") def close_connection(self): """Close the serial port.""" if self.serial_conn and self.serial_conn.is_open: self.serial_conn.close() print("Connection closed.") def send_command(self, command): """Send a command to the power supply.""" if self.serial_conn and self.serial_conn.is_open: self.serial_conn.write(command.encode("utf-8")) time.sleep(0.1) # Allow time for command processing # Read response if available if self.serial_conn.in_waiting > 0: response = self.serial_conn.readline().decode().strip() print(f"Response: {response}") return response return None else: print("Connection is not open.") return None def status(self): """Check the power supply status.""" # Note: Change the unit number (#1) strictly according to your device settings. command = "#1 STS\r\n" self.send_command(command) print(f"Command{command}") def set_remote(self): """Enable remote control mode.""" command = "#1 REN\r\n" self.send_command(command) def set_local(self): """Enable local control mode.""" command = "#1 GTL\r\n" self.send_command(command) def set_voltage(self, voltage): """Set the output voltage.""" command = f"#1 VSET {voltage:.2f}\r\n" self.send_command(command) def set_current(self, current): """Set the output current limit.""" command = f"#1 ISET {current:.2f}\r\n" self.send_command(command) def power_on(self): """Enable the output.""" command = "#1 SW1\r\n" self.send_command(command) def power_off(self): """Disable the output.""" command = "#1 SW0\r\n" self.send_command(command) def get_voltage(self): """Read the output voltage.""" command = "#1 VGET\r\n" return self.send_command(command) def get_current(self): """Read the output current.""" command = "#1 IGET\r\n" return self.send_command(command) # Main program if __name__ == "__main__": # Serial port configuration # Update 'port_name' based on your environment (e.g., 'COM3' for Windows, '/dev/ttyUSB0' for Linux) port_name = "COM3" # Initialize controller and open connection power_controller = PowerSupplyController(port=port_name) power_controller.open_connection() # Switch to remote mode power_controller.set_remote() # Configure output parameters power_controller.set_voltage(5.0) # Set voltage to 5.0 V power_controller.set_current(1.0) # Set current to 1.0 A # Enable output power_controller.power_on() time.sleep(1) # Stabilization time # Monitor output for 5 seconds print("Monitoring output...") for i in range(5): voltage = power_controller.get_voltage() current = power_controller.get_current() print(f"{i + 1}Seconds, Voltage: {voltage} V, Current: {current} A") time.sleep(1) #Wait for 1 second # Disable output power_controller.power_off() # Return to local mode power_controller.set_local() # Close connection power_controller.close_connection()