Skip to content
Snippets Groups Projects
PilatusXM.py 59 KiB
Newer Older
#!/usr/bin/env python
# -*- coding:utf-8 -*-


# ############################################################################
#  license :
# ============================================================================
#
#
#  Project :     Interface class for the Pilatus detectors
#
# This file is part of Tango device class.
# 
# Tango is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Tango is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Tango.  If not, see <http://www.gnu.org/licenses/>.
# 
#
#  $Author :      sci.comp$
#
#  $Revision :    $
#
#  $Date :        $
#
#  $HeadUrl :     $
# ============================================================================
#            This file is generated by POGO
#     (Program Obviously used to Generate tango Object)
# ############################################################################

__all__ = ["PilatusXM", "PilatusXMClass", "main"]

__docformat__ = 'restructuredtext'

import PyTango
import sys
# Add additional import
#----- PROTECTED REGION ID(PilatusXM.additionnal_import) ENABLED START -----#
import socket
import traceback
import time
import os
END_MESSAGE = chr(0x18)
DBUG = False

class CommunicationClass():
 
    def __init__(self, ipaddr_in):
        self.ipaddr_in = ipaddr_in
        self.socket = None
        self._stop = False
        self.connect()
 
    def disconnect(self):
         try:
             if self.socket is not None:
                 self.socket.close()
         except:
             self.socket = None

    def connect(self):
         try:
             if self.socket is not None:
                 self.socket.close()
         except:
             self.socket = None
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.socket.settimeout(0.2)
         port = 41234
         host = self.ipaddr_in
         try:
             self.socket.connect((host,port))
         except:
             self.socket = None
         
    def send_to_detector(self, msg, wait_reply = True):
        # check state, Pilatus won't responde if it's RUNNING
        # only STOP command can be sent in this case
        self.socket.sendall(msg + END_MESSAGE)
        t0 = time.time()
        if DBUG:
            print ("Sent", msg)
        if not wait_reply:
            return
        if "CamSetup" in msg:
            mandatory_code = "2 OK"
        else:
            mandatory_code = ""
        try:
            reply = self.get_detector_reply(timeout = 2, wait_string = mandatory_code)
        except:
            reply = ''
        if reply is None:
            self.disconnect()
            return None
        return reply.split(END_MESSAGE)[0]

    def get_detector_reply(self, timeout = 2, wait_string = ''):
        t0 = time.time()
        reply = ''
        while (END_MESSAGE not in reply) or (wait_string not in reply):
            if (time.time() - t0) > timeout:
                break
            tmpreply = self.socket.recv(1024).strip()
            reply += tmpreply
        if DBUG:
            print ("Received", reply)
        if END_MESSAGE not in reply:
            reply = None
        return reply

class fakeAttr():

    def __init__(self):
        self.value = None

    def set_value(self,value_in):
        self.value = value_in

    def get_write_value(self):
        return self.value
        
#----- PROTECTED REGION END -----#    //    PilatusXM.additionnal_import

# Device States Description
# ON : The detecor is ready to take images.
# DISABLE : The device is disconnected from the camserver.
# RUNNING : An acquisition is running.
# FAULT : An error occured on the detector during an acquisition.


class PilatusXM (PyTango.LatestDeviceImpl):
    """Pilatus detectors are a series pixel detecors build by DECTRIS
    <br />
    http://www.dectris.com.
    <br />
    All detectors of this series can talk to the outside world via a socket
    connection. An ASCI protocol is used on this sockect connection to
    communicate with the detector.
    <p>
    The server process which handles the socket on the detecor PC is
    called camserver. Only one client can commumicate with camserver.
    If the native client tvx is connected, the device server cannot connect until
    tvx gets disconnected.
    </p>"""
    
    # -------- Add you global variables here --------------------------
    #----- PROTECTED REGION ID(PilatusXM.global_variables) ENABLED START -----#
    
    #----- PROTECTED REGION END -----#    //    PilatusXM.global_variables

    def __init__(self, cl, name):
        PyTango.LatestDeviceImpl.__init__(self,cl,name)
        self.debug_stream("In __init__()")
        PilatusXM.init_device(self)
        #----- PROTECTED REGION ID(PilatusXM.__init__) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.__init__
        
    def delete_device(self):
        self.debug_stream("In delete_device()")
        #----- PROTECTED REGION ID(PilatusXM.delete_device) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.delete_device

    def init_device(self):
        self.debug_stream("In init_device()")
        self.get_device_properties(self.get_device_class())
        self.attr_ExposureTime_read = 0.0
        self.attr_ExposurePeriod_read = 0.0
        self.attr_NbFrames_read = 0
        self.attr_NbExposures_read = 0
        self.attr_DelayTime_read = 0.0
        self.attr_ShutterEnable_read = False
        self.attr_TriggerMode_read = 0
        self.attr_UseRamDisk_read = False
        self.attr_FileDir_read = ""
        self.attr_FilePrefix_read = ""
        self.attr_FileStartNum_read = 0
        self.attr_FilePostfix_read = ""
        self.attr_LastImageTaken_read = ""
        self.attr_Energy_read = 0
        self.attr_Threshold_read = 0
        self.attr_Gain_read = 0
        self.attr_LastImagePath_read = ""
        self.attr_MxSettings_read = ""
        self.attr_GapFill_read = 0
        self.attr_LdBadPixMap_read = ""
        #----- PROTECTED REGION ID(PilatusXM.init_device) ENABLED START -----#
        self.attr_FilePrefix_read = 'test'
        self.attr_FilePostfix_read = '.tif'
        self.attr_FileStartNum_read = 0
        self.total_exp_time = 0
        self.start_time = 0
        if not self.Simulation:
            self.comm = CommunicationClass(self.ServerAddress)
        self.set_state(PyTango.DevState.ON)
        self.set_status("The device is in ON state. Ready")
        #----- PROTECTED REGION END -----#    //    PilatusXM.init_device

    def always_executed_hook(self):
        self.debug_stream("In always_excuted_hook()")
        #----- PROTECTED REGION ID(PilatusXM.always_executed_hook) ENABLED START -----#
        if self.Simulation:
            if self.get_state() == PyTango.DevState.RUNNING:
                elapsed_time = time.time() - self.start_time
                if elapsed_time > self.total_exp_time:
                    self.set_state(PyTango.DevState.ON)
                    self.set_status("The device is in ON state. Ready")
                    filenum = self.attr_FileStartNum_read + self.attr_NbFrames_read - 1
                    fullname = "%s%05d%s" % (self.attr_FilePrefix_read,filenum,self.attr_FilePostfix_read)
                    self.attr_LastImagePath_read = os.path.join(self.attr_FileDir_read,fullname)
                    self.attr_LastImageTaken_read = os.path.join(self.attr_FileDir_read,fullname)
            return        
        if self.get_state() in [PyTango.DevState.RUNNING,PyTango.DevState.INIT]:
            try:
                det_msg = self.comm.get_detector_reply(timeout=0.1)
            except:
                # Pilatus does not reply when acquiring
                # Let's stay in RUNNING or INIT
                return
        cmd = "CamSetup"
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","always_executed_hook")
        for token in reply.split("\n"):
            if "Camera state:" in token:
                if 'idle' in token:
                    self.set_state(PyTango.DevState.ON)
                    self.set_status("The device is in ON state. Ready")
                else:
                    self.set_state(PyTango.DevState.RUNNING)
                    self.set_status("The device is in RUNNING state.")
            elif "Last image: " in token:
                self.attr_LastImageTaken_read = token.split("Last image: ")[-1]
                self.attr_LastImagePath_read = os.path.dirname(self.attr_LastImageTaken_read)

        #----- PROTECTED REGION END -----#    //    PilatusXM.always_executed_hook

    # -------------------------------------------------------------------------
    #    PilatusXM read/write attribute methods
    # -------------------------------------------------------------------------
    
    def read_ExposureTime(self, attr):
        self.debug_stream("In read_ExposureTime()")
        #----- PROTECTED REGION ID(PilatusXM.ExposureTime_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "exptime"
            reply_cmd = "Exposure time set to: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_ExposureTime")
            elif reply_cmd in reply:
                self.attr_ExposureTime_read = float((reply.split(reply_cmd)[-1]).split(" ")[0])
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_ExposureTime")
        attr.set_value(self.attr_ExposureTime_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.ExposureTime_read
        
    def write_ExposureTime(self, attr):
        self.debug_stream("In write_ExposureTime()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.ExposureTime_write) ENABLED START -----#
        if self.Simulation:
            self.attr_ExposureTime_read = data
            return
        cmd = "exptime %g" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_ExposureTime")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_ExposureTime")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.ExposureTime_write
        
    def is_ExposureTime_allowed(self, attr):
        self.debug_stream("In is_ExposureTime_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING])
        #----- PROTECTED REGION ID(PilatusXM.is_ExposureTime_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_ExposureTime_allowed
        return state_ok
        
    def read_ExposurePeriod(self, attr):
        self.debug_stream("In read_ExposurePeriod()")
        #----- PROTECTED REGION ID(PilatusXM.ExposurePeriod_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "expperiod"
            reply_cmd = "Exposure period set to: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_ExposurePeriod")
            elif reply_cmd in reply:
                self.attr_ExposurePeriod_read = float((reply.split(reply_cmd)[-1]).split(" ")[0])
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_ExposurePeriod")
        attr.set_value(self.attr_ExposurePeriod_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.ExposurePeriod_read
        
    def write_ExposurePeriod(self, attr):
        self.debug_stream("In write_ExposurePeriod()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.ExposurePeriod_write) ENABLED START -----#
        if self.Simulation:
            self.attr_ExposurePeriod_read = data
            return
        cmd = "expperiod %g" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_ExposurePeriod")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_ExposurePeriod")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.ExposurePeriod_write
        
    def is_ExposurePeriod_allowed(self, attr):
        self.debug_stream("In is_ExposurePeriod_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING])
        #----- PROTECTED REGION ID(PilatusXM.is_ExposurePeriod_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_ExposurePeriod_allowed
        return state_ok
        
    def read_NbFrames(self, attr):
        self.debug_stream("In read_NbFrames()")
        #----- PROTECTED REGION ID(PilatusXM.NbFrames_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "nimages"
            reply_cmd = "N images set to: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_NbFrames")
            elif reply_cmd in reply:
                self.attr_NbFrames_read = int((reply.split(reply_cmd)[-1]).split(" ")[0])
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_NbFrames")
        attr.set_value(self.attr_NbFrames_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.NbFrames_read
        
    def write_NbFrames(self, attr):
        self.debug_stream("In write_NbFrames()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.NbFrames_write) ENABLED START -----#
        if self.Simulation:
            self.attr_NbFrames_read = data
            return
        cmd = "nimages %d" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_NbFrames")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_NbFrames")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.NbFrames_write
        
    def is_NbFrames_allowed(self, attr):
        self.debug_stream("In is_NbFrames_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_NbFrames_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_NbFrames_allowed
        return state_ok
        
    def read_NbExposures(self, attr):
        self.debug_stream("In read_NbExposures()")
        #----- PROTECTED REGION ID(PilatusXM.NbExposures_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "nexpframe"
            reply_cmd = "Exposures per frame set to: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_NbExposures")
            elif reply_cmd in reply:
                self.attr_NbExposures_read = int((reply.split(reply_cmd)[-1]).split(" ")[0])
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_NbExposures")
        attr.set_value(self.attr_NbExposures_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.NbExposures_read
        
    def write_NbExposures(self, attr):
        self.debug_stream("In write_NbExposures()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.NbExposures_write) ENABLED START -----#
        if self.Simulation:
            self.attr_NbExposures_read = data
            return
        cmd = "nexpframe %d" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_NbExposures")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_NbExposures")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.NbExposures_write
        
    def is_NbExposures_allowed(self, attr):
        self.debug_stream("In is_NbExposures_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_NbExposures_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_NbExposures_allowed
        return state_ok
        
    def read_DelayTime(self, attr):
        self.debug_stream("In read_DelayTime()")
        #----- PROTECTED REGION ID(PilatusXM.DelayTime_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "delay"
            reply_cmd = "Delay time set to: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_DelayTime")
            elif reply_cmd in reply:
                self.attr_DelayTime_read = float((reply.split(reply_cmd)[-1]).split(" ")[0])
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_DelayTime")
        attr.set_value(self.attr_DelayTime_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.DelayTime_read
        
    def write_DelayTime(self, attr):
        self.debug_stream("In write_DelayTime()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.DelayTime_write) ENABLED START -----#
        if self.Simulation:
            self.attr_DelayTime_read = data
            return
        cmd = "delay %g" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_DelayTime")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_DelayTime")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.DelayTime_write
        
    def is_DelayTime_allowed(self, attr):
        self.debug_stream("In is_DelayTime_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_DelayTime_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_DelayTime_allowed
        return state_ok
        
    def read_ShutterEnable(self, attr):
        self.debug_stream("In read_ShutterEnable()")
        #----- PROTECTED REGION ID(PilatusXM.ShutterEnable_read) ENABLED START -----#
        attr.set_value(self.attr_ShutterEnable_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.ShutterEnable_read
        
    def write_ShutterEnable(self, attr):
        self.debug_stream("In write_ShutterEnable()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.ShutterEnable_write) ENABLED START -----#
        if self.Simulation:
            self.attr_ShutterEnable_read = data
            return
        cmd = "ShutterEnable %g" % int(data)
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_ShutterEnable")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_ShutterEnable")
        self.attr_ShutterEnable_read = data

        #----- PROTECTED REGION END -----#    //    PilatusXM.ShutterEnable_write
        
    def is_ShutterEnable_allowed(self, attr):
        self.debug_stream("In is_ShutterEnable_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_ShutterEnable_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_ShutterEnable_allowed
        return state_ok
        
    def read_TriggerMode(self, attr):
        self.debug_stream("In read_TriggerMode()")
        #----- PROTECTED REGION ID(PilatusXM.TriggerMode_read) ENABLED START -----#
        attr.set_value(self.attr_TriggerMode_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.TriggerMode_read
        
    def write_TriggerMode(self, attr):
        self.debug_stream("In write_TriggerMode()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.TriggerMode_write) ENABLED START -----#
        self.attr_TriggerMode_read = data
        #----- PROTECTED REGION END -----#    //    PilatusXM.TriggerMode_write
        
    def is_TriggerMode_allowed(self, attr):
        self.debug_stream("In is_TriggerMode_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_TriggerMode_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_TriggerMode_allowed
        return state_ok
        
    def read_UseRamDisk(self, attr):
        self.debug_stream("In read_UseRamDisk()")
        #----- PROTECTED REGION ID(PilatusXM.UseRamDisk_read) ENABLED START -----#
        attr.set_value(self.attr_UseRamDisk_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.UseRamDisk_read
        
    def write_UseRamDisk(self, attr):
        self.debug_stream("In write_UseRamDisk()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.UseRamDisk_write) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.UseRamDisk_write
        
    def read_FileDir(self, attr):
        self.debug_stream("In read_FileDir()")
        #----- PROTECTED REGION ID(PilatusXM.FileDir_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "imgpath"
            reply_cmd = "OK "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_FileDir")
            elif reply_cmd in reply:
                self.attr_FileDir_read = (reply.split(reply_cmd)[-1]).split(" ")[0]
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_FileDir")
        attr.set_value(self.attr_FileDir_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.FileDir_read
        
    def write_FileDir(self, attr):
        self.debug_stream("In write_FileDir()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.FileDir_write) ENABLED START -----#
        if self.Simulation:
            self.attr_FileDir_read = data
            return
        cmd = "imgpath %s" % (data)
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_FileDir")
        tokens = reply.split(" ")
        if (len(tokens) < 2) or ("OK" not in tokens[1]):
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_FileDir")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.FileDir_write
        
    def is_FileDir_allowed(self, attr):
        self.debug_stream("In is_FileDir_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_FileDir_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_FileDir_allowed
        return state_ok
        
    def read_FilePrefix(self, attr):
        self.debug_stream("In read_FilePrefix()")
        #----- PROTECTED REGION ID(PilatusXM.FilePrefix_read) ENABLED START -----#
        attr.set_value(self.attr_FilePrefix_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.FilePrefix_read
        
    def write_FilePrefix(self, attr):
        self.debug_stream("In write_FilePrefix()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.FilePrefix_write) ENABLED START -----#
        self.attr_FilePrefix_read = data
        if len(data) and not data.endswith("_"):
            self.attr_FilePrefix_read += "_"
        #----- PROTECTED REGION END -----#    //    PilatusXM.FilePrefix_write
        
    def is_FilePrefix_allowed(self, attr):
        self.debug_stream("In is_FilePrefix_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_FilePrefix_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_FilePrefix_allowed
        return state_ok
        
    def read_FileStartNum(self, attr):
        self.debug_stream("In read_FileStartNum()")
        #----- PROTECTED REGION ID(PilatusXM.FileStartNum_read) ENABLED START -----#
        attr.set_value(self.attr_FileStartNum_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.FileStartNum_read
        
    def write_FileStartNum(self, attr):
        self.debug_stream("In write_FileStartNum()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.FileStartNum_write) ENABLED START -----#
        self.attr_FileStartNum_read = data
        #----- PROTECTED REGION END -----#    //    PilatusXM.FileStartNum_write
        
    def is_FileStartNum_allowed(self, attr):
        self.debug_stream("In is_FileStartNum_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.RUNNING])
        #----- PROTECTED REGION ID(PilatusXM.is_FileStartNum_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_FileStartNum_allowed
        return state_ok
        
    def read_FilePostfix(self, attr):
        self.debug_stream("In read_FilePostfix()")
        #----- PROTECTED REGION ID(PilatusXM.FilePostfix_read) ENABLED START -----#
        attr.set_value(self.attr_FilePostfix_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.FilePostfix_read
        
    def write_FilePostfix(self, attr):
        self.debug_stream("In write_FilePostfix()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.FilePostfix_write) ENABLED START -----#
        allowed_ext = ['tif','edf','img','cbf']
        for item in allowed_ext:
            if item in data:
                self.attr_FilePostfix_read = ".%s" % item
                return
        PyTango.Except.throw_exception("Command error","Unknown Pilatus postfix (.tif, .edf, .img, .cbf)","write_FilePostfix")
        #----- PROTECTED REGION END -----#    //    PilatusXM.FilePostfix_write
        
    def is_FilePostfix_allowed(self, attr):
        self.debug_stream("In is_FilePostfix_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_FilePostfix_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_FilePostfix_allowed
        return state_ok
        
    def read_LastImageTaken(self, attr):
        self.debug_stream("In read_LastImageTaken()")
        #----- PROTECTED REGION ID(PilatusXM.LastImageTaken_read) ENABLED START -----#
        attr.set_value(self.attr_LastImageTaken_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.LastImageTaken_read
        
    def read_Energy(self, attr):
        self.debug_stream("In read_Energy()")
        #----- PROTECTED REGION ID(PilatusXM.Energy_read) ENABLED START -----#
        attr.set_value(self.attr_Energy_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Energy_read
        
    def write_Energy(self, attr):
        self.debug_stream("In write_Energy()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.Energy_write) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Energy_write
        
    def is_Energy_allowed(self, attr):
        self.debug_stream("In is_Energy_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_Energy_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_Energy_allowed
        return state_ok
        
    def read_Threshold(self, attr):
        self.debug_stream("In read_Threshold()")
        #----- PROTECTED REGION ID(PilatusXM.Threshold_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "SetThreshold"
            reply_cmd = "threshold: "
            retries = 5
            while (retries > 0):
                reply = self.comm.send_to_detector(cmd)
                if reply is None:
                    PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_Threshold")
                elif reply_cmd in reply:
                    self.attr_Threshold_read = int((reply.split(reply_cmd)[-1]).split(" ")[0])
                    break
                elif "Threshold has not been set" in reply:
                    PyTango.Except.throw_exception("Pilatus error","Threshold has not been set","read_Threshold")
		retries -= 1
		time.sleep(0.2)
            if retries == 0:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_Threshold")
        attr.set_value(self.attr_Threshold_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Threshold_read
        
    def write_Threshold(self, attr):
        self.debug_stream("In write_Threshold()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.Threshold_write) ENABLED START -----#
        #
        # read the actual threshold and send command only if necessary
        #
        if self.Simulation:
            self.attr_Threshold_read = data
            return
        cmd = "SetThreshold"
        reply_cmd = "threshold: "
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_Threshold")
        elif reply_cmd in reply:
            self.attr_Threshold_read = int((reply.split(reply_cmd)[-1]).split(" ")[0])
        elif "Threshold has not been set" in reply:
            self.attr_Threshold_read = None
        else:
            PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","write_Threshold")
        if self.attr_Threshold_read == data:
            return
        cmd = "SetThreshold %d" % data
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_Threshold")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_Threshold")
        self.set_state(PyTango.DevState.INIT)
        self.set_status("The device is in INIT state.")
        
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Threshold_write
        
    def is_Threshold_allowed(self, attr):
        self.debug_stream("In is_Threshold_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_Threshold_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_Threshold_allowed
        return state_ok
        
    def read_Gain(self, attr):
        self.debug_stream("In read_Gain()")
        #----- PROTECTED REGION ID(PilatusXM.Gain_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "SetThreshold"
            reply_cmd = "Settings: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_Gain")
            elif reply_cmd in reply:
                gainstr = ((reply.split(reply_cmd)[-1]).split(";")[0])
                if "mid gain" in gainstr:
                    self.attr_Gain_read = 1
                elif  "low gain" in gainstr:
                    self.attr_Gain_read = 0
                elif  "high gain" in gainstr:
                    self.attr_Gain_read = 2
                elif  "ultra high gain" in gainstr:
                    self.attr_Gain_read = 3
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_Gain")
        attr.set_value(self.attr_Gain_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Gain_read
        
    def write_Gain(self, attr):
        self.debug_stream("In write_Gain()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.Gain_write) ENABLED START -----#
        if self.Simulation:
            self.attr_Gain_read = data
            return
        gains = {0:"lowG", 1:"midG", 2:"highG", 3:"uhighG"}
        if data not in gains:
            PyTango.Except.throw_exception("Command error","Invalid gain value!","write_Gain")
        # read the actual gain and send command only if necessary        
        cmd = "SetThreshold"
        reply_cmd = "Settings: "
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_Gain")
        elif reply_cmd in reply:
            gainstr = ((reply.split(reply_cmd)[-1]).split(";")[0])
            if "mid gain" in gainstr:
                self.attr_Gain_read = 1
            elif  "low gain" in gainstr:
                self.attr_Gain_read = 0
            elif  "high gain" in gainstr:
                self.attr_Gain_read = 2
            elif  "ultra high gain" in gainstr:
                self.attr_Gain_read = 3
        else:
            PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","write_Gain")
        if self.attr_Gain_read == data:
            return
        # Get actual threshold
        reply_cmd = "threshold: "
        if reply_cmd in reply:
            actual_Threshold_read = int((reply.split(reply_cmd)[-1]).split(" ")[0])
        else:
            PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","write_Gain")
        cmd = "SetThreshold %s %d" % (gains[data],actual_Threshold_read) 
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_Gain")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_Gain")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.Gain_write
        
    def is_Gain_allowed(self, attr):
        self.debug_stream("In is_Gain_allowed()")
        if attr==PyTango.AttReqType.READ_REQ:
            state_ok = not(self.get_state() in [])
        else:
            state_ok = not(self.get_state() in [PyTango.DevState.DISABLE,
                PyTango.DevState.RUNNING,
                PyTango.DevState.FAULT])
        #----- PROTECTED REGION ID(PilatusXM.is_Gain_allowed) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.is_Gain_allowed
        return state_ok
        
    def read_LastImagePath(self, attr):
        self.debug_stream("In read_LastImagePath()")
        #----- PROTECTED REGION ID(PilatusXM.LastImagePath_read) ENABLED START -----#
        attr.set_value(self.attr_LastImagePath_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.LastImagePath_read
        
    def read_MxSettings(self, attr):
        self.debug_stream("In read_MxSettings()")
        #----- PROTECTED REGION ID(PilatusXM.MxSettings_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "mxsettings"
            reply_cmd = "OK "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_MxSettings")
            elif reply_cmd in reply:
                self.attr_MxSettings_read = (reply.split(reply_cmd)[-1]).strip()
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_MxSettings")
        attr.set_value(self.attr_MxSettings_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.MxSettings_read
        
    def write_MxSettings(self, attr):
        self.debug_stream("In write_MxSettings()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.MxSettings_write) ENABLED START -----#
        if self.Simulation:
            self.attr_MxSettings_read = data
            return
        cmd = "mxsettings %s" % (data)
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_MxSettings")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_MxSettings")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.MxSettings_write
        
    def read_GapFill(self, attr):
        self.debug_stream("In read_GapFill()")
        #----- PROTECTED REGION ID(PilatusXM.GapFill_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "gapfill"
            reply_cmd = "Detector gap-fill is: "
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_GapFill")
            elif reply_cmd in reply:
                self.attr_GapFill_read = int((reply.split(reply_cmd)[-1]).strip())
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_GapFill")
        attr.set_value(self.attr_GapFill_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.GapFill_read
        
    def write_GapFill(self, attr):
        self.debug_stream("In write_GapFill()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.GapFill_write) ENABLED START -----#
        if self.Simulation:
            self.attr_GapFill_read = data
            return
        cmd = "gapfill %d" % (data)
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_GapFill")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_GapFill")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.GapFill_write
        
    def read_LdBadPixMap(self, attr):
        self.debug_stream("In read_LdBadPixMap()")
        #----- PROTECTED REGION ID(PilatusXM.LdBadPixMap_read) ENABLED START -----#
        if not self.Simulation:
            cmd = "ldbadpixmap"
            reply_cmd = "Bad pixels loaded from"
            reply = self.comm.send_to_detector(cmd)
            if reply is None:
                PyTango.Except.throw_exception("Communication error","Pilatus does not reply","read_LdBadPixMap")
            elif reply_cmd in reply:
                self.attr_LdBadPixMap_read = ((reply.split(reply_cmd)[-1]).strip())
            else:
                PyTango.Except.throw_exception("Communication error","Pilatus wrong reply","read_LdBadPixMap")
        attr.set_value(self.attr_LdBadPixMap_read)
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.LdBadPixMap_read
        
    def write_LdBadPixMap(self, attr):
        self.debug_stream("In write_LdBadPixMap()")
        data = attr.get_write_value()
        #----- PROTECTED REGION ID(PilatusXM.LdBadPixMap_write) ENABLED START -----#
        if self.Simulation:
            self.attr_LdBadPixMap_read = data
            return
        cmd = "ldbadpixmap %s" % (data)
        reply = self.comm.send_to_detector(cmd)
        if reply is None:
            PyTango.Except.throw_exception("Communication error","Pilatus does not reply","write_LdBadPixMap")
        elif "ERR" in reply:
            PyTango.Except.throw_exception("Command error","Pilatus reply: %s" % reply,"write_LdBadPixMap")
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.LdBadPixMap_write
        
    
    
            
    def read_attr_hardware(self, data):
        self.debug_stream("In read_attr_hardware()")
        #----- PROTECTED REGION ID(PilatusXM.read_attr_hardware) ENABLED START -----#
        
        #----- PROTECTED REGION END -----#    //    PilatusXM.read_attr_hardware


    # -------------------------------------------------------------------------
    #    PilatusXM command methods
    # -------------------------------------------------------------------------