#!/usr/bin/env python3 """ Test script for channel control functionality. """ from tektronix_base import TektronixOscilloscopeBase def main(): """Test channel control functions on oscilloscope at 192.168.10.105""" scope_ip = "192.168.10.105" print(f"Connecting to oscilloscope at {scope_ip}...") scope = TektronixOscilloscopeBase(resource_name=scope_ip) try: # Connect to scope scope.connect() print(f"Connected to {scope.identify()}\n") # Test with CH1 test_channel = 1 print(f"=== Testing Channel {test_channel} ===\n") # Query all channel parameters print("Querying all channel parameters...") all_params = scope.query_channel(test_channel) print(f"CH{test_channel} parameters: {all_params[:100]}...\n") # Get current settings to restore later print("Getting current channel settings...") current_bandwidth = scope.get_channel_bandwidth(test_channel) current_coupling = scope.get_channel_coupling(test_channel) current_termination = scope.get_channel_termination(test_channel) current_scale = scope.get_channel_scale(test_channel) current_offset = scope.get_channel_offset(test_channel) current_position = scope.get_channel_position(test_channel) current_label_name = scope.get_channel_label_name(test_channel) current_label_color = scope.get_channel_label_color(test_channel) current_label_font_size = scope.get_channel_label_font_size(test_channel) current_label_font_type = scope.get_channel_label_font_type(test_channel) current_label_xpos = scope.get_channel_label_xpos(test_channel) current_label_ypos = scope.get_channel_label_ypos(test_channel) print(f" Current bandwidth: {current_bandwidth}") print(f" Current coupling: {current_coupling}") print(f" Current termination: {current_termination} ohms") print(f" Current scale: {current_scale} V/div") print(f" Current offset: {current_offset} V") print(f" Current position: {current_position} divisions") print(f" Current label name: {current_label_name}") print(f" Current label color: {current_label_color}") print(f" Current label font size: {current_label_font_size} pt") print(f" Current label font type: {current_label_font_type}") print(f" Current label X position: {current_label_xpos} px") print(f" Current label Y position: {current_label_ypos} px\n") # Test coupling modes print("Testing coupling modes:") for coupling in ['DC', 'AC']: print(f" Setting coupling to {coupling}...") scope.set_channel_coupling(test_channel, coupling) actual = scope.get_channel_coupling(test_channel) print(f" Actual coupling: {actual}") # Test termination print("\nTesting termination settings:") for term in [50, 1000000]: print(f" Setting termination to {term} ohms...") scope.set_channel_termination(test_channel, term) actual = scope.get_channel_termination(test_channel) print(f" Actual termination: {actual} ohms") # Test vertical scale print("\nTesting vertical scale:") test_scales = [0.1, 0.5, 1.0, 2.0] for scale in test_scales: print(f" Setting scale to {scale} V/div...") scope.set_channel_scale(test_channel, scale) actual = scope.get_channel_scale(test_channel) print(f" Actual scale: {actual} V/div") # Test vertical offset print("\nTesting vertical offset:") test_offsets = [0.0, 0.5, -0.5, 1.0] for offset in test_offsets: print(f" Setting offset to {offset} V...") scope.set_channel_offset(test_channel, offset) actual = scope.get_channel_offset(test_channel) print(f" Actual offset: {actual} V") # Test vertical position print("\nTesting vertical position:") test_positions = [0.0, 1.0, -1.0, 2.5] for position in test_positions: print(f" Setting position to {position} divisions...") scope.set_channel_position(test_channel, position) actual = scope.get_channel_position(test_channel) print(f" Actual position: {actual} divisions") # Test label name print("\nTesting label name:") test_names = ["Test Signal", "CH1-Custom", "Probe Input"] for name in test_names: print(f" Setting label to '{name}'...") scope.set_channel_label_name(test_channel, name) actual = scope.get_channel_label_name(test_channel) print(f" Actual label: {actual}") # Test label color print("\nTesting label color:") test_colors = ["#FF0000", "#00FF00", "#0000FF", "#FFFF00"] for color in test_colors: print(f" Setting color to {color}...") scope.set_channel_label_color(test_channel, color) actual = scope.get_channel_label_color(test_channel) print(f" Actual color: {actual}") # Test label font size print("\nTesting label font size:") test_sizes = [10, 12, 14, 16] for size in test_sizes: print(f" Setting font size to {size} pt...") scope.set_channel_label_font_size(test_channel, size) actual = scope.get_channel_label_font_size(test_channel) print(f" Actual font size: {actual} pt") # Test label font type print("\nTesting label font type:") test_fonts = ["Arial", "Helvetica", "Courier"] for font in test_fonts: print(f" Setting font to {font}...") scope.set_channel_label_font_type(test_channel, font) actual = scope.get_channel_label_font_type(test_channel) print(f" Actual font: {actual}") # Test label position print("\nTesting label X position:") test_xpos = [100, 200, 300] for xpos in test_xpos: print(f" Setting X position to {xpos} px...") scope.set_channel_label_xpos(test_channel, xpos) actual = scope.get_channel_label_xpos(test_channel) print(f" Actual X position: {actual} px") print("\nTesting label Y position:") test_ypos = [50, 100, 150] for ypos in test_ypos: print(f" Setting Y position to {ypos} px...") scope.set_channel_label_ypos(test_channel, ypos) actual = scope.get_channel_label_ypos(test_channel) print(f" Actual Y position: {actual} px") # Test using string channel format print("\nTesting with string channel format ('CH2'):") print(" Setting CH2 coupling to AC...") scope.set_channel_coupling('CH2', 'AC') actual = scope.get_channel_coupling('CH2') print(f" Actual CH2 coupling: {actual}") print(" Setting CH2 scale to 0.5 V/div...") scope.set_channel_scale('CH2', 0.5) actual = scope.get_channel_scale('CH2') print(f" Actual CH2 scale: {actual} V/div") # Restore original settings print(f"\nRestoring original settings for CH{test_channel}...") scope.set_channel_coupling(test_channel, current_coupling) scope.set_channel_termination(test_channel, current_termination) scope.set_channel_scale(test_channel, current_scale) scope.set_channel_offset(test_channel, current_offset) scope.set_channel_position(test_channel, current_position) scope.set_channel_label_name(test_channel, current_label_name) scope.set_channel_label_color(test_channel, current_label_color) scope.set_channel_label_font_size(test_channel, current_label_font_size) scope.set_channel_label_font_type(test_channel, current_label_font_type) scope.set_channel_label_xpos(test_channel, current_label_xpos) scope.set_channel_label_ypos(test_channel, current_label_ypos) print(f" Restored coupling: {scope.get_channel_coupling(test_channel)}") print(f" Restored termination: {scope.get_channel_termination(test_channel)} ohms") print(f" Restored scale: {scope.get_channel_scale(test_channel)} V/div") # Test invalid inputs print("\n=== Testing Error Handling ===\n") print("Testing invalid channel number (should raise ValueError)...") try: scope.get_channel_coupling(5) print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid channel string (should raise ValueError)...") try: scope.get_channel_coupling('CH5') print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid coupling (should raise ValueError)...") try: scope.set_channel_coupling(1, 'INVALID') print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid termination (should raise ValueError)...") try: scope.set_channel_termination(1, 75) print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid scale (should raise ValueError)...") try: scope.set_channel_scale(1, -1.0) print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid color format (should raise ValueError)...") try: scope.set_channel_label_color(1, "FF0000") # Missing # print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid font size (should raise ValueError)...") try: scope.set_channel_label_font_size(1, -5) print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\nTesting invalid X position (should raise ValueError)...") try: scope.set_channel_label_xpos(1, -10) print(" ERROR: Should have raised ValueError!") except ValueError as e: print(f" Correctly raised ValueError: {e}") print("\n=== All tests completed successfully! ===") except Exception as e: print(f"Error: {type(e).__name__}: {e}") import traceback traceback.print_exc() finally: if scope.is_connected: scope.disconnect() print("\nDisconnected from oscilloscope") if __name__ == "__main__": main()