#!/opt/srasenv/bin/python3 """ Test script for Genesis laser serial communication. Tests basic connectivity and I2C protocol without GUI. """ import sys import time import serial # Constants NXP_START_BYTE = 0x53 NXP_STOP_BYTE = 0x50 ADDR_PCA9555_PS_GLUE_OUT = 0x4a ADDR_ADS7828 = 0x90 CHAN_CURRENT_ACTUAL = 0x84 def test_serial_connection(port_name="/dev/ttyUSB1", baudrate=9600): """Test basic serial connection.""" print(f"Testing serial connection to {port_name} @ {baudrate}...") try: port = serial.Serial( port=port_name, baudrate=baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1.0 ) print("✓ Serial port opened successfully") return port except Exception as e: print(f"✗ Failed to open serial port: {e}") return None def nxp_write(port, i2c_addr_write, cmd, data, data_len): """Build and send NXP I2C write packet.""" # Determine command length if cmd <= 0xFF: cmd_bytes = bytes([cmd]) else: cmd_bytes = cmd.to_bytes(2, 'big') # Convert data to bytes (big-endian) data_bytes = data.to_bytes(data_len, 'big') # Calculate total length total_len = len(cmd_bytes) + len(data_bytes) # Build packet: [0x53][addr][len][cmd...][data...][0x50] packet = bytes([ NXP_START_BYTE, i2c_addr_write, total_len ]) + cmd_bytes + data_bytes + bytes([NXP_STOP_BYTE]) print(f" TX: {packet.hex(' ')}") port.write(packet) return True def nxp_read(port, i2c_addr_write, cmd, cmd_len, data_len): """Build and send NXP I2C read packet.""" # Convert command to bytes if cmd_len == 1: cmd_bytes = bytes([cmd]) else: cmd_bytes = cmd.to_bytes(2, 'big') # Build packet: [0x53][addr][cmd_len][cmd...][0x53][addr|0x01][data_len][0x50] packet = bytes([ NXP_START_BYTE, i2c_addr_write, cmd_len ]) + cmd_bytes + bytes([ NXP_START_BYTE, i2c_addr_write | 0x01, # Read address data_len, NXP_STOP_BYTE ]) print(f" TX: {packet.hex(' ')}") port.write(packet) time.sleep(0.1) # Read response response = port.read(data_len) if response: print(f" RX: {response.hex(' ')} ({len(response)} bytes)") return int.from_bytes(response, 'big') else: print(f" RX: (no data)") return None def test_read_ps_glue_out(port): """Test reading PS glue output port.""" print("\nTest 1: Reading PS glue output port (0x4a, reg 0x02)...") value = nxp_read(port, ADDR_PCA9555_PS_GLUE_OUT, 0x02, 1, 1) if value is not None: print(f"✓ PS Glue Out status: 0x{value:02x}") print(f" Shutter: {'OPEN' if value & 0x01 else 'CLOSED'}") print(f" Current Mode: {'ON' if value & 0x04 else 'OFF'}") print(f" Remote Enable: {'ON' if value & 0x08 else 'OFF'}") print(f" Analog Enable: {'ON' if value & 0x10 else 'OFF'}") print(f" Keyswitch: {'ON' if value & 0x20 else 'OFF'}") return True else: print("✗ Failed to read PS glue out") return False def test_read_current_adc(port): """Test reading current ADC.""" print("\nTest 2: Reading current ADC (ADS7828, channel 0x84)...") value = nxp_read(port, ADDR_ADS7828, CHAN_CURRENT_ACTUAL, 1, 2) if value is not None: print(f"✓ Current ADC raw value: 0x{value:04x} ({value})") scaled = value * 0.000244140625 print(f" Scaled value: {scaled:.6f}") return True else: print("✗ Failed to read current ADC") return False def test_write_current_zero(port): """Test setting current to zero.""" print("\nTest 3: Setting current to 0 (X9119 @ 0x52, cmd 0xa0)...") success = nxp_write(port, 0x52, 0xa0, 0, 2) if success: print("✓ Current set to 0 command sent") return True else: print("✗ Failed to set current") return False def test_read_status_bits(port): """Test reading all status bits from PS glue output.""" print("\nTest 4: Reading all control status bits...") # Read current state current_state = nxp_read(port, ADDR_PCA9555_PS_GLUE_OUT, 0x02, 1, 1) if current_state is None: print("✗ Failed to read current state") return False print(f" Current state: 0x{current_state:02x}") print(f" Note: Shutter bit status: {'SET' if current_state & 0x01 else 'CLEAR'} (manual shutter - not controlled)") print("✓ Successfully read all status bits") return True def main(): """Run all tests.""" print("=" * 60) print("Genesis SLM MX 532 - Serial Communication Test") print("=" * 60) # Open serial port port = test_serial_connection() if not port: print("\nTest FAILED: Cannot open serial port") return 1 try: # Run tests tests_passed = 0 tests_total = 4 if test_read_ps_glue_out(port): tests_passed += 1 if test_read_current_adc(port): tests_passed += 1 if test_write_current_zero(port): tests_passed += 1 if test_read_status_bits(port): tests_passed += 1 # Summary print("\n" + "=" * 60) print(f"Test Summary: {tests_passed}/{tests_total} tests passed") print("=" * 60) if tests_passed == tests_total: print("✓ All tests PASSED - Communication working!") print("\nYou can now run the GUI application:") print(" ./genesis_laser_gui.py") return 0 else: print("✗ Some tests FAILED - Check connections") return 1 finally: port.close() print("\nSerial port closed.") if __name__ == '__main__': sys.exit(main())