memoryfolder

メモです

Andor python wrapper

It is from ages ago, but I think this is for iXon EMCCD camera

iXon EMCCD Cameras | Ultimate Speed & Sensitivity - Andor - Oxford Instruments

The code below is also in my git repo for DMD fast feedback program used with quantum gas microscope.
GitHub - shinich1/DMDControl

I hope this helps someone working on hardware interface task in their PhD.

make sure you load from correct dll, especially look out for the ones with correct system bit (atmcd32d.dll or atmcd64.dll)!

from ctypes import *
import numpy as np
import matplotlib.pyplot as plt

DRV_SUCCESS = 20002
DRV_VXDNOTINSTALLED = 20003

dll = windll.LoadLibrary("C:/Program Files/Andor SOLIS/Drivers/atmcd32d.dll")
c_long_p=POINTER(c_long)
c_uint_p=POINTER(c_uint)
c_int_p=POINTER(c_int)
c_float_p=POINTER(c_float)
c_long_arr = c_long*262144
c_long_arrp = POINTER(c_long_arr)


dll.Initialize.argtypes=[c_char_p]
dll.Initialize.restype=c_uint
dll.GetAcquiredData.argtypes=[c_long_p,c_ulong]
dll.GetAcquiredData.restype=c_uint
dll.ShutDown.restype=c_uint
dll.GetNumberADChannels.argtypes=[c_int_p]
dll.GetNumberADChannels.restype=c_uint
dll.GetHSSpeed.restype=c_uint
dll.GetHSSpeed.argtypes=[c_int,c_int,c_int,c_float_p]
dll.GetDetector.restype=c_uint
dll.GetDetector.argtypes=[c_int_p,c_int_p]
#dll.GetNewData.argtypes = [c_long_arrp, c_int]
#dll.GetNewData.restype=c_uint
dll.WaitForAcquisition.restype=c_uint
dll.AbortAcquisition.restype=c_uint
dll.PrepareAcquisition.restype=c_uint
dll.SetShutter.restype=c_uint
dll.SetShutter.argtypes = [c_int, c_int,c_int,c_int]
dll.StartAcquisition.restype=c_uint
dll.SendSoftwareTrigger.restype=c_uint
dll.SetImage.restype=c_uint
dll.SetImage.argtypes = [c_int, c_int,c_int,c_int,c_int,c_int]
dll.GetAcquisitionTimings.argtypes=[c_float_p,c_float_p,c_float_p]
dll.GetAcquisitionTimings.restype=c_uint
dll.SetTriggerMode.restype=c_uint
dll.SetTriggerMode.argtypes=[c_int]

dll.SetExposureTime.argtypes=[c_float]
dll.SetExposureTime.restype=c_uint
dll.GetFastestRecommendedVSSpeed.argtypes=[c_int_p,c_float_p]
dll.GetFastestRecommendedVSSpeed.restype=c_uint
dll.GetNewData.argtypes=[c_long_p,c_long]
dll.GetNewData.restype=c_uint
cdll.msvcrt.malloc.restype=c_long_p

dll.SetAcquisitionMode.restype=c_uint
dll.SetAcquisitionMode.argtypes=[c_int]
dll.IsTriggerModeAvailable.argtypes=[c_int]
dll.IsTriggerModeAvailable.restype=c_uint

dll.SetExposureTime.restype=c_uint
dll.SetExposureTime.argtypes=[c_float]
dll.SetVSSpeed.restype=c_uint
dll.SetVSSpeed.argtypes=[c_int]
dll.GetNumberVSSpeeds.restype=c_uint
dll.GetNumberVSSpeeds.argtypes=[c_int_p]

##
dll.SetEMGainMode.restypes=c_uint
dll.SetEMGainMode.argtypes=[c_int]
dll.SetEMCCDGain.restypes=c_uint
dll.SetEMCCDGain.argtypes=[c_int]
dll.GetEMCCDGain.restypes=c_uint
dll.GetEMCCDGain.argtypes=[c_int_p]

dll.SetTemperature.argtypes=[c_int]
dll.SetTemperature.restype=c_int
dll.GetTemperature.argtypes=[c_int_p]
dll.GetTemperature.restype=c_int
dll.IsCoolerOn.argtypes=[c_int_p]
dll.CoolerON.restype=c_int
dll.CoolerOFF.restype=c_int

dll.GetBaselineClamp.restypes=c_int
dll.GetBaselineClamp.argtypes=[c_int_p]
dll.SetBaselineClamp.restypes=c_uint
dll.SetBaselineClamp.argtypes=[c_int]
dll.SetBaselineOffset.restypes=c_int
dll.SetBaselineOffset.argtypes=[c_int]

dll.AbortAcquisition.restypes=c_int

dll.GetBackground.argtypes=[c_long_p,c_long]
dll.GetBackground.restype=c_uint


class Andor(object):
    def __init__(self, **kwargs):
        path = "."
        c_s             =   c_char_p(path)
        self._iAD       =   c_int(0)
        self._nAD       =   c_int(0)
        self._nAD_p     =   c_int_p(self._nAD)
        self._HSS       =   c_float(0)
        self._HSS_p     =   c_float_p(self._HSS)
        self._giSize    =   c_int(992*992)
        self._extime    =   c_float(0.1)
        self._extime_p  =   c_float_p(self._extime)
        self._acc       =   c_float(0)
        self._acc_p     =   c_float_p(self._acc)
        self._kin       =   c_float(0)
        self._kin_p     =   c_float_p(self._kin)
        self._trgmode   =   c_int(10)   # trgmode10 : Software Trigger
        self._acqmode   =   c_int(1)    # acqmode=1  : Single Shot
        self._VSSpeed   =   c_int(0)
        self._EMGmode   =   c_int(2)
        self._EMGmode_p =   c_int_p(self._EMGmode)
        self._EMGain    =   c_int(10)
        self._EMGain_p  =   c_int_p(self._EMGain)
        self._gblXpixels = c_int(992)
        self._gblYpixels = c_int(992)
        self._image = np.zeros((992,992),dtype=int)
        
        self._camtemperature    = c_int(0)
        self._camtemperature_p  = c_int_p(self._camtemperature)
        self._coolerstate       = c_int(1)
        self._coolerstate_p     = c_int_p(self._coolerstate)
        self._setpoint          = -70

        self._clampstate    = c_int(0)
        self._clampstate_p  = c_int_p(self._clampstate)


        """initialize"""
        err=dll.Initialize(c_s)
        if err==DRV_SUCCESS:
            print "initialization success"
        else:
            print "initialization error %d"%(err)
            

        """PrepareAcquisition"""
        err=dll.PrepareAcquisition()
        if err==DRV_SUCCESS:
            print "PrepareAcquisition success"
        else:
            print "PrepareAcquisition error %d"%(err)
        

        """SetShutter"""
        err=dll.SetShutter(c_int(1),c_int(1),c_int(0),c_int(0))
        if err==DRV_SUCCESS:
            print "SetShutter success"
        else:
            print "SetShutter ERROR %d"%(err)
            

        """getADnumber"""
        err=dll.GetNumberADChannels(self._nAD_p)
        print "nAD %d"%(self._nAD.value)
        

        """getHSSpeed"""
        err=dll.GetHSSpeed(self._iAD,c_int(0),c_int(1),self._HSS_p)
        print "HSSpeed %f"%(self._HSS.value)
        

        """SetAcquisitionMode"""
        err=dll.SetAcquisitionMode(self._acqmode)
        if err==DRV_SUCCESS:
            print "Set to acquire Single Shot mode"
        else:
            print "SetAcquisitionMode error %d"%(err)

        """SetTriggerMode"""
        err=dll.SetTriggerMode(self._trgmode)
        if err==DRV_SUCCESS:
            print "set to Sofware trigger mode"
        else:
            print "triggermode ERROR %d"%(err)

        """SetImage"""
        err=dll.SetImage(c_int(1),c_int(1),c_int(1),self._gblXpixels,c_int(1),self._gblYpixels);
        if err==DRV_SUCCESS:
            print "SetImage success"
        else:
            print "SetImage ERROR %d"%(err)

        """Set Cooler"""
        err=dll.CoolerON()
        err=dll.SetTemperature(self._setpoint)
        dll.GetTemperature(self._camtemperature_p)
        if err==DRV_SUCCESS:
            print "Set Temperature success"
        else:
            print "SetTemperature ERROR %d"%(err)

        """SetExposureTime"""
        err=dll.SetExposureTime(self._extime)
        if err==DRV_SUCCESS:
            print "SetExposureTime success"
        else:
            print "SetExposure ERROR %d"%(err)

        """SetVSSpeed"""
        dll.GetNumberVSSpeeds(c_int_p(self._VSSpeed))
        err=dll.SetVSSpeed(c_int(self._VSSpeed.value-2))
        if err==DRV_SUCCESS:
            print "setVS success %d"%(self._VSSpeed.value)
        else:
            print "setVS ERROR %d"%(err)

        """SetEMGainMode"""
        err=dll.SetEMGainMode(self._EMGmode)
        if err==DRV_SUCCESS:
            print "SetEMGainMode liner"
        else:
            print "SetEMGainMode ERROR %d"%(err)

        """SetEMCCDGain"""
        err=dll.SetEMCCDGain(self._EMGain)
        if err==DRV_SUCCESS:
            print "SetEMCCDGain %d"%(self._EMGain.value)
        else:
            print "SetEMCCDGain ERROR %d"%(err)

        """GetAcquisitionTimings"""
        dll.GetAcquisitionTimings(self._extime_p,self._acc_p,self._kin_p);
        print "Exposure time %f s"%(self._extime.value)
        print "Accumulate %f"%(self._acc.value)
        print "Kinetic %f"%(self._kin.value)
        
        """SetBaselineClamp"""
        err=dll.SetBaselineClamp(self._clampstate)
        if err==DRV_SUCCESS:
            print "SetBaselineClamp success"
        else:
            print "SetBaselineClamp ERROR %d"%(err)

        #"""StartAcquisition"""
        #err=dll.StartAcquisition()
        #if err==DRV_SUCCESS:
        #    print "startAcquisition success"
        #else:
        #    print "startAcquisition error"

    def shutdown(self):
        """AbortAcquisition"""
        err = dll.AbortAcquisition()
        """shutdown"""
        err = dll.ShutDown()
        if err==DRV_SUCCESS:
        	print "shutdown success"
        else:
        	print "shutdown error"

    @property
    def exposuretime(self):
        return self._extime.value
    @exposuretime.setter
    def exposuretime(self,value):
        err=dll.SetExposureTime(c_float(value))
        if err==DRV_SUCCESS:
            self._extime=c_float(value)
        else:
            print "SetExposure ERROR %d"%(err)

    @property
    def setpoint(self):
        return self._setpoint
    @setpoint.setter
    def setpoint(self, value):
        err=dll.SetTemperature(c_int(value))
        if err==DRV_SUCCESS:
            self._setpoint=value
        else:
            print "Set setpoint ERROR %d"%(err)

    @property
    def cooler(self):
        dll.IsCoolerOn(self._coolerstate_p)
        return self._coolerstate.value
    @cooler.setter
    def cooler(self,state):
        if state:
            dll.CoolerON()
        else:
            dll.CoolerOFF()

    def readtemp(self):
        dll.AbortAcquisition()
        err=dll.GetTemperature(self._camtemperature_p)
        print err
        return {"temperature":self._camtemperature.value}

    @property
    def emgain(self):
        err=dll.GetEMCCDGain(self._EMGain_p)
        print err
        return self._EMGain.value
    @emgain.setter
    def emgain(self,value):
        err=dll.SetEMCCDGain(c_int(value))
        if err==DRV_SUCCESS:
            self._EMGain=c_int(value)
        else:
            print err

    @property
    def extime(self):
        err=dll.GetAcquisitionTimings(self._extime_p,self._acc_p,self._kin_p)
        #print err
        return self._extime.value
    @extime.setter
    def extime(self,value):
        err=dll.SetExposureTime(c_float(value))
        if err==DRV_SUCCESS:
            self._extime=c_float(value)
        else:
            print err
                                      
                
    def getimage(self):
        err=dll.StartAcquisition()
        if err==DRV_SUCCESS:
            dll.SendSoftwareTrigger()
            dll.WaitForAcquisition()
            dll.GetNewData(self._image.ctypes.data_as(c_long_p),self._giSize)
        else:
            print "startAcquisition error"

    def showimages(self,klen):
        for i in xrange(klen):
            self.getimage()
            plt.figure()
            plt.gray()
            plt.imshow(self._image)
            plt.colorbar()
        #plt.ion()
        plt.show()

    def settings(self):
        dll.AbortAcquisition()
        err=dll.GetTemperature(self._camtemperature_p)
        return {"iAD":self._iAD.value,"nAD":self._nAD.value,"HSS":self._HSS.value,
                "VSS":self._VSSpeed.value,"ExTime":self.extime,
                "TRG":self._trgmode.value,"ACQmode":self._acqmode.value,
                "EMGmode":self._EMGmode.value,"EMgain":self._EMGain.value,
                "temp":self._camtemperature.value}