Source code for cmd_multimeter

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 
# Copyright 2012-2017 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
# 
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pyrame 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 Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame.  If not, see <http://www.gnu.org/licenses/>

import conf_strings,pools,apipools

# CLASS ##########################################################

class multimeter_class():
    multimeter_pool=pools.pool("multimeter")

    def init(self,multimeter_id,conf_string):
        try:
            conf=conf_strings.parse(conf_string)
        except Exception as e:
            return 0,str(e)
        # Initialize MULTIMETER
        model=conf.name
        model_id="mmmodel_%s"%(multimeter_id)
        retcode,res=submod_execcmd("init@"+model,model_id,conf_string)
        if retcode==0:
            return 0,"Error initializing %s power supply <- %s" % (model,res)
        # Get API of the module and add it to the api_pool, if it's not already there
        if not api_pool.is_present(model):
            retcode,res=submod_execcmd("getapi@"+model)
            if retcode==0:
                return 0,"Can't get API for %s <- %s" % (model,res)
            api_pool.add_api_from_string(model,res)
        self.multimeter_pool.new(multimeter_id,{"model":model,"model_id":model_id})
        return 1,"ok"

    def deinit(self,multimeter_id):
        try:
            multimeter=self.multimeter_pool.get(multimeter_id)
        except Exception as e:
            return 0,str(e)
        # Call the deinitializer function for the model
        retcode,res=submod_execcmd("deinit@"+multimeter["model"],multimeter["model_id"])
        if retcode==0:
            return 0,"Error deinitializing %s power supply <- %s" % (multimeter["model"],res)
        # Remove multimeter from the pool
        try:
            self.multimeter_pool.remove(multimeter_id)
        except Exception as e:
            return 0,str(e)
        return retcode,res

    def config(self,multimeter_id):
        try:
            multimeter=self.multimeter_pool.get(multimeter_id)
        except Exception as e:
            return 0,str(e)
        # Call the configuration function for the model
        retcode,res=submod_execcmd("config@"+multimeter["model"],multimeter["model_id"])
        if retcode==0:
            return 0,"Error configuring %s power supply <- %s" % (multimeter["model"],res)
        return retcode,res

    def inval(self,multimeter_id):
        try:
            multimeter=self.multimeter_pool.get(multimeter_id)
        except Exception as e:
            return 0,str(e)
        # Call the invalidation function for the model
        retcode,res=submod_execcmd("inval@"+multimeter["model"],multimeter["model_id"])
        if retcode==0:
            return 0,"Error invalidating %s power supply <- %s" % (multimeter["model"],res)
        return retcode,res

    def relay(self,multimeter_id,function,params,range,resolution):
        try:
            multimeter=self.multimeter_pool.get(multimeter_id)
        except Exception as e:
            return 0,str(e)
        # Call the set_voltage_limit function for the model
        api=api_pool.get_api(multimeter["model"],function+"_"+multimeter["model"])
        if api == None:
            return 0,"The selected MULTIMETER model does not implement this function"
        #range
        if "range" in api:
            params += (range,)
        else:
            if range not in ["undef",""]:
                print("\n\nWarning: The selected MULTIMETER model does not support range selection in this function\n\n")
        #resolution
        if "resolution" in api:
            params += (resolution,)
        else:
            if resolution not in ["undef",""]:
                print("\n\nWarning: The selected MULTIMETER model does not support resolution selection in this function\n\n")
        retcode,res=submod_execcmd(function+"@"+multimeter["model"],multimeter["model_id"],*params)
        if retcode==0:
            return 0,"Error in %s <- %s" % (function+"_"+multimeter["model"],res)
        return 1,res

    def reset(self,multimeter_id):
        return self.relay(multimeter_id,"reset",[],"undef","undef")

    def get_dc_voltage(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_dc_voltage",[],range,resolution)

    def get_ac_voltage(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_ac_voltage",[],range,resolution)

    def get_dc_current(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_dc_current",[],range,resolution)

    def get_ac_current(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_ac_current",[],range,resolution)

    def get_2w_resistance(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_2w_resistance",[],range,resolution)

    def get_4w_resistance(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_4w_resistance",[],range,resolution)

    def get_frequency(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_frequence",[],range,resolution)

    def get_period(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_period",[],range,resolution)

    def get_temp(self,multimeter_id,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"get_temp",[],range,resolution)

    def timed_acq(self,multimeter_id,value_type,nb_measures,period,range="undef",resolution="undef"):
        return self.relay(multimeter_id,"timed_acq",[value_type,nb_measures,period],range,resolution)

    def free_command(self,multimeter_id):
        return self.relay(multimeter_id,"free_command",[],"undef","undef")

    def get_error_queue(self,multimeter_id):
        return self.relay(multimeter_id,"get_error_queue",[],"undef","undef")

# CREATE POOL ####################################################

me=multimeter_class()
api_pool=apipools.api_pool()

# COMMANDS #######################################################

# Functions

[docs]def init_multimeter(multimeter_id,conf_string): """Registers in the pool and initializes a new MULTIMETER. *conf_string* is the configuration string for the module to be initialized""" return me.init(multimeter_id,conf_string)
[docs]def deinit_multimeter(multimeter_id): "Deregister a MULTIMETER from the pool" return me.deinit(multimeter_id)
[docs]def config_multimeter(multimeter_id): "Configure the MULTIMETER" return me.config(multimeter_id)
[docs]def inval_multimeter(multimeter_id): "Invalidate configuration of MULTIMETER" return me.inval(multimeter_id)
[docs]def reset_multimeter(multimeter_id): "Reset MULTIMETER" return me.reset(multimeter_id)
[docs]def get_value_multimeter(multimeter_id,value_type,range="undef",resolution="undef"): "Generic function to get any *value_type*. The function get_VALUE_TYPE on the destination module will be called." return me.relay(multimeter_id,"get_%s"%(value_type),[],range,resolution)
[docs]def get_dc_voltage_multimeter(multimeter_id,range="undef",resolution="undef"): "Get DC voltage in Volts. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_dc_voltage(multimeter_id,range,resolution)
[docs]def get_ac_voltage_multimeter(multimeter_id,range="undef",resolution="undef"): "Get AC voltage in Volts. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_ac_voltage(multimeter_id,range,resolution)
[docs]def get_dc_current_multimeter(multimeter_id,range="undef",resolution="undef"): "Get DC current in Ampers. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_dc_current(multimeter_id,range,resolution)
[docs]def get_ac_current_multimeter(multimeter_id,range="undef",resolution="undef"): "Get AC current in Ampers. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_ac_current(multimeter_id,range,resolution)
[docs]def get_2w_resistance_multimeter(multimeter_id,range="undef",resolution="undef"): "Get 2-wire resitance in Ohms. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_2w_resistance(multimeter_id,range,resolution)
[docs]def get_4w_resistance_multimeter(multimeter_id,range="undef",resolution="undef"): "Get 4-wire resitance in Ohms. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_4w_resistance(multimeter_id,range,resolution)
[docs]def get_frequency_multimeter(multimeter_id,range="undef",resolution="undef"): "Get frequency in Hertzs. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_frequency(multimeter_id,range,resolution)
[docs]def get_period_multimeter(multimeter_id,range="undef",resolution="undef"): "Get period in seconds. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_period(multimeter_id,range,resolution)
[docs]def get_temp_multimeter(multimeter_id,range="undef",resolution="undef"): "Get temperature in degrees Celcius. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return me.get_temp(multimeter_id,range,resolution)
[docs]def get_rh_multimeter(multimeter_id,range="undef",resolution="undef"): "Get relative humidity in percent. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return get_value_multimeter(multimeter_id,"rh",range,resolution)
[docs]def get_pressure_multimeter(multimeter_id,range="undef",resolution="undef"): "Get pressure. Optional *range* argument for scale of measurement. Optional *resolution* argument for either integration time or allowable error. See documentation of the particular model for allowed values." return get_value_multimeter(multimeter_id,"pressure",range,resolution)
def timed_acq_multimeter(multimeter_id,value_type,nb_measures,period,range="undef",resolution="undef"): return me.timed_acq(multimeter_id,value_type,nb_measures,period,range,resolution)
[docs]def free_command_multimeter(multimeter_id,command): "Send free command MULTIMETER" return me.free_command_multimeter(multimeter_id)
[docs]def get_error_queue_multimeter(multimeter_id): "Read error queue" return me.get_error_queue(multimeter_id)