""" Test 01: SDK Initialization and Device Discovery 测试SDK初始化和设备发现功能 This script tests: 1. Loading the Andor SDK3 DLL 2. Initializing the SDK library 3. Getting the number of connected cameras 4. Finalizing the SDK library """ import ctypes import os import sys # Constants from atcore.h AT_SUCCESS = 0 AT_HANDLE_SYSTEM = 1 AT_ERR_NOTINITIALISED = 1 AT_ERR_DEVICENOTFOUND = 39 # DLL path - relative to script location SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) LIBS_PATH = os.path.join(SCRIPT_DIR, "libs") DLL_NAME = "atcore.dll" def test_init_and_discover(): """Test SDK initialization and device discovery.""" print("=" * 60) print("Test 01: SDK Initialization and Device Discovery") print("=" * 60) print() # Step 1: Load DLL print("[1/4] Loading Andor SDK DLL...") try: # Add libs directory to DLL search path if not os.path.exists(LIBS_PATH): print(f"❌ Error: libs folder not found at {LIBS_PATH}") return False # For Windows, add the directory to DLL search path if sys.platform == 'win32': if hasattr(os, 'add_dll_directory'): # Python 3.8+ os.add_dll_directory(LIBS_PATH) else: # Python 3.7 and earlier os.environ['PATH'] = LIBS_PATH + os.pathsep + os.environ.get('PATH', '') dll_path = os.path.join(LIBS_PATH, DLL_NAME) if not os.path.exists(dll_path): print(f"❌ Error: DLL not found at {dll_path}") return False atcore = ctypes.cdll.LoadLibrary(dll_path) print(f"✓ DLL loaded successfully: {dll_path}") except Exception as e: print(f"❌ Error loading DLL: {e}") return False print() # Step 2: Initialize SDK print("[2/4] Initializing SDK library...") try: ret = atcore.AT_InitialiseLibrary() if ret != AT_SUCCESS: print(f"❌ Failed to initialize library, error code: {ret}") return False print("✓ SDK library initialized successfully") except Exception as e: print(f"❌ Error during initialization: {e}") return False print() # Step 3: Get device count print("[3/4] Detecting connected cameras...") try: device_count = ctypes.c_longlong(0) # Use ctypes.create_unicode_buffer for proper wide string handling feature_name = ctypes.create_unicode_buffer("Device Count") ret = atcore.AT_GetInt( AT_HANDLE_SYSTEM, feature_name, ctypes.byref(device_count) ) if ret != AT_SUCCESS: print(f"❌ Failed to get device count, error code: {ret}") else: print(f"✓ Device count retrieved successfully") print(f" Number of cameras detected: {device_count.value}") if device_count.value == 0: print(" ⚠ Warning: No cameras detected!") elif device_count.value > 0: print(f" ✓ Found {device_count.value} camera(s)") except Exception as e: print(f"❌ Error getting device count: {e}") device_count = None print() # Step 4: Finalize SDK print("[4/4] Finalizing SDK library...") try: ret = atcore.AT_FinaliseLibrary() if ret != AT_SUCCESS: print(f"❌ Failed to finalize library, error code: {ret}") return False print("✓ SDK library finalized successfully") except Exception as e: print(f"❌ Error during finalization: {e}") return False print() print("=" * 60) # Summary if device_count is not None and device_count.value > 0: print("✓ TEST PASSED: SDK initialization and device discovery successful") print(f" {device_count.value} camera(s) detected") return True elif device_count is not None and device_count.value == 0: print("⚠ TEST COMPLETED WITH WARNING: SDK works but no cameras detected") print(" This is normal if no camera is connected") return True else: print("❌ TEST FAILED: Could not complete device discovery") return False if __name__ == "__main__": success = test_init_and_discover() sys.exit(0 if success else 1)