Files
navigate/andor-test/01_init_and_discover.py
2025-12-04 17:17:09 +08:00

169 lines
5.3 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Andor SDK3 Camera Discovery and Connection Test
Direct SDK usage without pylablib dependency
"""
import ctypes
import os
import sys
# Load the Andor SDK3 library
try:
# Try common installation paths
sdk_paths = [
r"C:\Program Files\Andor SOLIS\atcore.dll",
r"C:\Program Files\Andor SDK3\atcore.dll",
"atcore.dll" # Try system PATH
]
lib = None
for path in sdk_paths:
if os.path.exists(path) or path == "atcore.dll":
try:
lib = ctypes.WinDLL(path)
print(f" Loaded SDK from: {path}")
break
except OSError:
continue
if lib is None:
raise OSError("Could not find atcore.dll. Please install Andor SDK3 or Andor SOLIS.")
except Exception as e:
print(f" Failed to load Andor SDK3 library: {e}")
sys.exit(1)
# Define function signatures
lib.AT_InitialiseLibrary.restype = ctypes.c_int
lib.AT_InitialiseLibrary.argtypes = []
lib.AT_FinaliseLibrary.restype = ctypes.c_int
lib.AT_FinaliseLibrary.argtypes = []
lib.AT_Open.restype = ctypes.c_int
lib.AT_Open.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
lib.AT_Close.restype = ctypes.c_int
lib.AT_Close.argtypes = [ctypes.c_int]
lib.AT_GetInt.restype = ctypes.c_int
lib.AT_GetInt.argtypes = [ctypes.c_int, ctypes.c_wchar_p, ctypes.POINTER(ctypes.c_longlong)]
lib.AT_GetString.restype = ctypes.c_int
lib.AT_GetString.argtypes = [ctypes.c_int, ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_int]
lib.AT_GetStringMaxLength.restype = ctypes.c_int
lib.AT_GetStringMaxLength.argtypes = [ctypes.c_int, ctypes.c_wchar_p, ctypes.POINTER(ctypes.c_int)]
# Error checking
def check_error(code, func_name):
error_names = {
0: "SUCCESS",
1: "NOT_INITIALISED",
2: "NOT_IMPLEMENTED",
12: "INVALID_HANDLE",
39: "DEVICE_NOT_FOUND"
}
if code != 0:
error = error_names.get(code, f"UNKNOWN({code})")
raise RuntimeError(f"{func_name} failed with error: {error}")
# Helper functions
def get_int_feature(handle, feature):
value = ctypes.c_longlong()
code = lib.AT_GetInt(handle, feature, ctypes.byref(value))
check_error(code, f"AT_GetInt({feature})")
return value.value
def get_string_feature(handle, feature):
max_len = ctypes.c_int()
code = lib.AT_GetStringMaxLength(handle, feature, ctypes.byref(max_len))
check_error(code, f"AT_GetStringMaxLength({feature})")
buffer = ctypes.create_unicode_buffer(max_len.value)
code = lib.AT_GetString(handle, feature, buffer, max_len.value)
check_error(code, f"AT_GetString({feature})")
return buffer.value
# Main test code
print("=" * 60)
print("Andor SDK3 Camera Discovery Test")
print("=" * 60)
# Initialize library
print("\n[1] Initializing Andor SDK3 library...")
code = lib.AT_InitialiseLibrary()
check_error(code, "AT_InitialiseLibrary")
print(" Library initialized successfully")
try:
# Get number of cameras (using system handle = 1)
print("\n[2] Detecting cameras...")
num_cameras = get_int_feature(1, "DeviceCount")
print(f" Found {num_cameras} camera(s)")
if num_cameras == 0:
print("\n<EFBFBD> No cameras detected. Please check:")
print(" - Camera is powered on")
print(" - USB/PCIe connection is secure")
print(" - Drivers are installed correctly")
else:
# Open first camera
print(f"\n[3] Opening camera index 0...")
handle = ctypes.c_int()
code = lib.AT_Open(0, ctypes.byref(handle))
check_error(code, "AT_Open")
print(f" Camera opened (handle: {handle.value})")
try:
# Get camera information
print("\n[4] Reading camera information...")
print("-" * 60)
camera_name = get_string_feature(handle.value, "CameraName")
print(f" Camera Name : {camera_name}")
camera_model = get_string_feature(handle.value, "CameraModel")
print(f" Camera Model : {camera_model}")
serial_number = get_string_feature(handle.value, "SerialNumber")
print(f" Serial Number : {serial_number}")
firmware_ver = get_string_feature(handle.value, "FirmwareVersion")
print(f" Firmware Ver : {firmware_ver}")
# Additional info
try:
sensor_width = get_int_feature(handle.value, "SensorWidth")
sensor_height = get_int_feature(handle.value, "SensorHeight")
print(f" Sensor Size : {sensor_width} x {sensor_height} pixels")
except:
pass
try:
sensor_temp = lib.AT_GetFloat(handle.value, "SensorTemperature")
# Note: would need to define AT_GetFloat properly for this
except:
pass
print("-" * 60)
print(" Camera information retrieved successfully")
finally:
# Close camera
print("\n[5] Closing camera...")
code = lib.AT_Close(handle.value)
check_error(code, "AT_Close")
print(" Camera closed")
finally:
# Finalize library
print("\n[6] Shutting down SDK library...")
code = lib.AT_FinaliseLibrary()
check_error(code, "AT_FinaliseLibrary")
print(" Library shutdown complete")
print("\n" + "=" * 60)
print("Test completed successfully!")
print("=" * 60)