From 125c897e8580cb94163f7c4ec57e3fec33aa66fd Mon Sep 17 00:00:00 2001 From: chuan Date: Thu, 4 Dec 2025 16:45:29 +0800 Subject: [PATCH] 1 --- andor-test/01_init_and_discover.py | 168 +++++------------------------ 1 file changed, 24 insertions(+), 144 deletions(-) diff --git a/andor-test/01_init_and_discover.py b/andor-test/01_init_and_discover.py index ff393ec..d7eeddf 100644 --- a/andor-test/01_init_and_discover.py +++ b/andor-test/01_init_and_discover.py @@ -1,152 +1,32 @@ -""" -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 +# Load DLL +dll_path = os.path.join(os.path.dirname(__file__), "libs", "atcore.dll") +atcore = ctypes.cdll.LoadLibrary(dll_path) + +# Constants 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" +print("Initializing library...") +ret = atcore.AT_InitialiseLibrary() +if ret != AT_SUCCESS: + print(f"ERROR: Initialize failed, code: {ret}") + exit(1) +print("Initialize OK") +# Get device count +device_count = ctypes.c_longlong(0) +ret = atcore.AT_GetInt(AT_HANDLE_SYSTEM, "Device Count", ctypes.byref(device_count)) +if ret != AT_SUCCESS: + print(f"ERROR: Get device count failed, code: {ret}") +else: + print(f"Device count: {device_count.value}") -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) - - # Define function signatures to match C++ API - # int AT_InitialiseLibrary() - atcore.AT_InitialiseLibrary.argtypes = [] - atcore.AT_InitialiseLibrary.restype = ctypes.c_int - - # int AT_FinaliseLibrary() - atcore.AT_FinaliseLibrary.argtypes = [] - atcore.AT_FinaliseLibrary.restype = ctypes.c_int - - # int AT_GetInt(AT_H Hndl, const AT_WC* Feature, AT_64* Value) - atcore.AT_GetInt.argtypes = [ctypes.c_int, ctypes.c_wchar_p, ctypes.POINTER(ctypes.c_longlong)] - atcore.AT_GetInt.restype = ctypes.c_int - - 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 wide string like C++ L"Device Count" - ret = atcore.AT_GetInt( - AT_HANDLE_SYSTEM, - "Device Count", - 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) +# Finalize +ret = atcore.AT_FinaliseLibrary() +if ret != AT_SUCCESS: + print(f"ERROR: Finalize failed, code: {ret}") +else: + print("Finalize OK")