This commit is contained in:
2025-12-04 16:45:29 +08:00
parent 3376c7fa1d
commit 125c897e85

View File

@@ -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 ctypes
import os 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
# Load DLL
dll_path = os.path.join(os.path.dirname(__file__), "libs", "atcore.dll")
atcore = ctypes.cdll.LoadLibrary(dll_path) atcore = ctypes.cdll.LoadLibrary(dll_path)
# Define function signatures to match C++ API # Constants
# int AT_InitialiseLibrary() AT_SUCCESS = 0
atcore.AT_InitialiseLibrary.argtypes = [] AT_HANDLE_SYSTEM = 1
atcore.AT_InitialiseLibrary.restype = ctypes.c_int
# int AT_FinaliseLibrary() print("Initializing library...")
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() ret = atcore.AT_InitialiseLibrary()
if ret != AT_SUCCESS: if ret != AT_SUCCESS:
print(f"❌ Failed to initialize library, error code: {ret}") print(f"ERROR: Initialize failed, code: {ret}")
return False exit(1)
print("✓ SDK library initialized successfully") print("Initialize OK")
except Exception as e:
print(f"❌ Error during initialization: {e}")
return False
print() # Get device count
# Step 3: Get device count
print("[3/4] Detecting connected cameras...")
try:
device_count = ctypes.c_longlong(0) 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))
ret = atcore.AT_GetInt(
AT_HANDLE_SYSTEM,
"Device Count",
ctypes.byref(device_count)
)
if ret != AT_SUCCESS: if ret != AT_SUCCESS:
print(f"❌ Failed to get device count, error code: {ret}") print(f"ERROR: Get device count failed, code: {ret}")
else: else:
print(f"Device count retrieved successfully") print(f"Device count: {device_count.value}")
print(f" Number of cameras detected: {device_count.value}")
if device_count.value == 0: # Finalize
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() ret = atcore.AT_FinaliseLibrary()
if ret != AT_SUCCESS: if ret != AT_SUCCESS:
print(f"❌ Failed to finalize library, error code: {ret}") print(f"ERROR: Finalize failed, 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: else:
print("❌ TEST FAILED: Could not complete device discovery") print("Finalize OK")
return False
if __name__ == "__main__":
success = test_init_and_discover()
sys.exit(0 if success else 1)