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 # 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_SUCCESS = 0
AT_HANDLE_SYSTEM = 1 AT_HANDLE_SYSTEM = 1
AT_ERR_NOTINITIALISED = 1
AT_ERR_DEVICENOTFOUND = 39
# DLL path - relative to script location print("Initializing library...")
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) ret = atcore.AT_InitialiseLibrary()
LIBS_PATH = os.path.join(SCRIPT_DIR, "libs") if ret != AT_SUCCESS:
DLL_NAME = "atcore.dll" 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(): # Finalize
"""Test SDK initialization and device discovery.""" ret = atcore.AT_FinaliseLibrary()
if ret != AT_SUCCESS:
print("=" * 60) print(f"ERROR: Finalize failed, code: {ret}")
print("Test 01: SDK Initialization and Device Discovery") else:
print("=" * 60) print("Finalize OK")
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)