cpdctrl/cpdctrl/led_control_device/impl/thorlabs_dc2200.py
2025-03-19 17:01:33 +01:00

70 lines
2.3 KiB
Python

from ..base import LedControlDevice
from ...utility.visa import enumerate_devices
import logging
log = logging.getLogger(__name__)
import pyvisa
class DC2200(LedControlDevice):
"""
Class for controlling Thorlabs DC2200 LED controller.
This works only when the LED is connected to Terminal 2
Args:
instr (pyvisa.Resource): pyvisa resource object for the LED controller.
"""
def __init__(self, instr: pyvisa.Resource):
super().__init__()
self.instr = instr
# led presence test
self.instr.write('OUTPut:TERMinal2:TEST:INITiate')
presence = self.instr.query('OUTPut:TERMinal2:TEST:STATus?')
# Led name, format:
# "<vendor_name_string>,<led_head_model_name_string >, < led_head_serial_no_string >, < fw_version_major_num >, < fw_version_minor_num >, < fw_version_subminor_num > "
self.led_name = self.instr.query('SYSTem:TERMinal2:HTYPe?').strip("\n")
# constant brightness
self.instr.write('SOURCE1:MODE CB')
# turn off
self.instr.write(f'SOURCE1:CBRightness:BRIGhtness 0')
self.instr.write('OUTPUT1:STATE ON')
for _ in range(10): # print max 10 errors
error = self.instr.query('SYSTem:ERRor?')
if error.startswith('+0'): break
log.error(f"DC2200 device error during initialization: {error}")
def __del__(self):
self.off()
self.instr.close()
def test_connection(self) -> None:
try:
res = self.instr.query('*IDN?')
except Exception as e:
log.error(f"Failed to query *IDN?:", e)
raise ConnectionError
def on(self):
self.instr.write(f'SOURCE1:CBRightness:BRIGhtness 100')
def off(self):
self.instr.write(f'SOURCE1:CBRightness:BRIGhtness 0')
def set_level(self, level:int):
self.instr.write(f'SOURCE1:CBRightness:BRIGhtness {level}')
@staticmethod
def enumerate_devices(query="(GPIB)|(USB)?*:INSTR"):
return enumerate_devices("DC2200", query)
@staticmethod
def connect_device(name):
rm = pyvisa.ResourceManager()
instr = rm.open_resource(name)
return DC2200(instr)
def get_led_name(self) -> None|str:
return self.led_name
def __str__(self):
return "Thorlabs DC2200"