#Sample Code: Open the port and check the power supply status #Description: This script demonstrates how to open the port and check the power status. #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 and print the response.""" if self.serial_conn and self.serial_conn.is_open: # Encode command to bytes and write to serial port self.serial_conn.write(command.encode("utf-8")) # Wait briefly for command processing time.sleep(0.1) # Read response response = self.serial_conn.readline().decode().strip() print(f"Response: {response}") return response else: print("Connection is not open.") return None def status(self): """Check the power supply status.""" # Note: Update the unit number (#1) to match your device configuration. command = "#1 STS\r\n" self.send_command(command) print(f"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" # Create an instance and open the connection power_controller = PowerSupplyController(port=port_name) power_controller.open_connection() # Send status check command power_controller.status() # Close the connection power_controller.close_connection()